Permalink
Browse files

Allow nil geometry for thumbnails.

Useful when we just want to set the convert_options but not actually
resize the 'thumbnail'.
  • Loading branch information...
1 parent 10c79b2 commit 55b07e45c6b4ce4ccc6c6f78f0010ae1292008ac Darrick Wiebe committed Feb 12, 2009
Showing with 21 additions and 10 deletions.
  1. +18 −8 lib/paperclip/geometry.rb
  2. +3 −2 lib/paperclip/thumbnail.rb
View
@@ -26,7 +26,9 @@ def self.from_file file
# Parses a "WxH" formatted string, where W is the width and H is the height.
def self.parse string
- if match = (string && string.match(/\b(\d*)x?(\d*)\b([\>\<\#\@\%^!])?/))
+ if string.blank?
+ nil
+ elsif match = (string && string.match(/\b(\d*)x?(\d*)\b([\>\<\#\@\%^!])?/))
Geometry.new(*match[1,3])
end
end
@@ -82,16 +84,24 @@ def inspect
# destination Geometry would be completely filled by the source image, and any
# overhanging image would be cropped. Useful for square thumbnail images. The cropping
# is weighted at the center of the Geometry.
+ #
+ # If the destination geometry is nil, return nil scale and crop which enables
+ # transformation calls to be made without specifying any resize flag. For
+ # instance, using convert_options a user may draw text on an image without
+ # resizing it.
def transformation_to dst, crop = false
- if crop
- ratio = Geometry.new( dst.width / self.width, dst.height / self.height )
- scale_geometry, scale = scaling(dst, ratio)
- crop_geometry = cropping(dst, ratio, scale)
+ if dst
+ if crop
+ ratio = Geometry.new( dst.width / self.width, dst.height / self.height )
+ scale_geometry, scale = scaling(dst, ratio)
+ crop_geometry = cropping(dst, ratio, scale)
+ else
+ scale_geometry = dst.to_s
+ end
+ [ scale_geometry, crop_geometry ]
else
- scale_geometry = dst.to_s
+ [ nil, nil ]
end
-
- [ scale_geometry, crop_geometry ]
end
private
@@ -12,7 +12,7 @@ class Thumbnail < Processor
# set, the options will be appended to the convert command upon image conversion
def initialize file, options = {}, attachment = nil
super
- geometry = options[:geometry]
+ geometry = options[:geometry] || ''
@file = file
@crop = geometry[-1,1] == '#'
@target_geometry = Geometry.parse geometry
@@ -61,7 +61,8 @@ def make
# into the thumbnail.
def transformation_command
scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
- trans = "-resize \"#{scale}\""
+ trans = ''
+ trans << "-resize \"#{scale}\"" if scale
trans << " -crop \"#{crop}\" +repage" if crop
trans << " #{convert_options}" if convert_options?
trans

0 comments on commit 55b07e4

Please sign in to comment.