-
-
Save nhunzaker/20c4888adfb9717766f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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