Skip to content

Instantly share code, notes, and snippets.

@nhunzaker
Last active February 26, 2016 08:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhunzaker/20c4888adfb9717766f7 to your computer and use it in GitHub Desktop.
Save nhunzaker/20c4888adfb9717766f7 to your computer and use it in GitHub Desktop.
require 'mini_magick'
class Design
TEMP_TEXT_PATH = "./test_text.png"
def initialize (design)
@design = design
end
def opened_template
@opened_template ||= MiniMagick::Image.open(@design[:template])
end
def apply_text
opened_template.combine_options do |c|
c.font @design[:typeface]
c.fill @design[:color]
c.pointsize @design[:pointsize]
c.draw "translate #{@design[:x] + half_text_width},#{@design[:y]} "\
"rotate #{@design[:rotation]} "\
"text #{-half_text_width},0 '#{@design[:content]}'"
end
opened_template
end
def text_output
@text_output ||= begin
`convert -pointsize #{@design[:pointsize]} -font '#{@design[:typeface]}' label:'#{@design[:content]}' #{TEMP_TEXT_PATH}`
MiniMagick::Image.open(TEMP_TEXT_PATH)
end
end
def half_text_width
text_output[:width] * 0.5
end
end
# Produce a fake template for us to print upon
template = "./result.png"
`convert -size 400x400 xc:white #{template}`
# Generate a new design object
design = Design.new({
template: template,
color: '#1496bb',
content: 'Viget Labs',
pointsize: 50,
rotation: 45,
typeface: 'Helvetica',
x: 80,
y: 220
})
design.apply_text.write(template);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment