Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize image size according to size of text

Tags:

php

image

gd

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 - Creating IMage from Text in PHP - how can I make multiline?

UPDATE 2: Martin Geisler's version also works well

like image 227
usertest Avatar asked Dec 20 '09 19:12

usertest


2 Answers

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.

like image 61
Martin Geisler Avatar answered Nov 09 '22 17:11

Martin Geisler


@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);
like image 2
Christian Davén Avatar answered Nov 09 '22 17:11

Christian Davén