tags:

views:

73

answers:

2

The PHP code below generates text as a dynamically created image, how would I be able to get the image to only be as large as the text? Thanks.

<?php
    header('Content-Type: image/jpeg');

    $text='Test';

    $img = imageCreate(200,200);

    imagecolorallocate($img, 255, 255, 255);

    $textColor = imagecolorallocate($img, 0, 0, 0); 

    imagefttext($img, 15, 0, 0, 55, $textColor, 'bgtbt.ttf', $text);

    imagejpeg($img);

    imagedestroy($img);
?>

UPDATE 1: I found the answer here with the example of the original poster - http://stackoverflow.com/questions/1097583/creating-image-from-text-in-php-how-can-i-make-multiline

UPDATE 2: Martin Geisler's version also works well

+2  A: 

When using a TrueType font, you use the imageftbbox function to obtain the bounding box for a string typeset with your font. The bounding box gives the offsets from the base-point to the four corners in the rectangle occupied by the text. So if you store the bounding box in $bb and use imagefttext to put text at ($x, $y), then the corners will have these coordinates:

($x + $bb[6], $y + $bb[7])         ($x + $bb[4], $y + $bb[5])
                          +-------+
                          | Hello |
                          +-------+
($x + $bb[0], $y + $bb[1])         ($x + $bb[2], $y + $bb[3])

That tells us that we want an image width of ($x + $bb[2]) - ($x + $bb[6]) = $bb[2] - $bb[6] and similarly an image height of $bb[3] - $bb[7]. The text should then be rendered at coordinates (-$bb[6], -$bb[7]) inside that picture since we want to have

(0, 0) = ($x + $bb[6], $y + $bb[7]) ==> $x = -$bb[6]  and $y = -$bb[7]

You can try it out with this code. Put it into a file called img.php and browse to img.php?q=Hello to test:

<?php
header("Content-type: image/png");

$q     = $_REQUEST['q'];
$font  = "Impact.ttf";
$size  = 30;
$bbox   = imageftbbox($size, 0, $font, $q);

$width  = $bbox[2] - $bbox[6];
$height = $bbox[3] - $bbox[7];

$im    = imagecreatetruecolor($width, $height);
$green = imagecolorallocate($im, 60, 240, 60);

imagefttext($im, $size, 0, -$bbox[6], -$bbox[7], $green, $font, $q);
imagepng($im);
imagedestroy($im);
?>


If you use the bitmap fonts instead, then look at the imagefontwidth and imagefontheight functions.

Martin Geisler
I'd like to use ttf fonts, I looked at some of the examples on the imageftbbox page but I can't seem to get the dimensions of the text.
usertest
Yeah, I'm sorry that I answered the wrong question at first -- I've now given code for TrueType fonts instead.
Martin Geisler
I tried out the code and I get the error "The image “test.php” cannot be displayed, because it contains errors."
usertest
I guess you didn't change the name of the font? I just copied a random font to my webserver for testing. Change the header to `Content-type: text/plain` to see the errors PHP send back to you.
Martin Geisler
My mistake, works well thanks
usertest
No problem, I'm glad you got it working! :-)
Martin Geisler
A: 

@Martin Geisler's answer is almost correct, but I couldn't get my text to fit completely inside the image. I tried this instead, which works perfectly!

From the PHP Manual's User Contributed Notes:

$text = "<?php echo \"hello, world\"; ?>";
$font = "./arial.ttf";
$size = "60";

$bbox = imagettfbbox($size, 0, $font, $text);

$width = abs($bbox[2] - $bbox[0]);
$height = abs($bbox[7] - $bbox[1]);

$image = imagecreatetruecolor($width, $height);

$bgcolor = imagecolorallocate($image, 255, 255, 255);
$color = imagecolorallocate($image, 0, 0, 0);

$x = $bbox[0] + ($width / 2) - ($bbox[4] / 2);
$y = $bbox[1] + ($height / 2) - ($bbox[5] / 2);

imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $bgcolor);
imagettftext($image, $size, 0, $x, $y, $color, $font, $text);

$last_pixel= imagecolorat($image, 0, 0);

for ($j = 0; $j < $height; $j++)
{
    for ($i = 0; $i < $width; $i++)
    {
        if (isset($blank_left) && $i >= $blank_left)
        {
            break;
        }

        if (imagecolorat($image, $i, $j) !== $last_pixel)
        {
            if (!isset($blank_top))
            {
                $blank_top = $j;
            }
            $blank_left = $i;
            break;
        }

        $last_pixel = imagecolorat($image, $i, $j);
    }
}

$x -= $blank_left;
$y -= $blank_top;

imagefilledrectangle($image, 0, 0, $width - 1, $height - 1, $bgcolor);
imagettftext($image, $size, 0, $x, $y, $color, $font, $text);

header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
Christian Davén
Sorry, it seems that Windows and Linux works differently, and that no script seem to work perfectly in both.
Christian Davén