Skip to content

Instantly share code, notes, and snippets.

@barek2k2
Created May 5, 2013 15:26
Show Gist options
  • Save barek2k2/5521112 to your computer and use it in GitHub Desktop.
Save barek2k2/5521112 to your computer and use it in GitHub Desktop.
Drawing images from text in ruby on rails application
def draw_image
#Image parameters
options = {:img_width => 300, :img_height => 250, :text_color => "#FF0000", :font_size => 36,
:text => "This is text", :bg_color => "#EFEFEF"}
#Initialize a container with it's width and height
container=Magick::Image.new(options[:img_width],options[:img_height]){
self.background_color = options[:bg_color]
}
#Initialize a new image
image=Magick::Draw.new
image.stroke('transparent')
image.fill(options[:text_color])
image.font='/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType/Verdana_Italic.ttf'
image.pointsize=options[:font_size]
image.font_weight=Magick::BoldWeight
image.text(0,0,options[:text])
image.text_antialias(false)
image.font_style=Magick::NormalStyle
image.gravity=Magick::CenterGravity
#Place the image onto the container
image.draw(container)
container=container.raise(3,1)
# To test the image(a pop up will show you the generated dynamic image)
container.display
# generated image will be saved in public directory
#container.write("public/image.gif")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment