Class: LeMeme

Inherits:
Object
  • Object
show all
Defined in:
lib/le_meme.rb,
lib/le_meme/version.rb

Overview

Create some dank memes

Author:

Constant Summary collapse

VALID_IMAGE_EXTENSIONS =
/\.(jp[e]?g|png|gif)$/i
VERSION =
"0.0.5"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*dirs) ⇒ String

Create a new instance of LeMeme, for generating memes.

Parameters:

  • Directories (Array)

    of images you want the gem to know about. Scrapes images in said directory



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

def initialize(*dirs)
  @memes = {}
  load_directory!
  dirs.each do |dir|
    load_directory!(dir)
  end
end

Instance Attribute Details

#memesObject (readonly)

Returns the value of attribute memes.



12
13
14
# File 'lib/le_meme.rb', line 12

def memes
  @memes
end

Instance Method Details

#fast_meme(name: nil, top: nil, bottom: nil, watermark: nil, outpath: nil) ⇒ String Also known as: m

Create a meme, using the pre-loaded templates. If no template is specified, randomly picks one

Parameters:

  • name (String) (defaults to: nil)

    Name of the template to use. See #memes

  • top (String) (defaults to: nil)

    The text you want to appear on the top of the meme.

  • bottom (String) (defaults to: nil)

    The text you want to appear on the bottom of the meme.

  • watermark (String) (defaults to: nil)

    The watermark text. If nil it is omitted

  • outpath (String) (defaults to: nil)

    Where do you want to put the generated meme. Defaults to /tmp/

Returns:

  • (String)

    Path to the generated meme



74
75
76
77
78
79
80
81
82
83
# File 'lib/le_meme.rb', line 74

def fast_meme(name: nil, top: nil, bottom: nil, watermark: nil, outpath: nil)
  if name.nil?
    path = @memes[@memes.keys.sample]
  elsif @memes[name].nil?
    fail ArgumentError, "#{name} is not a pre-loaded meme"
  else
    path = @memes[name]
  end
  generate(path: path, top: top, bottom: bottom, watermark: watermark, outpath: outpath)
end

#generate(path:, top: nil, bottom: nil, watermark: nil, outpath: nil) ⇒ String Also known as: meme

Create a meme with the given text

Parameters:

  • path (String)

    The path to the image file you want to annotate.

  • top (String) (defaults to: nil)

    The text you want to appear on the top of the meme.

  • bottom (String) (defaults to: nil)

    The text you want to appear on the bottom of the meme.

  • watermark (String) (defaults to: nil)

    The watermark text. If nil it is omitted

  • outpath (String) (defaults to: nil)

    Where do you want to put the generated meme. Defaults to /tmp/

Returns:

  • (String)

    Path to the generated meme



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
# File 'lib/le_meme.rb', line 34

def generate(path:, top: nil, bottom: nil, watermark: nil, outpath: nil)
  top = (top || '').upcase
  bottom = (bottom || '').upcase

  path = Pathname.new(path).realpath

  canvas = Magick::ImageList.new(path)

  caption_meme(top, Magick::NorthGravity, canvas) unless top.empty?
  caption_meme(bottom, Magick::SouthGravity, canvas) unless bottom.empty?

  # Draw the watermark
  unless watermark.nil?
    watermark_draw = Magick::Draw.new
    watermark_draw.annotate(canvas, 0, 0, 0, 0, " #{watermark}") do
      self.font = 'Helvetica'
      self.fill = 'white'
      self.text_antialias(false)
      self.font_weight = 100
      self.gravity = Magick::SouthEastGravity
      self.pointsize = 10
      self.undercolor = 'hsla(0,0,0,.5)'
    end
  end

  output_path = outpath || "/tmp/meme-#{Time.now.to_i}#{path.extname}"
  canvas.write(output_path)
  output_path
end