file_column and image watermarks

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!