# File\_column, RMagick, and watermarking

**URL:** <https://www.ruby-forum.com/t/file-column-rmagick-and-watermarking/74941>\
**Category:** Rails\
**Created:** [February 28, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/file-column-rmagick-and-watermarking/74941 "2006-02-28T20:57:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [February 28, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/file-column-rmagick-and-watermarking/74941/1 "2006-02-28T20:57:20Z")

</div>

I’m working on a photo gallery and I’m using File\_column to handle the  
uploads of the files and create my thumbnails, but does anyone here have  
any idea how to have a watermark (opaque text maybe) placed over one of  
the versions? Any ideas on how to go about this would be great. Even if  
it’s just straight RMagick code, that would be fine too.

Thanks in advance for the help!

Brian H.  
Web D.  
Learning & Technology Services  
Schofield 3-B  
University of Wisconsin-Eau Claire  
715 836 3585  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [October 26, 2006, 8:51am UTC](https://www.ruby-forum.com/t/file-column-rmagick-and-watermarking/74941/2 "2006-10-26T08:51:31Z")

</div>

I have a similar requirement; did you find any solution for this?

thanks

Hogan, Brian P. wrote:

> I’m working on a photo gallery and I’m using File\_column to handle the  
> uploads of the files and create my thumbnails, but does anyone here have  
> any idea how to have a watermark (opaque text maybe) placed over one of  
> the versions? Any ideas on how to go about this would be great. Even if  
> it’s just straight RMagick code, that would be fine too.
> 
> Thanks in advance for the help!
> 
> Brian H.  
> Web D.  
> Learning & Technology Services  
> Schofield 3-B  
> University of Wisconsin-Eau Claire  
> 715 836 3585  
> removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [October 26, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/file-column-rmagick-and-watermarking/74941/3 "2006-10-26T21:36:40Z")

</div>

Hogan, Brian P. wrote:

> I’m working on a photo gallery and I’m using File\_column to handle the  
> uploads of the files and create my thumbnails, but does anyone here have  
> any idea how to have a watermark (opaque text maybe) placed over one of  
> the versions? Any ideas on how to go about this would be great. Even if  
> it’s just straight RMagick code, that would be fine too.
> 
> Thanks in advance for the help!

There are many ways to write text on an image with RMagick. The simplest  
way is to use the Draw#text method:  
[RMagick 1.15.0: class Draw](http://www.simplesystems.org/RMagick/doc/draw.html#text). Also check out  
the example for the Image#watermark method:  
[RMagick 1.15.0: class Image (instance methods, part 3)](http://www.simplesystems.org/RMagick/doc/image3.html#watermark) and the  
example for the Image#mask attribute:  
[RMagick 1.15.0: class Image (attribute methods)](http://www.simplesystems.org/RMagick/doc/imageattrs.html#mask). These  
last two require that you have RMagick 1.14.0 or later.

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [October 26, 2006, 10:24pm UTC](https://www.ruby-forum.com/t/file-column-rmagick-and-watermarking/74941/4 "2006-10-26T22:24:47Z")

</div>

FlexImage seems great, but i would prefer to story my images on disk  
rather than in the DB… thats not possible with FlexImage, is it?

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [October 26, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/file-column-rmagick-and-watermarking/74941/5 "2006-10-26T21:50:07Z")

</div>

Hogan, Brian P. wrote:

> I’m working on a photo gallery and I’m using File\_column to handle the  
> uploads of the files and create my thumbnails, but does anyone here have  
> any idea how to have a watermark (opaque text maybe) placed over one of  
> the versions? Any ideas on how to go about this would be great. Even if  
> it’s just straight RMagick code, that would be fine too.
> 
> Thanks in advance for the help!
> 
> Brian H.  
> Web D.  
> Learning & Technology Services  
> Schofield 3-B  
> University of Wisconsin-Eau Claire  
> 715 836 3585  
> removed\_email\_address@domain.invalid

I’ve just released an updated to the FlexImage that makes it super easy  
to do just that. Simply provide a PNG and give it some basic info on  
how to size and place it.

Check it out.  
[http://beautifulpixel.com/articles/2006/10/26/flex-image-now-has-special-effects](http://beautifulpixel.com/articles/2006/10/26/flex-image-now-has-special-effects)

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [October 26, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/file-column-rmagick-and-watermarking/74941/6 "2006-10-26T23:11:46Z")

</div>

Thorsten L wrote:

> FlexImage seems great, but i would prefer to story my images on disk  
> rather than in the DB… thats not possible with FlexImage, is it?

Not currently, sorry ☹

At the very least you can take a look into the source and see the  
rmagick commands that do the overlay. Check out  
FlexImage::Model#overlay! for the implementation.

---

<div class="post-metadata">

**Author:** ![HoganS\_Brian\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/9de0a6/32.png) [@HoganS\_Brian\_P](https://www.ruby-forum.com/u/HoganS_Brian_P)\
**Post date:** [October 26, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/file-column-rmagick-and-watermarking/74941/7 "2006-10-26T23:24:56Z")

</div>

I totally forgot I posted this. I figured it out with RMagick a long  
time  
ago… it’s pretty simple actually

This places “sample” diagonally across the middle of the image. You  
needa  
graphic called ‘sample.png’ that contains some alpha transparency but  
that’s  
easy to create.

class Photo \< ActiveRecord::Base

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

validates_presence_of :filename, :description
before_create :watermark_image

private
def watermark_image
    require 'RMagick' # probably don't need this line but it's here

```

anyway  
dst = Magick::Image.read(self.filename).first  
src =  
Magick::Image.read("#{RAILS\_ROOT}/config/sample.png").first  
result = dst.composite(src, Magick::CenterGravity,  
Magick::OverCompositeOp)  
result.write(self.filename)

```
end

```

end
