繁体   English   中英

根据文本大小调整图像大小

[英]Resize image size according to size of text

下面的PHP代码生成文本作为动态创建的图像,我怎样才能使图像只与文本一样大? 谢谢。

<?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);
?>

更新1:我在这里找到了原始海报示例的答案 - 在PHP中用文本创建IMage - 如何制作多行?

更新2:Martin Geisler的版本也很好用

使用TrueType字体时,使用imageftbbox函数获取字体字符串排版的边界框。 边界框给出了从基点到文本占据的矩形中的四个角的偏移量。 因此,如果您将边界框存储在$bb并使用imagefttext将文本放在($x, $y) ,那么角将具有以下坐标:

($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])

这告诉我们,我们想要的图像宽度为($x + $bb[2]) - ($x + $bb[6]) = $bb[2] - $bb[6] ,同样地,图像高度为$bb[3] - $bb[7] 然后,文本应该在该图片中的坐标(-$bb[6], -$bb[7])处呈现,因为我们希望

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

您可以使用此代码进行试用。 把它放到一个名为img.php的文件中并浏览到img.php?q=Hello测试:

<?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);
?>

如果您使用位图字体,请查看imagefontwidthimagefontheight函数。

@Martin Geisler的答案几乎是正确的,但我无法让我的文字完全适合图像。 我尝试了这个,它完美无缺!

PHP手册的用户贡献说明

$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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM