Rails, Paperclip: Downsizing image minimum amount such that one dimension matches geometry

StackOverflow https://stackoverflow.com/questions/23119022

سؤال

I'm using Paperclip in a Rails project to resize an important image and have tried the following:

has_attached_file :attachment, :styles => { :medium => ['640x480>', :jpg] }
has_attached_file :attachment, :styles => { :medium => ['640x480^#', :jpg] }

I'd like to downsize an image the minimum amount necessary so that the height=640 or width=480. For instance a source image which is 1920x1080 would be resized to 853x480, and a source image which is 1080x1920 would be resized to 640x1137. None of the geometry arguments listed in the ImageMagick documentation seem to have the desired effect.

Is there a geometry argument that would accomplish the resizing I desire? If not, how might this best be accomplished?

هل كانت مفيدة؟

المحلول

As posted in the comments the fill flag (^) does exactly what Mark needs. That flag is used to resize the image based on the smallest fitting dimension. That is, the image is resized to completely fill (and even overflow) the pixel area given. The code should be changed to this:

has_attached_file :attachment, :styles => { :medium => ['640x480^', :jpg] }

Documentation about this flag can be found here: http://www.imagemagick.org/Usage/resize/#fill.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top