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? &&
parent_id.nil?
      temp_file = temp_path || create_temp_file
      attachment_options[:thumbnails].each { |suffix, size|
create_or_update_thumbnail(temp_file, suffix, *size) }
    end

    # ---- new added callback
    callback :before_attachment_saved
    save_to_storage
    @temp_paths.clear
    @saved_attachment = nil
    callback :after_attachment_saved
  end
end

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

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

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 :)

Cheers,
Will

On Jul 23, 6:21 pm, Will <[email protected]> wrote:
> 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
> thumbnails.
>
> 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 = Magick::Image.read("#{RAILS_ROOT}/public/#
> {self.public_filename}").first
>           src = Magick::Image.read("#{RAILS_ROOT}/config/
> logo.png").first
>           result = dst.composite(src, Magick::SouthEastGravity,
> Magick::OverCompositeOp)
>           result.write("#{RAILS_ROOT}/public/#{self.public_filename}")
>         end
>     end
>
> 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
> plugin.
>
> Any help or advice anyone has would be much appreciated.
>
> Many thanks,
> Will
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to