Reputation: 1670
I have a Photo
model that has 2 styles :original
and :medium
where :medium
is a cropped version of the original. I would now like to add a :small
style that's just a resized version of the :medium
. For new images everything works: I just crop the original image twice, once for the :medium
and once for the :small
style. But I also have several thousand existing images that need to be reprocessed to have a :small
thumb (all stored on AWS S3). Unfortunately, I can't just call .reprocess! :small
since it will make small versions from the original, while I need small versions based on the cropped :medium
version.
The medium versions have been cropped by users so I can't just reprocess the originals.
Is there an easy way to do this using Paperclip or do I have to write a script to pull the :medium
version from S3, resize it locally and then ship it to the :small
directory on S3?
UPDATE 1:
These are my styles
:original, { geometry: "1500x1500>", format: :jpg },
:medium, { geometry: "650x650#", processors: [:cropper], format: :jpg },
:small, { geometry: "262x262#", processors: [:cropper], format: :jpg }
Notice that I've already added the :small
style, but I still need to generate :small
thumbs for images that have been created before this addition.
UPDATE 2: The way to do this it is probably using a rake task that would:
:medium
image from S3 for each photo that was created before :small
style has been added:small
size/some/path/to/small/image/
on S3Just not sure where to start.
Upvotes: 0
Views: 381
Reputation: 6942
Looks like you could use a custom processor. In the past I've copied processors from paperclip and modified them to do what I need. For example if you take the thumbnail processor, modify it so that it crops your thumbnail and resizes it the way you want, and then save it as a custom processor (it should aotu-load if you put it in lib/paperclip).
Either way. Copy the file from https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/thumbnail.rb
Edit the section:
def transformation_command
scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
trans = []
trans << "-coalesce" if animated?
trans << "-auto-orient" if auto_orient
trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty?
trans << "-crop" << %["#{crop}"] << "+repage" if crop
trans << '-layers "optimize"' if animated?
trans
end
to crop the image before you resize it. My gess is that you'll have to swap the resize and crop commands, but I'm not sure. All paperclip does is run imagemagick commands so you might find some good documentation here: http://www.imagemagick.org/script/command-line-processing.php
Save this as a custom processor in the lib/paperclip/your_processor_name.rb, and make whatever style you need style like this:
:original, { geometry: "1500x1500>", format: :jpg },
:medium, { geometry: "650x650#", processors: [:your_processor_name], format: :jpg },
:small, { geometry: "262x262#", processors: [:your_processor_name], format: :jpg }
The bottom line is that you can only crop from an original, so if you need custom processing you have to build it. You'll have to play around with it, but this will get you on the right track.
Upvotes: 0