lib/piet.rb in piet-0.2.0 vs lib/piet.rb in piet-0.2.1

- old
+ new

@@ -1,12 +1,10 @@ require 'png_quantizator' require 'piet/carrierwave_extension' module Piet class << self - VALID_EXTS = %w{ png gif jpg jpeg } - def optimize(path, opts={}) output = optimize_for(path, opts) puts output if opts[:verbose] true end @@ -16,23 +14,24 @@ end private def optimize_for(path, opts) - case extension(path) + case mimetype(path) when "png", "gif" then optimize_png(path, opts) - when "jpg", "jpeg" then optimize_jpg(path, opts) + when "jpeg" then optimize_jpg(path, opts) end end - def extension(path) - path.split(".").last.downcase + def mimetype(path) + IO.popen(['file', '--brief', '--mime-type', path], in: :close, err: :close).read.chomp.sub(/image\//, '') end def optimize_png(path, opts) + level = (0..7).include?(opts[:level]) ? opts[:level] : 7 vo = opts[:verbose] ? "-v" : "-quiet" path.gsub!(/([\(\)\[\]\{\}\*\?\\])/, '\\\\\1') - `#{command_path("optipng")} -o7 #{opts[:command_options]} #{vo} #{path}` + `#{command_path("optipng")} -o#{level} #{opts[:command_options]} #{vo} #{path}` end def optimize_jpg(path, opts) quality = (0..100).include?(opts[:quality]) ? opts[:quality] : 100 vo = opts[:verbose] ? "-v" : "-q"