Class | Fleximage::Operator::Shadow |
In: |
lib/fleximage/operator/shadow.rb
|
Parent: | Operator::Base |
Add a drop shadow to the image.
image.shadow(options = {})
Use the following keys in the options hash:
Example:
@photo.operate do |image| # Default settings image.shadow( :color => 'black', # or color(0, 0, 0) :background => 'white', # or color(255, 255, 255) :blur => 8, :offset => '2x2', :opacity => 0.75 ) # Huge, red shadow image.shadow( :color => color(255, 0, 0), :background => 'black', # or color(255, 255, 255) :blur => 30, :offset => '20x10', :opacity => 1 ) end
# File lib/fleximage/operator/shadow.rb, line 48 48: def operate(options = {}) 49: options = options.symbolize_keys if options.respond_to?(:symbolize_keys) 50: defaults = { 51: :offset => 2, 52: :blur => 8, 53: :background => 'white', 54: :color => 'black', 55: :opacity => 0.75 56: } 57: options = options.is_a?(Hash) ? defaults.update(options) : defaults 58: 59: # verify options 60: options[:offset] = size_to_xy(options[:offset]) 61: options[:blur] = options[:blur].to_i 62: 63: options[:background] = Magick::Pixel.from_color(options[:background]) unless options[:background].is_a?(Magick::Pixel) 64: options[:color] = Magick::Pixel.from_color(options[:color]) unless options[:color].is_a?(Magick::Pixel) 65: options[:color].opacity = (1 - options[:opacity]) * 255 66: 67: # generate shadow image 68: shadow = @image.dup 69: shadow.background_color = options[:color] 70: shadow.erase! 71: shadow.border!(options[:offset].max + options[:blur] * 3, options[:offset].max + options[:blur] * 3, options[:background]) 72: shadow = shadow.blur_image(0, options[:blur] / 2) 73: 74: # apply shadow 75: @image = shadow.composite( 76: @image, 77: GRAVITIES[:top_left], 78: (shadow.columns - @image.columns) / 2 - options[:offset][0], 79: (shadow.rows - @image.rows) / 2 - options[:offset][1], 80: symbol_to_blending_mode(:over) 81: ) 82: @image.trim! 83: end