Class: Fleximage::Operator::Shadow
- Defined in:
- lib/fleximage/operator/shadow.rb
Overview
Add a drop shadow to the image.
image.shadow( = {})
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 thecolor
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
Instance Method Summary collapse
Methods inherited from Base
#color, color, #execute, #initialize, #scale, #scale_and_crop, size_to_xy, #size_to_xy, #stretch, #symbol_to_blending_mode, #symbol_to_gravity
Constructor Details
This class inherits a constructor from Fleximage::Operator::Base
Instance Method Details
#operate(options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/fleximage/operator/shadow.rb', line 48 def operate( = {}) = .symbolize_keys if .respond_to?(:symbolize_keys) defaults = { :offset => 2, :blur => 8, :background => 'white', :color => 'black', :opacity => 0.75 } = .is_a?(Hash) ? defaults.update() : defaults # verify options [:offset] = size_to_xy([:offset]) [:blur] = [:blur].to_i [:background] = Magick::Pixel.from_color([:background]) unless [:background].is_a?(Magick::Pixel) [:color] = Magick::Pixel.from_color([:color]) unless [:color].is_a?(Magick::Pixel) [:color].opacity = (1 - [:opacity]) * 255 # generate shadow image shadow = @image.dup shadow.background_color = [:color] shadow.erase! shadow.border!([:offset].max + [:blur] * 3, [:offset].max + [:blur] * 3, [:background]) shadow = shadow.blur_image(0, [:blur] / 2) # apply shadow @image = shadow.composite( @image, symbol_to_gravity(:top_left), (shadow.columns - @image.columns) / 2 - [:offset][0], (shadow.rows - @image.rows) / 2 - [:offset][1], symbol_to_blending_mode(:over) ) @image.trim! end |