Class: LeMeme
- Inherits:
-
Object
- Object
- LeMeme
- Defined in:
- lib/le_meme.rb,
lib/le_meme/version.rb
Overview
Create some dank memes
Constant Summary collapse
- VALID_IMAGE_EXTENSIONS =
/\.(jp[e]?g|png|gif)$/i- VERSION =
"0.0.5"
Instance Attribute Summary collapse
-
#memes ⇒ Object
readonly
Returns the value of attribute memes.
Instance Method Summary collapse
-
#fast_meme(name: nil, top: nil, bottom: nil, watermark: nil, outpath: nil) ⇒ String
(also: #m)
Create a meme, using the pre-loaded templates.
-
#generate(path:, top: nil, bottom: nil, watermark: nil, outpath: nil) ⇒ String
(also: #meme)
Create a meme with the given text.
-
#initialize(*dirs) ⇒ String
constructor
Create a new instance of LeMeme, for generating memes.
Constructor Details
#initialize(*dirs) ⇒ String
Create a new instance of LeMeme, for generating memes.
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
#memes ⇒ Object (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
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
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 |