Finding A Word"s Length In Pixels

Finding A Word"s Length In Pixels

am 23.08.2007 15:51:00 von william.oram

Here's a toughie for me.

Using the GD library, I want to print a word or string as an image,
but with some constant amount of padding around it. The text is not
known beforehand. In other words, something like

________
| |
| flan |
|________|

I'm very new to GD, but offhand the only solution that comes to mind
is to create a very long general-purpose image, print the text at (x1,
0), measure the word there, destroy the image, then create a second
image with dimensions calculated from before. That seems rather
sloppy. Is there a better alternative?

Re: Finding A Word"s Length In Pixels

am 23.08.2007 15:58:26 von Erwin Moller

william.oram@gmail.com wrote:
> Here's a toughie for me.
>
> Using the GD library, I want to print a word or string as an image,
> but with some constant amount of padding around it. The text is not
> known beforehand. In other words, something like
>
> ________
> | |
> | flan |
> |________|
>
> I'm very new to GD, but offhand the only solution that comes to mind
> is to create a very long general-purpose image, print the text at (x1,
> 0), measure the word there, destroy the image, then create a second
> image with dimensions calculated from before. That seems rather
> sloppy. Is there a better alternative?
>

Hi,

If you use freetype2 fonts, have a look at:
imageftbbox()
imagefontheight()
imagefontwidth()

That should do the trick.

Regards,
Erwin Moller

Re: Finding A Word"s Length In Pixels

am 23.08.2007 15:59:50 von rf

wrote in message
news:1187877060.316184.32650@x40g2000prg.googlegroups.com...
> Here's a toughie for me.
>
> Using the GD library, I want to print a word or string as an image,

Why? If this is destined for a web page the the text should be exactly that,
text.

> but with some constant amount of padding around it. The text is not
> known beforehand. In other words, something like

Margin on the image, using CSS?

--
Richard.

Re: Finding A Word"s Length In Pixels

am 23.08.2007 17:01:27 von william.oram

On Aug 23, 8:59 am, "rf" wrote:

> Why? If this is destined for a web page the the text should be exactly that,
> text.

I promise you, I have a reason that makes plaintext insufficient. It
seems rather hasty to question and fix my project when I haven't said
a word about it.

Re: Finding A Word"s Length In Pixels

am 23.08.2007 17:01:50 von william.oram

On Aug 23, 8:58 am, Erwin Moller
wrote:
> william.o...@gmail.com wrote:
> > Here's a toughie for me.
>
> > Using the GD library, I want to print a word or string as an image,
> > but with some constant amount of padding around it. The text is not
> > known beforehand. In other words, something like
>
> > ________
> > | |
> > | flan |
> > |________|
>
> > I'm very new to GD, but offhand the only solution that comes to mind
> > is to create a very long general-purpose image, print the text at (x1,
> > 0), measure the word there, destroy the image, then create a second
> > image with dimensions calculated from before. That seems rather
> > sloppy. Is there a better alternative?
>
> Hi,
>
> If you use freetype2 fonts, have a look at:
> imageftbbox()
> imagefontheight()
> imagefontwidth()
>
> That should do the trick.
>
> Regards,
> Erwin Moller

Perfect. Thanks!

Re: Finding A Word"s Length In Pixels

am 23.08.2007 17:09:58 von Jerry Stuckle

rf wrote:
> wrote in message
> news:1187877060.316184.32650@x40g2000prg.googlegroups.com...
>> Here's a toughie for me.
>>
>> Using the GD library, I want to print a word or string as an image,
>
> Why? If this is destined for a web page the the text should be exactly that,
> text.
>
>> but with some constant amount of padding around it. The text is not
>> known beforehand. In other words, something like
>
> Margin on the image, using CSS?
>

Possibly a CAPTCHA implementation?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Finding A Word"s Length In Pixels

am 23.08.2007 21:04:46 von axlq

In article ,
rf wrote:
>
> wrote in message
>news:1187877060.316184.32650@x40g2000prg.googlegroups.com.. .
>> Here's a toughie for me.
>>
>> Using the GD library, I want to print a word or string as an image,
>
>Why? If this is destined for a web page the the text should be exactly that,
>text.

Wrong. Don't make rash assumptions about what the project
requirements should be when that project hasn't been described. A
need was posted, so the proper response is to address that need
rather than question it. There are plenty of reasons why someone
may want to render text as graphics:

* Graphical display of data requires text on the graph axes and
legend, and that makes most sense as part of the graphic.

* CAPTCHA requires the use of text in graphics.

* Using some text as graphics is a good way to shield content from
spiders and crawlers.

* Clickable buttons containing text descriptions.

* Etc.

-A