Sha256: 66a3f6bda489b49baa7dcc4d157b71f1527de7130b52ddbb3ff05d98021dc599

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mememe-0.1.2 lib/mememe/meme.rb
mememe-0.1.1 lib/mememe/meme.rb
mememe-0.1.0 lib/mememe/meme.rb