attachment_fu cropping

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

andycroll@deepcalm.com wrote:

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

>

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
and the diff is at:
http://pastie.caboo.se/48191

Hope this helps,
Chris

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?

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

Blogged the answer. http://www.deepcalm.com/writing/cropped-thumbnails-in-attachment_fu-using-imagescience

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

Dorian Mcfarland wrote:

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

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
and the diff is at:
http://pastie.caboo.se/48191

Hope this helps,
Chris
-------------------------------
http://autopendium.co.uk
stuff about old cars

~~~
I do things for love or money

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

>

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

Chris