Class: MemeGenerator

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

Constant Summary collapse

VERSION =
"0.0.7"
IMPACT_PATH =

If you don’t have OS X, fork me :)

"fonts/Impact.ttf"

Class Method Summary collapse

Class Method Details

.generate(path, top, bottom) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/meme_generator.rb', line 10

def generate(path, top, bottom)
  top = top.upcase
  bottom = bottom.upcase

  canvas = Magick::ImageList.new(path)
  image = canvas.first

  draw = Magick::Draw.new
  draw.font = IMPACT_PATH if File.exists?(IMPACT_PATH)
  draw.font_weight = Magick::BoldWeight

  pointsize = image.columns / 5.0
  stroke_width = pointsize / 30.0
  x_position = image.columns / 2
  y_position = image.rows * 0.15

  # Draw top
  unless top.empty?
    scale, text = scale_text(top)
    bottom_draw = draw.dup
    bottom_draw.annotate(canvas, 0, 0, 0, 0, text) do
      self.interline_spacing = -(pointsize / 5)
      self.stroke_antialias(true)
      self.stroke = "black"
      self.fill = "white"
      self.gravity = Magick::NorthGravity
      self.stroke_width = stroke_width * scale
      self.pointsize = pointsize * scale
    end
  end

  # Draw bottom
  unless bottom.empty?
    scale, text = scale_text(bottom)
    bottom_draw = draw.dup
    bottom_draw.annotate(canvas, 0, 0, 0, 0, text) do
      self.interline_spacing = -(pointsize / 5)
      self.stroke_antialias(true)
      self.stroke = "black"
      self.fill = "white"
      self.gravity = Magick::SouthGravity
      self.stroke_width = stroke_width * scale
      self.pointsize = pointsize * scale
    end
  end

  output_path = "/tmp/meme-#{Time.now.to_i}.jpeg"
  canvas.write(output_path)
  puts output_path
  exit 0
end