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
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!
JPEG's don't work? seems to work for me.
Are you being serious about the transparency? Again it works for me.
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!!!
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 ;)
hey thanks - this helped me out.
Post a Comment