require 'mini_magick' module MemeMe module Meme attr_accessor :image def name @name || "meme" end def top_phrase @top_phrase || "" end def top_phrase=(phrase) @top_phrase = phrase end def bottom_phrase @bottom_phrase || "" end def bottom_phrase=(phrase) @bottom_phrase = phrase end def generate(path) raise ArgumentError, "MemeMe::Meme.generate requires a valid image path" unless @image && File.exists?(image) img = MiniMagick::Image.open(image) write_top_phrase img write_bottom_phrase img img.write(File.join(path, "#{name}#{File.extname(image)}")) end def write_top_phrase(img) img.combine_options do |c| meme_text top_phrase, c end end def write_bottom_phrase(img) img.combine_options do |c| meme_text bottom_phrase, c, { position: 'South' } end end def meme_text(text, c, config = {}) settings = { font: MemeMe.fonts[:impact], position: 'North', pointsize: 50, stroke_color: '#000', stroke_width: 2, color: '#FFF' }.merge! config settings[:pointsize] = calculate_pointsize text, settings[:pointsize] c.gravity settings[:position] c.font settings[:font] c.pointsize settings[:pointsize] c.stroke settings[:stroke_color] c.strokewidth settings[:stroke_width].to_s c.fill settings[:color] c.draw "text 0,0 '#{text.upcase}'" end def calculate_pointsize text, pointsize if text.size > 20 pointsize = pointsize - (text.size - 20) * 1.25 pointsize = 28 if pointsize < 28 pointsize.to_s else pointsize.to_s end end end end