View Full Version : Obfuscate e-mail in a GIF with GD, what to do when text is too large to fit the image

03-11-2007, 03:45 AM

I'm working at a PHP script that has to obfuscate an e-mail address into a GIF generated with GD.

I use an Arial font of 10 points (since GD2 takes values in points) and guessing that one average letter is 7.5 pixels this almost does the job. However, if the string I'm trying to obfuscate is composed of many "m"s or "w"s the generated text's width exceeds the image width.

Is there any way to calculate the width of the image relative to the text?

Is there any formula between pixels and points?

03-11-2007, 04:14 AM
I sort of have a solution for you.

Someone else once asked me about getting the pixel width of text.

So, I thought about using Javascripting to capture the width
of a <td>. <td>'s will resize to fit any text, so you can measure that
width in pixels.

There is a problem though where you need to put that <td> somewhere
on your page to test it. You could make the text color the same as the
background so it would be invisible.

Anyhow, here's the example page: http://www.catpin.com/width.php

View the HTML source.

03-11-2007, 11:33 PM
If you have GD and the Freetype library (look at the output of phpinfo) you can use the imagettfbbox function to return the bounding box size in pixels for the text you are obfuscating.

imagettfbbox(size, angle, fontfile, text);


Be sure to note the comments about the function returning negative values, and the need to use the abs function (second comment down).

03-11-2007, 11:36 PM
Use a fixed-width font.

03-11-2007, 11:48 PM
Thanks AndyR, that did the job!