FIX: don't use 'convert' to optimize GIF + refactor
This commit is contained in:
parent
01b4fd16cd
commit
d5828d3743
|
@ -81,69 +81,74 @@ class OptimizedImage < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def self.resize_instructions(from, to, width, height, opts={})
|
||||
def self.resize_instructions(from, to, dimensions, opts={})
|
||||
# NOTE: ORDER is important!
|
||||
if !!opts[:allow_animation] && from =~ /\.GIF$/i
|
||||
%W{
|
||||
#{from}
|
||||
-coalesce
|
||||
-gravity center
|
||||
-thumbnail #{width}x#{height}^
|
||||
-extent #{width}x#{height}
|
||||
-layers optimize
|
||||
#{to}
|
||||
}
|
||||
else
|
||||
%W{
|
||||
#{from}[0]
|
||||
-gravity center
|
||||
-background transparent
|
||||
-thumbnail #{width}x#{height}^
|
||||
-extent #{width}x#{height}
|
||||
-interpolate bicubic
|
||||
-unsharp 2x0.5+0.7+0
|
||||
-quality 98
|
||||
#{to}
|
||||
}
|
||||
end
|
||||
%W{
|
||||
#{from}[0]
|
||||
-gravity center
|
||||
-background transparent
|
||||
-thumbnail #{dimensions}^
|
||||
-extent #{dimensions}
|
||||
-interpolate bicubic
|
||||
-unsharp 2x0.5+0.7+0
|
||||
-quality 98
|
||||
#{to}
|
||||
}
|
||||
end
|
||||
|
||||
def self.downsize_instructions(from, to, max_width, max_height, opts={})
|
||||
dimensions = "#{max_width}x#{max_height}"
|
||||
dimensions += !!opts[:force_aspect_ratio] ? "\\!" : "\\>"
|
||||
def self.resize_instructions_animated(from, to, dimensions, opts={})
|
||||
%W{
|
||||
#{from}
|
||||
-coalesce
|
||||
-gravity center
|
||||
-thumbnail #{dimensions}^
|
||||
-extent #{dimensions}
|
||||
#{to}
|
||||
}
|
||||
end
|
||||
|
||||
if !!opts[:allow_animation] && from =~ /\.GIF$/i
|
||||
%W{
|
||||
#{from}
|
||||
-coalesce
|
||||
-gravity center
|
||||
-background transparent
|
||||
-thumbnail #{dimensions}
|
||||
-layers optimize
|
||||
#{to}
|
||||
}
|
||||
else
|
||||
%W{
|
||||
#{from}[0]
|
||||
-gravity center
|
||||
-background transparent
|
||||
-thumbnail #{dimensions}
|
||||
#{to}
|
||||
}
|
||||
end
|
||||
def self.downsize_instructions(from, to, dimensions, opts={})
|
||||
%W{
|
||||
#{from}[0]
|
||||
-gravity center
|
||||
-background transparent
|
||||
-thumbnail #{dimensions}#{!!opts[:force_aspect_ratio] ? "\\!" : "\\>"}
|
||||
#{to}
|
||||
}
|
||||
end
|
||||
|
||||
def self.downsize_instructions_animated(from, to, dimensions, opts={})
|
||||
%W{
|
||||
#{from}
|
||||
-coalesce
|
||||
-gravity center
|
||||
-background transparent
|
||||
-thumbnail #{dimensions}#{!!opts[:force_aspect_ratio] ? "\\!" : "\\>"}
|
||||
#{to}
|
||||
}
|
||||
end
|
||||
|
||||
def self.resize(from, to, width, height, opts={})
|
||||
instructions = resize_instructions(from, to, width, height, opts)
|
||||
convert_and_optimize_with(instructions)
|
||||
optimize("resize", from, to, width, height, opts)
|
||||
end
|
||||
|
||||
def self.downsize(from, to, max_width, max_height, opts={})
|
||||
instructions = downsize_instructions(from, to, max_width, max_height, opts)
|
||||
convert_and_optimize_with(instructions)
|
||||
optimize("downsize", from, to, max_width, max_height, opts)
|
||||
end
|
||||
|
||||
def self.convert_and_optimize_with(instructions)
|
||||
def self.optimize(operation, from, to, width, height, opts={})
|
||||
dim = dimensions(width, height)
|
||||
method_name = "#{operation}_instructions"
|
||||
method_name += "_animated" if !!opts[:allow_animation] && from =~ /\.GIF$/i
|
||||
instructions = self.send(method_name.to_sym, from, to, dim, opts)
|
||||
convert_with(instructions)
|
||||
end
|
||||
|
||||
def self.dimensions(width, height)
|
||||
"#{width}x#{height}"
|
||||
end
|
||||
|
||||
def self.convert_with(instructions)
|
||||
`convert #{instructions.join(" ")}`
|
||||
|
||||
return false if $?.exitstatus != 0
|
||||
|
|
Loading…
Reference in New Issue