andycroll-AtINh79MtmZWk0Htik3J/w@public.gmane.org
2007-Apr-10 11:12 UTC
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
andycroll-AtINh79MtmZWk0Htik3J/w@public.gmane.org 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 ------------------------------- http://autopendium.co.uk stuff about old cars --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
andycroll-AtINh79MtmZWk0Htik3J/w@public.gmane.org
2007-Apr-11 01:10 UTC
Re: attachment_fu cropping
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? On Apr 11, 6:27 am, Chris T <ctmailingli...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> andycr...-AtINh79MtmZWk0Htik3J/w@public.gmane.org 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 > -------------------------------http://autopendium.co.uk > stuff about old cars--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
andycroll-AtINh79MtmZWk0Htik3J/w@public.gmane.org
2007-Apr-11 03:53 UTC
Re: attachment_fu cropping
I''m getting a ''Bitmap has already been freed'' error, in case that helps. On Apr 11, 9:10 am, "andycr...-AtINh79MtmZWk0Htik3J/w@public.gmane.org" <andyjcr...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 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? > > On Apr 11, 6:27 am, Chris T <ctmailingli...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > andycr...-AtINh79MtmZWk0Htik3J/w@public.gmane.org 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 > > -------------------------------http://autopendium.co.uk > > stuff about old cars--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
andycroll-AtINh79MtmZWk0Htik3J/w@public.gmane.org
2007-Apr-11 15:41 UTC
Re: attachment_fu cropping
Blogged the answer. http://www.deepcalm.com/writing/cropped-thumbnails-in-attachment_fu-using-imagescience On Apr 11, 11:53 am, "andycr...-AtINh79MtmZWk0Htik3J/w@public.gmane.org" <andyjcr...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> I''m getting a ''Bitmap has already been freed'' error, in case that > helps. > > On Apr 11, 9:10 am, "andycr...-AtINh79MtmZWk0Htik3J/w@public.gmane.org" > > <andyjcr...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > 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? > > > On Apr 11, 6:27 am, Chris T <ctmailingli...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > andycr...-AtINh79MtmZWk0Htik3J/w@public.gmane.org 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 > > > -------------------------------http://autopendium.co.uk > > > stuff about old cars--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
>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) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 ------------------------------- http://autopendium.co.uk stuff about old cars -- ---------------------------- http://www.autopendium.co.uk Stuff about old cars --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---