# Attachment\_fu - watermark tmp file before saved to S3

**URL:** <https://www.ruby-forum.com/t/attachment-fu-watermark-tmp-file-before-saved-to-s3/172612>\
**Category:** Rails\
**Created:** [July 24, 2009, 3:21am UTC](https://www.ruby-forum.com/t/attachment-fu-watermark-tmp-file-before-saved-to-s3/172612 "2009-07-24T03:21:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [July 24, 2009, 3:21am UTC](https://www.ruby-forum.com/t/attachment-fu-watermark-tmp-file-before-saved-to-s3/172612/1 "2009-07-24T03:21:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [July 24, 2009, 4:54am UTC](https://www.ruby-forum.com/t/attachment-fu-watermark-tmp-file-before-saved-to-s3/172612/2 "2009-07-24T04:54:18Z")

</div>

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
