Paperclip: how to resize an image only if it is large enough?

I use Paperclip to resize images as follows:

class Asset < ActiveRecord::Base has_attached_file :asset, :styles => { :thumb => "80x80#", :medium => "1280x800>" }, ... 

When the size of the original image is 32x32 :

  • The resulting medium image is the same size (i.e. 32x32 ), but the file is a little different and the image looks a bit resized. Why is this?

  • As a result, the thumb size is 80x80 (it looks stretched to fit that size). How could I avoid resizing the image when it is too small. Suppose the original image dimensions are in the width and height variables.

+8
ruby-on-rails ruby-on-rails-3 imagemagick paperclip
source share
1 answer

(1) Possibly because Paperclip decrypts the JPEG and then writes / encodes the new JPEG. JPEG is a lossy format, so each encoding degrades the image. You can use convert_options to adjust the quality of the JPEGs, but you may have to accept some degradation in your JPEGs.

(2) is that Paperclip does what he said. Paperclip uses ImageMagick for heavy lifting, and style dimensions are ImageMagick geometry strings with one modification :

Paperclip also adds the "#" option (for example, "50x50 #"), which resizes the image as close as possible, and then crop the rest (center-weighted).

In your style :thumb , "#" is used, so you tell Paperclip that you want an 80x80 image, even if you need to scale and crop the image to get there. You can drop the "#" from the measurement line and, if desired or necessary, add one of the other modifiers .

+6
source share

All Articles