# Attachment\_fu, Watermarks and after\_save

**URL:** <https://www.ruby-forum.com/t/attachment-fu-watermarks-and-after-save/130747>\
**Category:** Rails\
**Created:** [March 3, 2008, 12:26am UTC](https://www.ruby-forum.com/t/attachment-fu-watermarks-and-after-save/130747 "2008-03-03T00:26:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [March 3, 2008, 12:26am UTC](https://www.ruby-forum.com/t/attachment-fu-watermarks-and-after-save/130747/1 "2008-03-03T00:26:55Z")

</div>

Hi,  
I’m having an issue with trying to watermark an uploaded image using  
attachment\_fu and Rails 2.0.2.

The model looks like…

class Picture \< ActiveRecord::Base  
belongs\_to :gallery  
after\_save :watermark\_image

has\_attachment :content\_type =\> :image,  
:storage =\> :file\_system,  
:max\_size =\> 2.megabytes,  
:resize\_to =\> ‘500x500’,  
:processor =\> “Rmagick”,  
:thumbnails =\> {  
:large =\> ‘128x128\>’,  
:small =\> ‘64x64\>’  
}  
validates\_as\_attachment

def watermark\_image  
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

The code fails with a ‘Cannot read image’ on the ‘dst = …’ line of  
the watermark\_image method.

Looks like the file does not actually exist in the file system until  
after ‘after\_save’?

removing the ‘after\_save’ and all works, except of course no  
watermark…

Anyone have an ideas on how to achieve this?

thanks in advance…

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [March 3, 2008, 5:11am UTC](https://www.ruby-forum.com/t/attachment-fu-watermarks-and-after-save/130747/2 "2008-03-03T05:11:50Z")

</div>

kevin evans wrote:

> Hi,

> Anyone have an ideas on how to achieve this?
> 
> thanks in advance…

Besides public\_filename there are other methods within the  
Attachment\_fu’ modified model that you can get at… I don’t remember off  
the top of my head what it’s name is but you can do the following to  
find out

script/console

> Picture.instance\_methods.grep /path/

One of those methods should get you an absolute path in which your  
after\_save filter should work…

I haven’t done this myself (other than using the alternate path methods)  
and it’s only the path I would take myself…

hth

ilan

---

<div class="post-metadata">

**Author:** ![kevin\_evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/50afbb/32.png) [@kevin\_evans](https://www.ruby-forum.com/u/kevin_evans)\
**Post date:** [March 3, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/attachment-fu-watermarks-and-after-save/130747/3 "2008-03-03T15:42:07Z")

</div>

I have the exact same code as you do, and it works pefectly.

Actually, I also have ‘unless self.thumbnail?’ at the beginning of  
that code block because I was getting my fullsize logo on top of my  
thumbnails as well, but that’s a different story.

I would check your paths to make sure that things are where you expect  
them. ‘after save’ is, well, after the record/file is saved, so I  
would look elsewhere for your problem. When you’re looking at the  
error page, are you 1000% sure that the path shown for the error page  
exists and is valid?
