Forum: Ruby on Rails attachment_fu cropping

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Aa85bbef6911f5facb5a49a48ddd42b5?d=identicon&s=25 (Guest)
on 2007-04-10 13:13
(Received via mailing list)
attachment_fu doesn't have cropping, I'm trying to bake some in using

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) temp_path
    callback_with_args :after_resize, img

  size = size.first if size.is_a?(Array) && size.length == 1
  if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?
    if size.is_a?(Fixnum)
      img.thumbnail(size, &grab_dimensions)
      img.resize(size[0], size[1], &grab_dimensions)
    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

      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 )

      img.resize(new_size[0], new_size[1], &grab_dimensions)
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris Taggart (christ)
on 2007-04-11 00:28
(Received via mailing list) 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
Here's the relevant post:
and the diff is at:

Hope this helps,
stuff about old cars
Aa85bbef6911f5facb5a49a48ddd42b5?d=identicon&s=25 (Guest)
on 2007-04-11 03:11
(Received via mailing list)
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?
Aa85bbef6911f5facb5a49a48ddd42b5?d=identicon&s=25 (Guest)
on 2007-04-11 05:53
(Received via mailing list)
I'm getting a 'Bitmap has already been freed' error, in case that

On Apr 11, 9:10 am, ""
Aa85bbef6911f5facb5a49a48ddd42b5?d=identicon&s=25 (Guest)
on 2007-04-11 17:42
(Received via mailing list)
Blogged the answer.

On Apr 11, 11:53 am, ""
F719c6cb34bd5b691c12b373b7244b4d?d=identicon&s=25 Dorian Mcfarland (tachekent)
on 2007-05-06 01:18
(Received via mailing list)

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 &
could I have missed something?


> Chris
> -------------------------------
> stuff about old cars
> >

I do things for love or money

this one's for love:
(and money)
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris Taggart (christ)
on 2007-05-08 09:29
(Received via mailing list)
Dorian Mcfarland wrote:
>> I posted a way to do this on railsweenie (with the diff on pastie).
>> -------------------------------
> this one's for love:
> (and money)
> >
Do the tests pass OK? Also, just to check you restarted the server after
making the change...

stuff about old cars

Stuff about old cars
This topic is locked and can not be replied to.