Cropping with Attachment Fu

Hello-

I’m trying to work out the options for cropping an image into a square
image. Basically, I want a picture turned into a 200x200 image. If the
source picture is in landscape mode, then I want slivers of the right
and left hand side of the picture cut off. If the source is in
portrait mode, then the slivers would be on the top and bottom. Does
anyone have an example of this sort of configuration?

Regards-
Eric

Eric M. wrote:

Hello-

I’m trying to work out the options for cropping an image into a square
image. Basically, I want a picture turned into a 200x200 image. If the
source picture is in landscape mode, then I want slivers of the right
and left hand side of the picture cut off. If the source is in
portrait mode, then the slivers would be on the top and bottom. Does
anyone have an example of this sort of configuration?

Regards-
Eric

Hi Eric

This link should help;
http://brendanlim.com/2007/7/28/crop-images-using-attachment_fu-and-rmagick

However, if you can afford to do so, I would consider swapping
attachment_fu for the Paperclip plugin - it makes cropped thumbnails
really, really easy;

http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails/

Thanks Neil-

The first link helped a lot. I like the look of Paperclip as well. I
might have to try it out on my next project.

-Eric

If you don’t want to rewrite any of the attachment_fu files (which
might make updates a little bit of a problem) try this instead. Put
the following code into whichever model has has_attachment

protected

def resize_image(img, size)
# resize_image take size in a number of formats, we just want
# Strings in the form of “crop: WxH”
if (size.is_a?(String) && size =~ /^crop: (\d*)x(\d*)/i) ||
(size.is_a?(Array) && size.first.is_a?(String) &&
size.first =~ /^crop: (\d*)x(\d*)/i)
img.crop_resized!($1.to_i, $2.to_i)
# We need to save the resized image in the same way the
# orignal does.
self.temp_path = write_to_temp_file(img.to_blob)
else
super # Otherwise let attachment_fu handle it
end
end

This will override the resize_image method in attachment_fu. Now
instead of something like

has_attachment :content_type => :image,
:storage => :s3,
:max_size => 1024.kilobytes,
:resize_to => ‘256x160>’,
:thumbnails => {:thumb => ‘100x100>’, :tiny =>
‘50x50’},
:processor => ‘Rmagick’

you can do

has_attachment :content_type => :image,
:storage => :s3,
:max_size => 1024.kilobytes,
:resize_to => ‘256x160>’,
:thumbnails => {:thumb => ‘crop: 100x100>’, :tiny =>
‘crop: 50x50’},
:processor => ‘Rmagick’

(Notice that thumb and tiny changed to ‘crop: XxX’). If you leave crop
out it will work as normal, put crop in and it crops the image down to
the right size. This works with RMagick - in particular the
crop_resized call, I don’t know how to do this for ImageMagick or any
of the other libs - sorry.

D.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs