Class: Jekyll::Webp::WebpExec
- Inherits:
-
Object
- Object
- Jekyll::Webp::WebpExec
- Defined in:
- lib/jekyll-webp-resize/webpExec.rb
Class Method Summary collapse
-
.exe_name ⇒ Object
Returns the correct executable name depending on the OS platform and OS architecture.
-
.run(input_file, output_file, flags, size) ⇒ Object
Runs the WebP executable for the given input parameters the function detects the OS platform and architecture automatically.
- .run_cmd(cmd) ⇒ Object
Class Method Details
.exe_name ⇒ Object
Returns the correct executable name depending on the OS platform and OS architecture
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/jekyll-webp-resize/webpExec.rb', line 71 def self.exe_name if OS.mac? return "osx-cwebp" elsif OS.windows? if OS.x32? return "win-x86-cwebp.exe" else return "win-x64-cwebp.exe" end elsif OS.unix? || OS.linux? if OS.x32? return "linux-x86-cwebp" else return "linux-x64-cwebp" end else raise ArgumentError.new("OS platform could not be identified (gem can only be run on linux,osx or windows)") end end |
.run(input_file, output_file, flags, size) ⇒ Object
Runs the WebP executable for the given input parameters the function detects the OS platform and architecture automatically
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/jekyll-webp-resize/webpExec.rb', line 29 def self.run(input_file, output_file, flags, size) width, height = FastImage.size(input_file) # What is the path to the execs inside the gem? perhaps just bin/? bin_path = "bin/" # What is the OS and architecture specific executable name? exe_name = WebpExec.exe_name # We need to locate the Gems bin path as we're currently running inside the # jekyll site working directory # http://stackoverflow.com/a/10083594/779521 gem_spec = Gem::Specification.find_by_name("jekyll-webp-resize") gem_root = gem_spec.gem_dir # Construct the full path to the executable full_path = File.join(gem_root, bin_path, exe_name) Jekyll.logger.info("Webp:", "Generating #{output_file}") # Construct the full program call cmd = "convert #{flags} \"#{input_file}\" " if size != 0 cmd += "-filter Lanczos -resize \"#{size.to_s}>\" " end cmd += "\"#{output_file}\"" Jekyll.logger.info(cmd) run_cmd(cmd) extension = File.extname(output_file) basename = File.basename(output_file, extension) path = File.dirname(output_file) new_output_file = path + "/" + basename + ".jpg" cmd = "convert \"#{input_file}\" -quality 94% -define jpeg:dct-method=float -strip -interlace Plane " if size != 0 cmd += "-filter Lanczos -resize \"#{size.to_s}>\" " end cmd += "\"#{new_output_file}\"" Jekyll.logger.info(cmd) run_cmd(cmd) end |
.run_cmd(cmd) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/jekyll-webp-resize/webpExec.rb', line 7 def self.run_cmd(cmd) # Execute the command exit_code = 0 error = "" output = "" Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr| stdin.close # we don't pass any input to the process output = stdout.gets error = stderr.gets exit_code = wait_thr.value end if exit_code != 0 Jekyll.logger.error("WebP:","command returned #{exit_code} with error #{error}") end # Return any captured return value return [output, error] end |