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:

  • offset: distance of the dropsahdow form the image, in FlexImage size format. Positive number move it down and right, negative numbers move it up and left.
  • blur: how blurry the shadow is. Roughly corresponds to distance in pixels of the blur.
  • background: a color for the background of the image. What the shadow fades into. Use an RMagick named color or use the color method in FlexImage::Controller, or a Magick::Pixel object.
  • color: color of the shadow itself. Use an RMagick named color or use the color method in FlexImage::Controller, or a Magick::Pixel object.
  • opacity: opacity of the shadow. A value between 0.0 and 1.0, where 1 is opaque and 0 is transparent.

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

Methods

operate  

Public Instance methods

[Source]

    # 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

[Validate]