 View Cart Login        

Garbage Burrito!

An online internet web blog by Ben Kittrell

  • Blog
  • Tweets
  • Photos
  • Contact
  • Favorite Burrito
  • Burrito Shop

 About Ben

BenKittrell.jpgCo-Founder of Doodlekit & LiveOn Coder, Home Renovator, Enjoyer of Refreshing Beverages.

Sometimes I blog here, sometimes on Doodlekit and sometimes I tweet.

My Resume

Fork Me on Github!

 Blog Entries

  • Doodlekit 2.0
  • New Doodlekit Release in 2013
  • Variable Arguments with Callback in CoffeeScript
  • Sequence Diagrams for Asynchronous Flows in Node.js
  • Putting your layouts in a non "layouts" directory in Rails
more  

 Blog Comments

  • Ruby: Slideshow / Clearing All Javascript Timers
  • Ana: Slideshow / Clearing All Javascript Timers
  • Paige: Slideshow / Clearing All Javascript Timers
  • Agnes: Slideshow / Clearing All Javascript Timers
  • Rachael: Slideshow / Clearing All Javascript Timers
more  

 Links


  • website reviewer
  • live on - sharing memories
  • kids website builder
  • resume website
  • martial arts website builder
  • affiliate website builder
  • teacher website

 Doodlekit

Free Website Builder

free website builder

 

 Login

 Forgot Password?
 Back to Blog

Rails: Resizing Animated Gifs with File_Column

Ben Kittrell
 08/17/2006 05:56PM

Ryan kept whining about how he has to have an animated avatar. I finally gave in and figured out how to make this work.

The problem is that I'm using file_column and its rmagick support to keep the avatars small. By default it will just take the first frame of an animated gif and throw the rest away. I did some searching and found a post that said to just use ImageList, instead of Image. Then just iterate through each image and resize them individually. Here's the code...

def transform_with_magick
  if needs_resize?
    begin
      imgs = ::Magick::ImageList::new(absolute_path)
    rescue ::Magick::ImageMagickError
      @magick_errors ||= []
      @magick_errors << "invalid image"
      return
    end
 
    if options[:magick][:versions]
      options[:magick][:versions].each_pair do |version, version_options|
        next if version_options[:lazy]
        dirname = version_options[:name]
        FileUtils.mkdir File.join(@dir, dirname)
        resize_image(imgs, version_options, absolute_path(dirname))
      end
    end
    if options[:magick][:size] or options[:magick][:crop]
      resize_image(imgs, options[:magick], absolute_path)
    end
 
    GC.start
  end
end

def resize_image(imgs, img_options, dest_path)
  begin
    if img_options[:crop]
      img = imgs.first
      dx, dy = img_options[:crop].split(':').map { |x| x.to_f }
      w, h = (img.rows * dx / dy), (img.columns * dy / dx)
      for img in imgs
        img.crop!(::Magick::CenterGravity, [img.columns, w].min,
         [img.rows, h].min)
      end
    end
 
    if img_options[:size]
      for img in imgs
        img.change_geometry!(img_options[:size]) do |c, r, i|
          i.resize!(c, r)
        end
      end
    end
  ensure
    imgs.write dest_path
    File.chmod options[:permissions], dest_path
  end
end

Just paste these methods in over the existing ones in 'magick_file_column.rb'. Hopefully someone will revive the project and we can get stuff like this put into the main plugin.

Go crazy Ryan.

 Comments

 Ryan  08/17/2006 09:22PM 

MORE WHINING!

OMG!!!!! NO!~~~~~~~
PLEASE MAKE THE BAD MAN STOP!!!!!

You can't resize an animated Gif! if there's transperancies involved, it'll just look like a muddy footprint! if you want to allow animated gif avitars, then just make the user responsible for resizing. you coud possibly throw up a validation err. unlike now when it just acts like it uploaded successfully, but not really. additionally, I can't even upload jpg's or anything anymore. your avitar feature is in deperate need of some open minded development. if you refuse.. I'm foing to punish you. Puhahahahahah!

 thabenksta  08/18/2006 01:21AM 

JPEG's don't work? seems to work for me.

Are you being serious about the transparency? Again it works for me.

 rheaghen  08/18/2006 09:32AM 

Look, I don't know ehat rails is doing under the covers, but in my ecperience, resizing is almost always a bad Idea unless your gonna do it properly. I've seen ppl so non-shalontly throw quality to the wind so much that it's left me bitter. I know that resizing Gif's can be less than preferable. If you really need to resize, add some guidelines for maitaining integrity. list the Maximun size, supported filetypes and file sizes. if your gonna modify peoples shit, you really need to tell the. It always infuriates me when I load an image on a site and they compress it, reduce it, reformat it, with out notification. myabe I'm more advanced than the average user, but still, I'm not asking for world peace here, just a little help. (grumbling... arrogent incosiderate little shit heade thenk they know everything... OMG!)

Ryan's Emotional OutBurst! MAKE IT EASY FOR ME TO MANAGE MY GRAPHICS!!!

 thabenksta  08/18/2006 10:31AM 

I understand, and if I was writing forum software for general use, then I'd probably provide a way to upload unadulterated images. Considering my market I think it's worth it to let them upload any size picture. Most people don't even know how to resize a photo, and even adding a checkbox that says "[x] Do not resize" would confuse the crap out of them.

Sorry Ryan, you don't fit into my demographic ;)

 john  09/27/2006 09:14PM 

hey thanks - this helped me out.

 Post a Comment



 Search

Follow On...

  • Facebook
  • Twitter
  • LinkedIn
  • Google+
  • Pinterest
  • RSS Feeds
  • Blog Posts,
  • Blog Comments,
  • Gallery Images,
  • Forum Topics

Copyright © 2006 Garbage Burrito. All Rights Reserved.
hosted by doodlekit™- free website builder

 

 powered by Doodlekit™ Free Website Builder