Skip to content

Instantly share code, notes, and snippets.

@nhunzaker nhunzaker/designed.rb Secret
Last active Feb 26, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.