# Cropping with Attachment Fu

**URL:** <https://www.ruby-forum.com/t/cropping-with-attachment-fu/147915>\
**Category:** Rails\
**Created:** [September 6, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/cropping-with-attachment-fu/147915 "2008-09-06T23:40:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [September 6, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/cropping-with-attachment-fu/147915/1 "2008-09-06T23:40:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [September 7, 2008, 6:50pm UTC](https://www.ruby-forum.com/t/cropping-with-attachment-fu/147915/2 "2008-09-07T18:50:10Z")

</div>

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](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/](http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails/)

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [September 8, 2008, 11:04pm UTC](https://www.ruby-forum.com/t/cropping-with-attachment-fu/147915/3 "2008-09-08T23:04:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![snowbird122](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c68b51/32.png) [@snowbird122](https://www.ruby-forum.com/u/snowbird122)\
**Post date:** [September 9, 2008, 7:18am UTC](https://www.ruby-forum.com/t/cropping-with-attachment-fu/147915/4 "2008-09-09T07:18:22Z")

</div>

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.
