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.
Post a Comment