File_column and image watermarks

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

That looks like my code. My solution for your problem is simple: Upload
the
image twice :slight_smile: 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 :slight_smile:

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!

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs