Class: MemeGenerator

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

Defined Under Namespace

Classes: Campfire, GroupMe

Constant Summary collapse

VERSION =
"1.0.9"

Class Method Summary collapse

Class Method Details

.generate(path, top, bottom) ⇒ Object



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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/meme_generator.rb', line 28

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 = File.join(File.dirname(__FILE__), "..", "fonts", "Impact.ttf")
  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)
  output_path
end

.meme_pathsHash

Returns a hash of of available memes. Hash keys are the short names for the memes and the values are paths to the meme image on disk.

Returns:

  • (Hash)

    Returns a hash of of available memes. Hash keys are the short names for the memes and the values are paths to the meme image on disk.



18
19
20
21
22
23
24
25
26
# File 'lib/meme_generator.rb', line 18

def meme_paths
  local_image_path = File.expand_path("~/.memegen")
  base = File.join(File.dirname(__FILE__), "..", "generators")
  files = Dir.glob(["#{base}/*", "#{local_image_path}/*.*"])
  files.inject({}) do |images,path|
    name = path.split('/').last.sub(/\.jpg$/, '')
    images.merge(name => path)
  end
end

.memesArray<String>

Returns a list of short names for memes available locally on disk.

Returns:

  • (Array<String>)

    Returns a list of short names for memes available locally on disk.



11
12
13
# File 'lib/meme_generator.rb', line 11

def memes
  meme_paths.keys
end