DigitalBlueprint Blog

Resizing Animated GIFs with acts_as_attachment

If you’ve ever tried uploading an animated gif using Techno Weenie’s superb acts_as_attachment plugin you will have noticed that it only ever picks up the first frame!

This is actually due to the way that it’s interfacing with RMagick and is a relatively simple fix. Two files need to be updated, the first is in class_methods.rb:

# Yields a block containing an RMagick Image for the given binary data.
def with_image(data, &block)
  begin
    imgs = Magick::ImageList.new
    binary_data = data.is_a?(Magick::ImageList) ? data : imgs.from_blob(data) unless !Object.const_defined?(:Magick)
  rescue
    # Log the failure to load the image.  This should match ::Magick::ImageMagickError
    # but that would cause acts_as_attachment to require rmagick.
    logger.debug("Exception working with image: #{$!}")
    binary_data = nil
  end
  block.call binary_data if block && binary_data
ensure
  !binary_data.nil?
end

The changes here are very simple, we’re simply swapping out Magick::Image for Magick::ImageList as ImageMagick deals with animated gif’s as multiple images rather than a single file.

The next file that needs modifying is instance_methods.rb:

# Performs the actual resizing operation for a thumbnail
def thumbnail_for_image(img, size)
  size = size.first if size.is_a?(Array) && size.length == 1 && !size.first.is_a?(Fixnum)
  if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
    size = [size, size] if size.is_a?(Fixnum)
    img.each { |i| i.thumbnail(size.first, size[1]) }
  else
    img.each { |i| i.change_geometry!(size.to_s) { |cols, rows, image| image.resize!(cols, rows) } }
  end
end

These changes are necessary as we are now dealing with an ImageList rather than an Image instance. Again the modifications are very simple, all that’s happening here is that instead of applying image modifications to just one image we loop through all the images in the ImageList and apply the modifications to each one – this is especially important for animated GIF’s as they won’t save properly unless all frames are the same dimensions.

Hopefully someone will find this useful, if you run into any problems or have suggestions on a better way to do this please leave a comment!

Posted by Kevin Ansfield on Jul 19, 2007

Vi Arrow Keys in Ubuntu Edgy

Ever since upgrading to Ubuntu Edgy back around the late beta days I’ve had to ditch vi as my usual console editor of choice due to the arrow keys outputting characters such as “A” “B” “C” or “D” on a newline with each key-press. Well, no more! I’ve found a remarkably simple fix…

sudo aptitude install vim-full

As well as vi now supporting the “xterm” term type that fixes the arrow keys problem, it is also now possible to add indenting and syntax highlighting for many filetypes. To do this add the following to your .vimrc file (located in your user directory, create it if it doesn’t already exist)...

filetype plugin on
filetype indent on
syntax on

The reason for vi misbehaving in the first place is due to Ubuntu installing vim-tiny as default – although this package is much smaller it does lack a lot of features such as those mentioned above. This fix was so easy I’m now annoyed at myself that I didn’t try to find a solution sooner!

Note: You can also use apt-get in place of aptitude if you prefer

Posted by Kevin Ansfield on Nov 23, 2006

Live side-by-side CSS editing for FF and IE

Do you love the live css editing feature that the Web Developer extension adds to Firefox?

Well, a new tool has arrived for those of us who spend hours cutting css and trying to kick IE until it behaves itself. CSSVista is a truly fabulous bit of software that allows you to edit your css files and see live updates in both Firefox and Internet Explorer at the same time, it’s so useful I’m suprised no one has come up with the idea before!

Be warned though – it’s still beta and there are bugs in the system! However I do suggest you check it out, it’ll save hours of tedious refreshing and window shuffling.

For all those budding CSS ninjas, another useful service I’ve recently come across is Browsershots.org – it lets you test your page in 9 different browsers for free! Extremely useful, especially as it gives you access to the entire length of the page rather than an above-the-fold view.

Posted by Kevin Ansfield on Oct 27, 2006

Inside Facebook

Karel Baloun has just released a fantastic book entitled “Inside Facebook”. It covers his early experiences in the booming startup and touches on many ideas that make social networks valuable. For a good idea of the joys of working in a young and thriving, modern company with a thoroughly thought-provoking and inspirational read I suggest you check it out – http://www.fbbook.com

If you’re quick you may also be able to get access to the free HTML version that he’s put online available here

Posted by Kevin Ansfield on Oct 26, 2006

Ruby, Gems and RMagick on Ubuntu Edgy

I’ve seen a lot of questions on the ubuntu forums and elsewhere about getting rails, gems and rmagick up and running on Ubuntu. I’ve just performed a fresh install of Ubuntu Edgy Eft and here’s how I got everything installed (I’ll assume you have already set up the additional apt repositories):

# Make sure everything is up to date
sudo apt-get update
sudo apt-get dist-upgrade

# Install ruby
sudo apt-get install ruby ri rdoc libmysql-ruby

# Install gems
wget http://rubyforge.org/frs/download.php/11289/rubygems-0.9.0.tgz
tar zxvf rubygems-0.9.0.tgz
cd rubygems-0.9.0
sudo ruby setup.rb

# Intstall rails
sudo gem install rails --include-dependencies

# Install imagemagick - say yes to all of the additional dependencies
sudo apt-get install libmagick9-dev

# Install rmagick
sudo gem install rmagick

After following these steps everything was running perfectly.

It’s important to note that if you do follow this example then you will need to stick entirely to using gem to manage your ruby/rails install rather than the Debian/Ubuntu packages. This is due to some philosophical issues between the debian package management system and the gem package management system and their differences in file locations.

Let me know how it turns out for you.

Posted by Kevin Ansfield on Oct 26, 2006