# File\_column and image watermarks

**URL:** <https://www.ruby-forum.com/t/file-column-and-image-watermarks/76214>\
**Category:** Rails\
**Created:** [November 16, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/file-column-and-image-watermarks/76214 "2006-11-16T19:36:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jwm](https://www.ruby-forum.com/u/jwm)\
**Post date:** [November 16, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/file-column-and-image-watermarks/76214/1 "2006-11-16T19:36:48Z")

</div>

This code works for watermarking images with file\_column, however it  
currently watermarks the original and not the other versions (though for  
obvious reasons I dont want it on the small sizes). How can I apply this  
watermark code to other versions of my file\_column entries.

Also it should be noted that the watermark\_image function is not mine, I  
found it on a message board but have since lost the link. Big thanks to  
whomever wrote it originally.

Thanks for your help.

Here is my entire Avatar model, theres no sense in trying to keep this a  
secret or anything as Im sure there are many people that want or could  
use this code.

class Avatar \< ActiveRecord::Base

# 

# Associations

# 

belongs\_to :profile

# 

# File Column

# 

file\_column :image,  
:permission =\> “0664”,  
:magick =\> {  
:versions =\> {  
:full =\> { :size =\> ‘400x300\>’},  
:square =\> { :crop =\> ‘1:1’, :size =\> ‘90x90!’ },  
:icon =\> { :crop =\> ‘1:1’, :size =\> ‘60x60!’ }  
}  
}

# 

# Before, During & After

# 

before\_create :watermark\_image

# 

# Validations

# 

validates\_file\_format\_of :image,  
:in =\> [‘jpg’, ‘png’, ‘gif’], :message =\> ‘must be jpg, png or gif’

# 

# Private Methods

# 

private  
def watermark\_image  
#require ‘RMagick’  
dst = Magick::Image.read(self.image).first  
src =  
Magick::Image.read("#{RAILS\_ROOT}/public/images/watermark.png").first

```
result = dst.composite(src, Magick::CenterGravity, 

```

Magick::OverCompositeOp)  
result.write(self.image)  
end

end

---

<div class="post-metadata">

**Author:** ![jwm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jwm](https://www.ruby-forum.com/u/jwm)\
**Post date:** [November 16, 2006, 7:59pm UTC](https://www.ruby-forum.com/t/file-column-and-image-watermarks/76214/2 "2006-11-16T19:59:12Z")

</div>

That looks like my code. My solution for your problem is simple: Upload  
the  
image twice 🙂 One gets put in the public folder, another one gets put  
in a  
private folder.

This was originally going to be in a book I was going to write, but I  
don’t  
have time any more.

def create  
@photo = Photo.new params[:photo]  
@photo.public = params[:photo][:filename]  
if @photo.save  
flash[:notice] = “The photo was saved!”  
redirect\_to :action =\> “list”  
else  
render :action=\>“new”  
end  
end

What I do here is make the user upload the photo once into a filed  
called  
filename. Then I use the controller to populate the public field as  
well.  
This way the user doesn’t need to upload the photo twice. I’d like to be  
able to do this in the model but there’s not yet a 100% working solution  
for  
that yet. I just throw an exception if they didn’t do that 🙂

Upon creation, the public picture gets the watermark applied.

class Photo \< ActiveRecord::Base  
file\_column :filename, :store\_dir =\> “photos”, :magick =\> {  
:versions =\> {“medium” =\> “640x480\>”, “large” =\> “1024x768” }  
}

```
file_column :public, :magick => {
  :versions => { "thumb" => "50x50", "large" => "320x240>" }
  }

validates_presence_of :filename, :description
before_create :watermark_image

private
def watermark_image
    raise "Ensure that you have set the public field for the photo 

```

in  
the controller. Usually you want to set @photo.public =  
params[‘photo’][‘filename’]" if self.public == nil  
require ‘RMagick’  
dst = Magick::Image.read(self.public).first  
src =  
Magick::Image.read("#{RAILS\_ROOT}/config/sample.png").first  
result = dst.composite(src, Magick::CenterGravity,  
Magick::OverCompositeOp)  
result.write(self.public)

```
end

```

end

Good luck with that!

---

<div class="post-metadata">

**Author:** ![jwm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jwm](https://www.ruby-forum.com/u/jwm)\
**Post date:** [November 17, 2006, 5:23am UTC](https://www.ruby-forum.com/t/file-column-and-image-watermarks/76214/3 "2006-11-17T05:23:05Z")

</div>

I did it by doing this

after\_save :watermark\_on\_image

private  
def watermark\_image  
#original photo  
dst = Magick::Image.read(self.image).first  
src =  
Magick::Image.read("#{RAILS\_ROOT}/public/images/watermark.jpg").first  
result = dst.composite(src, Magick::SouthEastGravity,  
Magick::OverCompositeOp)  
result.write(self.image)  
#medium  
medium =  
“#{RAILS\_ROOT}/public/photo/image/#{self.id}/medium/#{File.basename(self.image)}”  
dst = Magick::Image.read(medium).first  
src =  
Magick::Image.read("#{RAILS\_ROOT}/public/images/watermadrk.jpg").first  
result = dst.composite(src, Magick::SouthEastGravity,  
Magick::OverCompositeOp)  
result.write(medium)

```
end
```
