PHP GD 从创建的 PNG 中修剪透明像素
我目前正在开发一个网站,最近一直在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要有可能,我都会使用 Imagick 类,因为我更喜欢 ImageMagick图书馆。以下示例几乎逐字取自此处。它根据提供的文本大小创建图像:
如果您想了解有关 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:
If you want more information on the Imagick class, I recommend the excellent Imagick articles from Mikko's Blog.
您的代码有问题,导致您无法确定文本的正确大小 - 当使用 imagettfbox 询问文本框的大小时,您使用 $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:
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).