attachment_fu snippets

Installing ImageMagick, mini-magick and rmagick on Mac OS X Leopard

Tagged imagemagick, attachment_fu, rails, ruby, mac, osx, leopard, mini-magick, rmagick  Languages ruby

I had no success installing ImageMagick and mini-magick with the instructions I found on this page but after some googling I found this blog post, which had the magic commands that worked for me:

sudo port install tiff -macosx  #disables the linkage with Apple's open gl
sudo port install ImageMagick

sudo gem install rmagick
sudo gem install mini_magick

To test mini-magick, open an irb console and paste in the following code:

require 'rubygems'
require 'mini_magick'

path = "public/images/0000/0003/logo.jpg"
image =

#print width and height
puts image[:width]
puts image[:height]

How to add a watermark to images using MiniMagick, attachment_fu and Ruby

Tagged attachment_fu, mini_magick, ruby, watermark  Languages ruby

Use this snippet to add a watermark to an image, after the image is uploaded:

class Image
  has_attachment ...
  after_attachment_saved do |record|    
    # Don't add watermarks to thumbnails
    if record.thumbnail.nil?
      full_path = File.join(RAILS_ROOT, 'public/', record.public_filename)
      img = MiniMagick::Image.from_file(full_path)
      width = img[:width]
      height = img[:height]
      if width > 150 && height > 150
        img.combine_options do |c|
          c.gravity 'SouthWest'
          # This is RAILS_ROOT/images/watermark.gif
          c.draw "image Over 0,0 0,0 \"images/watermark.gif\""


Note that after_attachment_saved is a callback added by attachment_fu, use after_save if you're not using attachment_fu.