Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize original image in Paperclip

Paperclip stores original images in "original" folder. Is there a way to resize the original images? I want to make the originals smaller in order to save the disc space.

So, for example, if visitor uploads a photo with 2592x1936 I want to store it as 1024x1024, the same way we set the dimensions for :thumb images in :styles

Update (solved)

I found out how to resize original images automatically on upload. One just needs to add :original to styles:

class MyModel < ActiveRecord::Base
    has_attached_file :photo, 
        :styles => { :original => "1024x1024>", :thumb => "150x150>" }
end
like image 215
Evgenii Avatar asked Aug 11 '11 14:08

Evgenii


1 Answers

I'm not sure paperclip does resizing by itself. You might have to look at Rmagick to get this done. I would try to get RMagick going (or minimagick) and then use a before_save callback to execute a :resize method that you write that tells RMagic to resize the image. Your method might look like:

class Image < ActiveRecord::Base
  belongs_to :profile
  before_save :resize

  def resize
    self.image = self.image.resize "1024x1024"
  end
end

or

class Image < ActiveRecord::Base
  belongs_to :profile
  before_save do
    self.image = self.image.resize "1024x1024"
  end
end
like image 164
Caley Woods Avatar answered Nov 04 '22 12:11

Caley Woods