使用 PHP 中的 gd 库获得更好的 gif 图像质量
我使用 gd 库创建了一个带有文本的透明 gif,但文本的输出质量不好。有人知道如何提高质量吗?
这是代码:
$req = explode('|', $_REQUEST['r']);
$text = $req[0];
header ("Content-type: image/gif");
$font = getFont($req[2]);
$font_size = $req[1];
$tmpcolor = getColor($req[3]);
$tmp_image=@imagecreatefromgif('gfx/transparent.gif');
$width = imagesx($tmp_image);
$height = imagesy($tmp_image);
//calculate the new width / height
$tmp = imagettfbbox($font_size,0,$font,$text);
$new_width = $tmp[2]+10;
$new_height = $font_size+5;
$new_image = imagecreate($new_width,$new_height);
ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width, $new_height, $width, $height);
$black = ImageColorAllocate($new_image, 0, 0,0);
$trans = ImageColortransparent($new_image,$black);
$color = ImageColorAllocate($new_image, trim($tmpcolor[0]), trim($tmpcolor[1]), trim($tmpcolor[2]));
imagettftext($new_image, $font_size, 0, 0, $font_size, $color, $font, $text);
//Grab new image
imagegif($new_image);
imagedestroy($new_image);
imagedestroy($tmp_image);
这是结果:
谢谢
I create a transparent gif with text with the gd library but the output quality of the Text isn't good. Has anybody an idea how can I improve the quality?
Here is the code:
$req = explode('|', $_REQUEST['r']);
$text = $req[0];
header ("Content-type: image/gif");
$font = getFont($req[2]);
$font_size = $req[1];
$tmpcolor = getColor($req[3]);
$tmp_image=@imagecreatefromgif('gfx/transparent.gif');
$width = imagesx($tmp_image);
$height = imagesy($tmp_image);
//calculate the new width / height
$tmp = imagettfbbox($font_size,0,$font,$text);
$new_width = $tmp[2]+10;
$new_height = $font_size+5;
$new_image = imagecreate($new_width,$new_height);
ImageCopyResized($new_image, $tmp_image,0,0,0,0, $new_width, $new_height, $width, $height);
$black = ImageColorAllocate($new_image, 0, 0,0);
$trans = ImageColortransparent($new_image,$black);
$color = ImageColorAllocate($new_image, trim($tmpcolor[0]), trim($tmpcolor[1]), trim($tmpcolor[2]));
imagettftext($new_image, $font_size, 0, 0, $font_size, $color, $font, $text);
//Grab new image
imagegif($new_image);
imagedestroy($new_image);
imagedestroy($tmp_image);
Here is the result:
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GIF 格式仅支持 1 位透明度(因此像素要么是透明的,要么是不透明的),因此您的文本具有锯齿状边缘。要获得平滑的边缘,请使用 PNG 格式(具有 8 位 Alpha 通道,这意味着 256 级半透明度),使用不透明的 GIF(即在不透明背景上)。
GIF format supports only 1-bit transparency (so pixel is either transpatent or opaque), so your text has jagged edges. To get smooth edges, use PNG format (which has 8-bit alpha channel, which means 256 levels of translucency), use GIF without transparency (i.e. on opaque background).
遗憾的是,GD 的 TrueType 字体渲染能力并不出色。
首先尝试
imageFTText()
系列函数。它们依赖于质量更好的外部 FreeType 库,并且比 TTF 函数更好地尊重 TrueType 字体中的字距调整信息(使文本看起来规则的特定字符对之间的单独距离)。如果这不起作用,请使用Imagemagick,根据我的经验,它远远优于 GD 所拥有的任何东西
Sadly, GD's TrueType font rendering capabilities are not great.
Try the
imageFTText()
family of functions first. They rely on the external FreeType library which is better in quality, and also respects the kerning information in TrueType fonts (the individual distances between specific pairs of characters that make text look regular) better than the TTF functions.If that doesn't work, use Imagemagick which in my experience is far superior to anything GD has to offer.
尝试使用 imagecreatetruecolor 而不是 imagecreate。
Try using imagecreatetruecolor instead of imagecreate.