Class: Fleximage::Operator::Text
- Defined in:
- lib/fleximage/operator/text.rb
Overview
Draw text on the image. Customize size, position, color, dropshadow, and font.
image.text(string_to_write, = {})
Use the following keys in the options
hash:
-
alignment: symbol like in
ImageOverlay
-
offset: size string
-
antialias: true or false
-
color: string or
color(r, g, b)
-
font_size: integer
-
font: path to a font file relative to
Rails.root
-
rotate: degrees as an integer
-
shadow:
{:blur => 1, :opacity => 1.0}
-
font_weight: RMagick font weight constant or value. See: www.imagemagick.org/RMagick/doc/draw.html#font_weight
-
stroke: hash that, if present, will stroke the text. The hash should have both
:width
(integer) and:color
(string or color object).
Example:
@photo.operate do |image|
image.text('I like Cheese',
:alignment => :top_left,
:offset => '300x150',
:antialias => true,
:color => 'pink',
:font_size => 24,
:font => 'path/to/myfont.ttf',
:rotate => -15,
:shadow => {
:blur => 1,
:opacity => 0.5,
},
:stroke => {
:width => 3,
:color => color(0, 0, 0),
}
)
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(string_to_write, options = {}) ⇒ Object
43 44 45 46 47 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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/fleximage/operator/text.rb', line 43 def operate(string_to_write, = {}) = { :alignment => :top_left, :offset => '0x0', :antialias => true, :color => 'black', :font_size => '12', :font => nil, :text_align => :left, :rotate => 0, :shadow => nil, :stroke => { :width => 0, :color => 'white', } }.merge() [:offset] = size_to_xy([:offset]) # prepare drawing surface text = Magick::Draw.new text.gravity = symbol_to_gravity([:alignment]) text.fill = [:color] text.text_antialias = [:antialias] text.pointsize = [:font_size].to_i text.rotation = [:rotate] text.font_weight = [:font_weight] if [:font_weight] if [:stroke][:width] > 0 text.stroke_width = [:stroke][:width] text.stroke = [:stroke][:color] end # assign font path with to rails root unless the path is absolute if [:font] font = [:font] font = "#{Rails.root}/#{font}" unless font =~ %r{^(~?|[A-Za-z]:)/} text.font = font end # draw text on transparent image temp_image = Magick::Image.new(@image.columns, @image.rows) { self.background_color = 'none' } temp_image = temp_image.annotate(text, 0, 0, [:offset][0], [:offset][1], string_to_write) # add drop shadow to text image if [:shadow] shadow_args = [2, 2, 1, 1] if [:shadow].is_a?(Hash) #shadow_args[0], shadow_args[1] = size_to_xy(options[:shadow][:offset]) if options[:shadow][:offset] shadow_args[2] = [:shadow][:blur] if [:shadow][:blur] shadow_args[3] = [:shadow][:opacity] if [:shadow][:opacity] end shadow = temp_image.shadow(*shadow_args) temp_image = shadow.composite(temp_image, 0, 0, symbol_to_blending_mode(:over)) end # composite text on original image @image.composite!(temp_image, 0, 0, symbol_to_blending_mode(:over)) end |