# Attachment\_fu cropping

**URL:** <https://www.ruby-forum.com/t/attachment-fu-cropping/91558>\
**Category:** Rails\
**Created:** [April 10, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/attachment-fu-cropping/91558 "2007-04-10T13:13:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 10, 2007, 1:13pm UTC](https://www.ruby-forum.com/t/attachment-fu-cropping/91558/1 "2007-04-10T13:13:22Z")

</div>

attachment\_fu doesn’t have cropping, I’m trying to bake some in using  
ImageScience.

Firstly this requires the enabling of the ‘!’ modifier in the  
geometry.rb file. However in the to\_s method I don’t think it’s  
passing out the flags correctly. So I changed ‘FLAGS[@flag.to\_i]’ to  
‘RFLAGS.index(@flag)’ on line 47.

However now I need to change the logic in the  
image\_science\_processor.rb file to recognise the “!” aspect flag and  
then execute the crop and resize.

My issue is in doing two transformations, I don’t really understand  
what the &grab\_dimensions argument passed to the with\_crop and resize  
methods is doing… please excuse my noob-ness.

I post my modified resize\_image method to see if anyone can help…

def resize\_image(img, size)

# create a dummy temp file to write to

self.temp\_path = write\_to\_temp\_file(filename)  
grab\_dimensions = lambda do |img|  
self.width = img.width if respond\_to?(:width)  
self.height = img.height if respond\_to?(:height)  
img.save temp\_path  
callback\_with\_args :after\_resize, img  
end

size = size.first if size.is\_a?(Array) && size.length == 1  
if size.is\_a?(Fixnum) || (size.is\_a?(Array) && size.first.is\_a?  
(Fixnum))  
if size.is\_a?(Fixnum)  
img.thumbnail(size, &grab\_dimensions)  
else  
img.resize(size[0], size[1], &grab\_dimensions)  
end  
else  
new\_size = [img.width, img.height] / size.to\_s  
# Added logic here  
if size.ends\_with? “!”  
aspect = new\_size[0].to\_f / new\_size[1].to\_f  
ih, iw = img.height, img.width  
w, h = (ih \* aspect), (iw / aspect)  
w = [iw, w].min.to\_i  
h = [ih, h].min.to\_i

```
  # THIS IS WHERE THE ISSUE IS!
  img.with_crop( iw/2-w/2, ih/2-h/2, iw/2+w/2, ih/2+h/2,

```

&grab\_dimensions )  
img.resize(new\_size[0], new\_size[1], &grab\_dimensions )

```
else
  img.resize(new_size[0], new_size[1], &grab_dimensions)
end

```

end  
end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 11, 2007, 12:28am UTC](https://www.ruby-forum.com/t/attachment-fu-cropping/91558/2 "2007-04-11T00:28:28Z")

</div>

removed\_email\_address@domain.invalid wrote:

> then execute the crop and resize.  
> grab\_dimensions = lambda do |img|  
> img.thumbnail(size, &grab\_dimensions)  
> w = [iw, w].min.to\_i  
> end  
> end
> 
> >

I posted a way to do this on railsweenie (with the diff on pastie). It’s  
a little bit hackish (works on ImageScience and RMagick), but works for  
me.  
Here’s the relevant post:  
[http://www.railsweenie.com/forums/3/topics/1145](http://www.railsweenie.com/forums/3/topics/1145)  
and the diff is at:  
[http://pastie.caboo.se/48191](http://pastie.caboo.se/48191)

## Hope this helps, Chris

[http://autopendium.co.uk](http://autopendium.co.uk)  
stuff about old cars

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 11, 2007, 3:11am UTC](https://www.ruby-forum.com/t/attachment-fu-cropping/91558/3 "2007-04-11T03:11:36Z")

</div>

I did see your solution Chris, it helped a lot in translating what the  
methods were doing. However the ImageScience cropped thumbnail  
function provides only a square image, my solution requires a cropped  
thumbnail to the width and height that it is passed…

Or at least it would if I could do two transformations on it!

Can anyone explain what I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 11, 2007, 5:53am UTC](https://www.ruby-forum.com/t/attachment-fu-cropping/91558/4 "2007-04-11T05:53:44Z")

</div>

I’m getting a ‘Bitmap has already been freed’ error, in case that  
helps.

On Apr 11, 9:10 am, “removed\_email\_address@domain.invalid”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 6, 2007, 1:18am UTC](https://www.ruby-forum.com/t/attachment-fu-cropping/91558/5 "2007-05-06T01:18:44Z")

</div>

> 

Hi Chris,  
thanks for that. I applied the patch, but didn’t seem to see any  
difference to my results. If I do :square =\> “100!” I get w=100 &  
h=original-height.  
could I have missed something?

dorian

> ## Chris
> 
> [http://autopendium.co.uk](http://autopendium.co.uk)  
> stuff about old cars
> 
> >

```auto
I do things for love or money

this one's for love:
http://www.marseillefigs.org
(and money)
```

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 8, 2007, 9:29am UTC](https://www.ruby-forum.com/t/attachment-fu-cropping/91558/6 "2007-05-08T09:29:54Z")

</div>

Dorian M. wrote:

> > ## I posted a way to do this on railsweenie (with the diff on pastie).
> 
> this one’s for love:  
> [http://www.marseillefigs.org](http://www.marseillefigs.org)  
> (and money)
> 
> >

Do the tests pass OK? Also, just to check you restarted the server after  
making the change…

## Chris

[http://autopendium.co.uk](http://autopendium.co.uk)  
stuff about old cars

## –

[http://www.autopendium.co.uk](http://www.autopendium.co.uk)  
Stuff about old cars

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 11, 2007, 5:42pm UTC](https://www.ruby-forum.com/t/attachment-fu-cropping/91558/7 "2007-04-11T17:42:17Z")

</div>

Blogged the answer.  
[http://www.deepcalm.com/writing/cropped-thumbnails-in-attachment\_fu-using-imagescience](http://www.deepcalm.com/writing/cropped-thumbnails-in-attachment_fu-using-imagescience)

On Apr 11, 11:53 am, “removed\_email\_address@domain.invalid”
