Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby/GD2: generate image that strictly fits a text

Reply
Thread Tools

Ruby/GD2: generate image that strictly fits a text

 
 
Jonathan M├ętillon
Guest
Posts: n/a
 
      06-19-2008
Hi!

I'm trying to use GD2::Image and GD2::Font::TrueType to get an image to
fit a text string. I use the bounding_rectangle() method to extract the
bounding box (bbox).

My issue is that the image never really fits the text. It adds some
margins. Maybe I don't resize the image correctly according to the bbox
data? Here's my code:

text = 'some text'
image = Image.import('white.png') #this is 1x1 white image
image.draw do |pen|
pen.font = Font::TrueType['Arial_Bold.ttf', 10]
pen.color = image.palette.resolve Color[0, 0, 0]
bbox = pen.font.bounding_rectangle(text)
width = bbox[:lower_right][0].abs + bbox[:upper_left][0].abs
height = bbox[:lower_right][1].abs + bbox[:upper_left][1].abs
image.resize! width, height
pen.move 0, bbox[:upper_right][1].abs
pen.text text
end
image.export('new-image.png')

Is there something I can do so the image strictly fits the text, with no
margin?

Thank you!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
How much text fits in the frame? Hank Mishkoff Javascript 5 12-26-2006 07:46 PM
PIL FITs image decoder jbrewer Python 8 11-23-2005 06:21 AM
Not Strictly Java but... Dave Brown Java 0 03-23-2005 11:40 AM
not strictly an asp.net or C# question but?..... Ollie ASP .Net 2 07-15-2004 01:49 PM



Advertisments