Class: ImageStamping::ImageStamper

Inherits:
Object
  • Object
show all
Defined in:
lib/image_stamping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeImageStamper

Returns a new instance of ImageStamper.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/image_stamping.rb', line 8

def initialize()
  @input_file = ''
  @output_file = ''

  @font = 'Arial'
  @font_size = 32
  @font_color = 'white'
  @font_bold = false
  @font_italic = false
  @gravity_value = Magick::SouthEastGravity
end

Instance Attribute Details

#fontObject

Returns the value of attribute font.



6
7
8
# File 'lib/image_stamping.rb', line 6

def font
  @font
end

#font_boldObject

Returns the value of attribute font_bold.



6
7
8
# File 'lib/image_stamping.rb', line 6

def font_bold
  @font_bold
end

#font_colorObject

Returns the value of attribute font_color.



6
7
8
# File 'lib/image_stamping.rb', line 6

def font_color
  @font_color
end

#font_italicObject

Returns the value of attribute font_italic.



6
7
8
# File 'lib/image_stamping.rb', line 6

def font_italic
  @font_italic
end

#font_sizeObject

Returns the value of attribute font_size.



6
7
8
# File 'lib/image_stamping.rb', line 6

def font_size
  @font_size
end

#input_fileObject

Returns the value of attribute input_file.



6
7
8
# File 'lib/image_stamping.rb', line 6

def input_file
  @input_file
end

#output_fileObject

Returns the value of attribute output_file.



6
7
8
# File 'lib/image_stamping.rb', line 6

def output_file
  @output_file
end

Instance Method Details

#gravity(value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/image_stamping.rb', line 43

def gravity(value)
  case value
    when :northeast
      @gravity_value = Magick::NorthEastGravity
    when :northwest
      @gravity_value = Magick::NorthWestGravity
    when :southeast
      @gravity_value = Magick::SouthEastGravity
    when :southwest
      @gravity_value = Magick::SouthWestGravity
    else
      @gravity_value = Magick::SouthEastGravity
  end
end

#stamp(text, width = 0, height = 0, x = 30, y = 30) ⇒ Object

Raises:

  • (RuntimeError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/image_stamping.rb', line 20

def stamp(text, width = 0, height = 0, x = 30, y = 30)
  raise RuntimeError, "The input file does not exist" if @input_file.empty? || !file_exists?(@input_file)
  raise RuntimeError, "The output file has not been given" if @output_file.empty?

  begin
    img = Magick::Image.read(@input_file).first
    drawing = Magick::Draw.new
    drawing.font = @font
    drawing.pointsize = @font_size
    drawing.font_weight = Magick::BoldWeight if @font_bold
    drawing.font_style = Magick::ItalicStyle if @font_italic
    drawing.fill = @font_color

    drawing.gravity = @gravity_value
    drawing.annotate(img, width, height, x, y, text)
    img.write(@output_file)
  rescue Exception => e
    raise RuntimeError, e.message
  end

  true
end