Attachment_fu - watermark tmp file before saved to S3

Hi there,

I'd like to use RMagick to watermark an image in the tmp directory
before Attachment_fu saves it to Amazon S3.

I took a look at the callbacks available in attachment_fu. There's an
'after_attachment_saved' method but this would be too late, and a
'before_thumbnail_saved' but this is no good because it's for

I think I need 'before_attachment_saved' or the equivalent in the
attachment workflow.

I do have a watermark working for :file_system storage option using
the code below.

# -------------------------------------------- watermark on
file_system after initial save - but doesn't work for s3
  def after_attachment_saved
        unless self.thumbnail?
          dst ="#{RAILS_ROOT}/public/#
          src ="#{RAILS_ROOT}/config/
          result = dst.composite(src, Magick::SouthEastGravity,

Now I'm looking for a way to hook into the attachment_fu process and
apply the watermark before the main image is saved to :s3

Or perhaps I'm approaching this in the wrong way? I'm reluctant to
start modifying the 'write_to_temp_file' method in the attachment_fu

Any help or advice anyone has would be much appreciated.

Many thanks,

I just got this working..

I added my own callback method 'before_attachment_save' to
attachment_fu.rb . I hook into this in my attachment class and now
have a great place to do my watermarking directly on the tmp file
before it's sent off to S3.

If anyone is interested:

# -------- In attachment_fu.rb modification

def after_process_attachment
  if @saved_attachment
    if respond_to?(:process_attachment_with_processing) &&
thumbnailable? && !attachment_options[:thumbnails].blank? &&
      temp_file = temp_path || create_temp_file
      attachment_options[:thumbnails].each { |suffix, size|
create_or_update_thumbnail(temp_file, suffix, *size) }

    # ---- new added callback
    callback :before_attachment_saved
    @saved_attachment = nil
    callback :after_attachment_saved

# --------- image_attachment.rb

def before_attachment_saved
  unless self.thumbnail?
    dst =
    src ="#{RAILS_ROOT}/config/logo.png").first
    result = dst.composite(src, Magick::SouthEastGravity,

There are some other bits and pieces you have to add in
attachment_fu.rb to register the new callback method as well, but I
copied this from the callbacks already there.

Just tested this on my host (heroku) and it's watermarking happily and
storing on S3 :slight_smile: