set width of image based on text it displays

Posted on 2007-07-28
Last Modified: 2008-02-01
I can create a image with text on it, but my problem is getting the width of the image correct each time.

I want the width to fit the text nicely - not too much gaps to the left or to the right.

The text can change depending on what users type so I cant set a fixed width for the image.  I'm using a custom font (verdana.ttf) and to display 1 character I have worked out it takes a width of 7.8.    I cant tell if this is pixels or what?  

However I have tried working out some calculations to dynamically work out the width based on the number of chars, numbers, dots, dashes, special chars etc etc but it never works out correctly.

My PHP application takes a string of text and searches for a email address.  if it find a email address, it returns an image with the email address displayed on it so it cant be indexed as text by search engines.  

According to the web standards an email address can have X amount of characters and numbers (A-Z 0-9), 1 at symbol (@), X amount of dots (.), X amount of dashes (-) and X amount of underscores (_) to be valid for most email programs.

This is my snippet so far (that does not work out width correctly)

header('Content-type: image/png');

$email = fcnDecode(trim($_GET['input']));

$font = 'verdana.ttf';
$chars = 7.8;
$ats = 9;
$dots = 3;
$dashes = 7;
$underscores = 8;

$total_dots = substr_count($email,'.');
$total_ats = substr_count($email,'@');
$total_dash = substr_count($email,'-');
$total_underscore = substr_count($email,'_');
$total_chars = fcnCountChars($email);

$char = ($total_chars * $chars);
$at = ($total_ats * $ats);
$dot = ($total_dots * $dots);
$dash = ($total_dashes * $dashes);
$underscore = ($total_underscores * $underscores);

$length = $char + $at + $dot + $dash = $underscore;

$im = imagecreate($length,14);  <--- 14 here is the height of the image BTW
$bg = imagecolorallocate($im,244,244,244);
$fg = imagecolorallocate($im,33,33,33);

Question by:ellandrd
    LVL 17

    Expert Comment

    Can you give some examples of when it works, and when it doesn't work?

    Also, how "exact" do you want the fit to be?

    Only thing that I can see you are missing is the possible use of Capital Letters in the e-mail which should be slightly wider than normal caps.
    LVL 13

    Expert Comment

    Here's a bug, but it might have just been a mistake in copying it into EE:

    $length = $char + $at + $dot + $dash = $underscore;

    If your code really has that, then the effect would be $length = $char _ $at + $dot + 1, because $dash = $underscore will return a "1" for true if the assignment can able to be made (which it can).

    Just change "=" to "+".

    However, the real issue here is probably the fact that fonts and images are measured differently. 7.8 probably isn't pixels - it's probably points (or pt). The conversion of one measurement to the other is going to be hairy, since different screen resolutions and sizes can change the values in different ways.
    LVL 48

    Accepted Solution

    You can calculate the with exactly with this:

    $info = imagettfbbox(10,0,$font,$email); // see
    $im = imagecreate($info[2],14);  <--- 14 here is the height of the image BTW
    $bg = imagecolorallocate($im,244,244,244);
    $fg = imagecolorallocate($im,33,33,33);

    LVL 16

    Author Comment

    thank you hernst42

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
    Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now