PHP GD 从创建的 PNG 中修剪透明像素

发布于 2024-09-14 13:04:22 字数 1393 浏览 5 评论 0原文

我目前正在开发一个网站,最近一直在使用 GD 和 PHP 来自动创建透明图像,其中包含用于网站上的导航、标题等的文本。它为我节省了很多使用 Photoshop 的时间,而且我可以在需要时立即更改按钮上的文本。

好吧,我在这里陷入了死胡同。我找到了将作为我的图像创建的“文本框”调整为文本大小的方法。但我面临的问题是我使用的 TTF 字体与 GD 期望的大小不同。基本上,最后一个字母将从输出图像中被切掉。所以我想知道是否有一种方法可以解决这个问题,同时保持文本的紧密边缘,或者使原始文本框的尺寸更大,然后“修剪”图像上的透明像素。

这是我现在正在使用的代码...

<?
$text = strip_tags($_GET['btn']);
if(file_exists('nav_'.$text.'.png')) {
    header("Content-type: image/png");

    $image = imagecreatefrompng('nav_'.$text.'.png');
    imagesavealpha($image, true);
    imagealphablending($image, false);
    imagepng($image);
    imagedestroy($image);
} else {
    header("Content-type: image/png");

    $fontSize = 10;
    $angle = 0;
    $font = "RonniaBold.ttf";

    $size = imagettfbbox($fontSize, $angle, $font, $text);
    $image = imagecreatetruecolor(abs($size[2]) + abs($size[0]) + 5, abs($size[7]) + abs($size[1]) + 5);
    imagesavealpha($image, true);
    imagealphablending($image, false);

    $transparentColor = imagecolorallocatealpha($image, 200, 200, 200, 127);
    imagefill($image, 0, 0, $transparentColor);

    $textColor = imagecolorallocate($image, 125, 184, 222);
    imagettftext($image, $fontSize, 0, 1, abs($size[5])+1, $textColor, $font, str_replace('_',' ',strtoupper($text)));
    imagepng($image, 'nav_'.$text.'.png', 0);
    imagedestroy($image);
}
?>

希望你们对此有一些见解,我真的可以使用它!

I am currently working on a website and lately have been using GD with PHP to automatically create transparent images that consist of text for use with navigation, headers, etc on the website. It saves me a lot of time in Photoshop plus I can change the text on the buttons instantly when needed.

Well I have hit a dead end here. I found the method of sizing the "textbox", created as my image, to what the size of the text is. But the problem I am facing is the fact that I am using a TTF font which is different then what GD expects the size to be. Basically, the last letter would be chopped off the outputted image. So I was wondering if there was a way to fix this while keeping a tight edge to the text or make the original textbox a much larger size and then "trim" the transparent pixels off the image.

This is the code I'm working with now...

<?
$text = strip_tags($_GET['btn']);
if(file_exists('nav_'.$text.'.png')) {
    header("Content-type: image/png");

    $image = imagecreatefrompng('nav_'.$text.'.png');
    imagesavealpha($image, true);
    imagealphablending($image, false);
    imagepng($image);
    imagedestroy($image);
} else {
    header("Content-type: image/png");

    $fontSize = 10;
    $angle = 0;
    $font = "RonniaBold.ttf";

    $size = imagettfbbox($fontSize, $angle, $font, $text);
    $image = imagecreatetruecolor(abs($size[2]) + abs($size[0]) + 5, abs($size[7]) + abs($size[1]) + 5);
    imagesavealpha($image, true);
    imagealphablending($image, false);

    $transparentColor = imagecolorallocatealpha($image, 200, 200, 200, 127);
    imagefill($image, 0, 0, $transparentColor);

    $textColor = imagecolorallocate($image, 125, 184, 222);
    imagettftext($image, $fontSize, 0, 1, abs($size[5])+1, $textColor, $font, str_replace('_',' ',strtoupper($text)));
    imagepng($image, 'nav_'.$text.'.png', 0);
    imagedestroy($image);
}
?>

Hopefully you guys have some insight to this, I could really use it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城泪 2024-09-21 13:04:22

只要有可能,我都会使用 Imagick 类,因为我更喜欢 ImageMagick图书馆。以下示例几乎逐字取自此处。它根据提供的文本大小创建图像:

$text = 'Hello World!';

// Create a new ImageMagick objects.
$im = new Imagick();
$draw = new ImagickDraw();
$colour = new ImagickPixel('#000000');
$background = new ImagickPixel('none');

// Set font properties.
$draw->setFont('Fertigo_PRO.otf');
$draw->setFontSize(72);
$draw->setFillColor($colour);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);

// Get font metrics.
$fm = $im->queryFontMetrics($draw, $text);

// Create text.
$draw->annotation(0, $fm['ascender'], $text);

// Create a new empty canvas, using the text size.
$im->newImage($fm['textWidth'], $fm['textHeight'], $background);

// Create the image.
$im->setImageFormat('png');
$im->drawImage($draw);

// Save the image.
$im->writeImage('test.png');

如果您想了解有关 Imagick 类的更多信息,我推荐优秀的 Imagick 文章,来自 Mikko 的博客

Wherever possible, I use the Imagick class, as I prefer the ImageMagick library. The following example is taken almost verbatim from the example given here. It creates an image based on the size of the text supplied:

$text = 'Hello World!';

// Create a new ImageMagick objects.
$im = new Imagick();
$draw = new ImagickDraw();
$colour = new ImagickPixel('#000000');
$background = new ImagickPixel('none');

// Set font properties.
$draw->setFont('Fertigo_PRO.otf');
$draw->setFontSize(72);
$draw->setFillColor($colour);
$draw->setStrokeAntialias(true);
$draw->setTextAntialias(true);

// Get font metrics.
$fm = $im->queryFontMetrics($draw, $text);

// Create text.
$draw->annotation(0, $fm['ascender'], $text);

// Create a new empty canvas, using the text size.
$im->newImage($fm['textWidth'], $fm['textHeight'], $background);

// Create the image.
$im->setImageFormat('png');
$im->drawImage($draw);

// Save the image.
$im->writeImage('test.png');

If you want more information on the Imagick class, I recommend the excellent Imagick articles from Mikko's Blog.

巷雨优美回忆 2024-09-21 13:04:22

您的代码有问题,导致您无法确定文本的正确大小 - 当使用 imagettfbox 询问文本框的大小时,您使用 $text:

$size = imagettfbbox($fontSize, $angle, $font, $text);

但是当您将文本写入图像时,您使用 strtoupper($text ) - 这会使您的文本变大(除非您使用等宽字体)。

You have a problem with your code which prevents you from determining the correct size of your text - when asking for the size of the text box with imagettfbox you use $text:

$size = imagettfbbox($fontSize, $angle, $font, $text);

But when you write the text to the image you use strtoupper($text) - which makes your text bigger (unless you're using a monospaced font).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文