使用 PHP 中的 gd 库获得更好的 gif 图像质量

发布于 2024-09-06 05:24:55 字数 1498 浏览 4 评论 0原文

我使用 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);

这是结果:

result
http://desmond.yfrog.com/Himg691/scaled.php?tn=0&ser​​ver=691&filename=createphp.gif&xsize=640&ysize=640

谢谢

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:

result
http://desmond.yfrog.com/Himg691/scaled.php?tn=0&server=691&filename=createphp.gif&xsize=640&ysize=640

Thank you

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

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

发布评论

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

评论(3

扶醉桌前 2024-09-13 05:24:55

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).

执着的年纪 2024-09-13 05:24:55

其他回答者指出这可能是一个简单的透明度问题,而不是 TrueType 渲染问题。首先尝试这些建议,因为它们可能已经解决了当前的问题。

遗憾的是,GD 的 TrueType 字体渲染能力并不出色。

  • 首先尝试 imageFTText() 系列函数。它们依赖于质量更好的外部 FreeType 库,并且比 TTF 函数更好地尊重 TrueType 字体中的字距调整信息(使文本看起来规则的特定字符对之间的单独距离)。

  • 如果这不起作用,请使用Imagemagick,根据我的经验,它远远优于 GD 所拥有的任何东西

Other answerers point out that this could be a simple transparency issue rather than a TrueType rendering one. Try those suggestions first, as they may already remedy the problem at hand.

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.

你的心境我的脸 2024-09-13 05:24:55

尝试使用 imagecreatetruecolor 而不是 imagecreate。

Try using imagecreatetruecolor instead of imagecreate.

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