Class | Fleximage::Operator::ImageOverlay |
In: |
lib/fleximage/operator/image_overlay.rb
|
Parent: | Operator::Base |
Adds an overlay to the base image. It‘s useful for things like attaching a logo, watermark, or even a border to the image. It will work best with a 24-bit PNG with alpha channel since it will properly deal with partial transparency.
image.resize(image_overlay_path, options = {})
image_overlay_path is the path, relative to RAILS_ROOT where the image you want superimposed can be found.
Use the following keys in the options hash:
Example:
@photo.operate do |image| image.image_overlay('images/my_logo_with_alpha.png', :size => '25x25', :alignment => :top_right, :blending => :screen ) end
# File lib/fleximage/operator/image_overlay.rb, line 52 52: def operate(image_overlay_path, options = {}) 53: options = options.symbolize_keys 54: 55: #load overlay 56: overlay = Magick::Image.read(image_overlay_path).first 57: 58: #resize overlay 59: if options[:size] 60: if options[:size] == :scale_to_fit || options[:size] == :stretch_to_fit 61: x, y = @image.columns, @image.rows 62: else 63: x, y = size_to_xy(options[:size]) 64: end 65: 66: method = options[:size] == :stretch_to_fit ? :stretch : :scale 67: send(method, [x, y], overlay) 68: end 69: 70: #prepare arguments for composite! 71: args = [] 72: args << overlay #overlay image 73: args << GRAVITIES[options[:alignment] || :center] #gravity 74: args += size_to_xy(options[:offset]) if options[:offset] #offset 75: args << symbol_to_blending_mode(options[:blending] || :over) #compositing mode 76: 77: #composite 78: @image.composite!(*args) 79: 80: return @image 81: end