class Image < ActiveRecord::Base
require 'RMagick'
has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 4.megabytes,
:resize_to => '640x400>',
:thumbnails => { :thumb => '100x100>',:medium => '250x150' }
validates_as_attachment
def watermark_image
dst = Magick::Image.read("#{RAILS_ROOT}/public/#{self.public_filename}").first
src = Magick::Image.read("#{RAILS_ROOT}/public/images/logo.gif").first
result = dst.composite(src, Magick::CenterGravity, Magick::HardLightCompositeOp)
result.write("#{RAILS_ROOT}/public/#{self.public_filename}")
end
Hi!! but how to call "watermark_image" method? Is it called automatically when you upload pic.?
ReplyDeleteYou can call it after your picture save. i.e. You can call it by callback method 'after_save' in model.
ReplyDeleteSorry! i am new for ruby on rails. I understood your code. But, if i want to upload a pic, watermark it and save to database, how is my code gonna be? i am confused with logo.gif.
ReplyDeleteThank you! worked like a charm
ReplyDelete