Sunday, March 20, 2011

How do I throw away the original image file and just keep the resizes with Paperclip and Rails 3?

I am using Paperclip to, among other things, allow a registered user to upload an avatar for use on thier profile. I want to store a big and small version of the image which will be rmagicked to standard sizes. Now, what i want to do, is store these two standard sizes (:normal and :tiny, for example.) but I do not want to store the :original.

This will be nice for several reasons as I will never display or use any version than the two standard (re)sizes.

From stackoverflow
  • I can't think of an way to do that with Paperclip directly, but you could remove the original manually after creating the record. An example could look like this:

    class Photo
      has_attached_file :photo
    
      after_create :destroy_original
    
      protected
    
        def destroy_original
          # photo.url will look something like /system/photos/1/original.png
          File.unlink("#{Rails.root}/public#{self.photo.url}")
        end
    
    end
    
    NJ : I like this idea, but I am using S3 and my whole intent is to save transfer and storage costs. Not using the local file system also makes it more complicated. I should have said. Thank you, though!
  • What is your reasoning for wanting to delete the files? File storage is so cheap now, that it's not really a valid reason anymore.

    I would advise against deleting the original files. If you ever decide you want to resize the files using Paperclip's rake tasks, you will need the originals.

    NJ : A good question. I plan on only using the avatar images in two sizes. I want them all to be standard so I resize whatever they upload into the two versions. You have a point, I just thought it would be worth implementing if not too difficult. I really mostly wanted to know how, I guess. Maybe it is not an intended use of Paperclip. :) I'll just set a max size so people aren't uploading massive originals! Thanks!

0 comments:

Post a Comment