Commit 9412f71b by jyurek

Switched up calculations for cropping

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@270 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent da74efdd
module Paperclip module Paperclip
class Thumbnail class Thumbnail
class Geometry
attr_accessor :height, :width
def initialize width = nil, height = nil
@height = (height || width).to_f
@width = (width || height).to_f
end
def self.parse string
if match = (string && string.match(/(\d*)x(\d*)/))
Geometry.new(*match[1,2])
end
end
def square?
height == width
end
def horizontal?
height < width
end
def vertical?
height > width
end
def aspect
width / height
end
def larger
[height, width].max
end
def smaller
[height, width].min
end
def to_s
"#{width}x#{height}"
end
def inspect
to_s
end
end
attr_accessor :geometry, :data attr_accessor :geometry, :data
def initialize geometry, data def initialize geometry, data
...@@ -35,29 +81,20 @@ module Paperclip ...@@ -35,29 +81,20 @@ module Paperclip
identify = Paperclip.path_for_command("identify") identify = Paperclip.path_for_command("identify")
piping data, :to => "#{identify} - 2>/dev/null" do |pipeout| piping data, :to => "#{identify} - 2>/dev/null" do |pipeout|
dimensions = pipeout.split[2] dimensions = pipeout.split[2]
if dimensions && (match = dimensions.match(/(\d+)x(\d+)/)) if src = Geometry.parse(dimensions)
src = match[1,2].map(&:to_f) dst = Geometry.parse(geometry)
srch = src[0] > src[1]
dst = geometry.match(/(\d+)x(\d+)/)[1,2].map(&:to_f) ratio = Geometry.new( dst.width / src.width, dst.height / src.height )
dsth = dst[0] > dst[1] scale_geometry, scale = if ratio.horizontal? || ratio.square?
ar = src[0] / src[1] [ "%dx" % dst.width, ratio.width ]
scale_geometry, scale = if dst[0] == dst[1]
if srch
[ "x#{dst[1].to_i}", src[1] / dst[1] ]
else
[ "#{dst[0].to_i}x", src[0] / dst[0] ]
end
elsif dsth
[ "#{dst[0].to_i}x", src[0] / dst[0] ]
else else
[ "x#{dst[1].to_i}", src[1] / dst[1] ] [ "x%d" % dst.height, ratio.height ]
end end
crop_geometry = if dsth crop_geometry = if ratio.horizontal? || ratio.square?
"%dx%d+%d+%d" % [ dst[0], dst[1], 0, (src[1] / scale - dst[1]) / 2 ] "%dx%d+%d+%d" % [ dst.width, dst.height, 0, (src.height * scale - dst.height) / 2 ]
else else
"%dx%d+%d+%d" % [ dst[0], dst[1], (src[0] / scale - dst[0]) / 2, 0 ] "%dx%d+%d+%d" % [ dst.width, dst.height, (src.width * scale - dst.width) / 2, 0 ]
end end
[ scale_geometry, crop_geometry ] [ scale_geometry, crop_geometry ]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment