【问题标题】:imagecreatefrompng() + imagettftext() low quality text - how to anti aliasimagecreatefrompng() + imagettftext() 低质量文本 - 如何抗锯齿
【发布时间】:2012-11-09 16:49:25
【问题描述】:

获取以下基本图像 (PNG-24):

我们正在尝试将文本写入图像,如下所示:

<?
ini_set('display_errors', 1); 
error_reporting(E_ALL);

//#### Load the base image
$im = imagecreatefrompng("images/SpecialClearanceBlank.png");
imagealphablending($im, false);
imagesavealpha($im, true);

//#### Create the badge
if($im) {
    //#### define some colours to use with the image
    $white = imagecolorallocate($im, 255, 255, 255);

    //#### get the width and the height of the base image
    $width = imagesx($im);
    $height = imagesy($im);

    //#### Define font and text
    $font = "/var/www/arial.ttf";
    $fontSize = 13;
    $angle = 0;
    $text = "15%";

    //#### calculate the left position of the text:
    $dimensions = imagettfbbox($fontSize, $angle, $font, $text);
    $textWidth = abs($dimensions[4] - $dimensions[0]);
    $leftTextPos = ( $width - $textWidth ) / 2;

    //#### finally, write the string:
    //imagestring($im, 5, $leftTextPos, $topTextPos, $text, $white);
    imagettftext($im, $fontSize, $angle, $leftTextPos + 1, 29, $white, $font, $text);

    // output the image
    // tell the browser what we're sending it
    Header('Content-type: image/png');
    // output the image as a png
    imagepng($im);

    // tidy up
    imagedestroy($im);
}

?>

这会产生低质量的文本(非常块状) - 如何对文本进行反锯齿以使其看起来平滑?

这是块状版本:

在仔细分析渲染的 png(在 photoshop 中放大)后,我可以看到我正在编写的文本没有抗锯齿,并且正在写入的像素几乎是透明的?

是什么原因造成的 - 如何获得平滑的文本?

【问题讨论】:

  • 我不能完全确定,因为我无法测试它,但是您是否尝试过更改imagecolortransparent 的值?
  • 您是否尝试更改您使用的字体?
  • 放大的图片显示有抗锯齿,但不是针对果岭,而是针对 100% 透明度。您可能需要首先在带有 alpha 通道的透明图像上绘制文本,然后将其合并到绿色按钮(或者您称其为绿色的东西)。

标签: php image gd


【解决方案1】:

Explanation

在真彩色图像上使用imagettftext 时必须打开imagealphablending,否则会根据图像背景颜色而不是每个目标像素的颜色计算混叠。

正确的(显式)设置是:

//#### Load the base image
$im = imagecreatefrompng("images/SpecialClearanceBlank.png");
imagealphablending($im, true);
                        ^^^^

您的图片默认启用它,这就是为什么将其设置为 false 之前会创建无锯齿效果。

【讨论】:

    【解决方案2】:

    想通了:

    是我打电话给imagealphablending()imagesavealpha() 的原因!如果我在写完文字后调用这些就可以了!

    (不知道为什么 - 会对解释感兴趣)

    下面是产生这个的工作代码:

    <?
    Header('Content-type: image/png');
    
    $Percentage = round(@$_GET["value"]);
    $root = dirname(__FILE__) . "\\";
    
    //#### Check the Cache
    if (file_exists("images/Badges_Discounts/" . $Percentage . ".png") === true) {
        //#### Serve image from cache
        $im = imagecreatefrompng("images/Badges_Discounts/" . $Percentage . ".png");
    
        //#### Fix transparency
        imagealphablending($im, false);
        imagesavealpha($im, true);
    
        //#### Output from cache
        imagepng($im);
    
        //#### tidy up
        imagedestroy($im);
    
    } else {
        //#### Load the base image
        $im = imagecreatefrompng("images/SpecialClearanceBlank.png");
    
        //#### Create the badge
        if($im) {
            //#### define some colours to use with the image
            $white = imagecolorallocate($im, 255, 255, 255);
    
            //#### get the width and the height of the base image
            $width = imagesx($im);
            $height = imagesy($im);
    
            //#### Define font and text
            $font = $root . "arial.ttf";
            $fontSize = 15;
            $angle = 0;
            $text = $Percentage . "%";
    
            //#### calculate the left position of the text:
            $dimensions = imagettfbbox($fontSize, $angle, $font, $text);
            $textWidth = abs($dimensions[4] - $dimensions[0]);
            $leftTextPos = ( $width - $textWidth ) / 2;
    
            //#### write the XX%
            imagettftext($im, $fontSize, $angle, $leftTextPos + 1, 26, $white, $font, $text);
    
            //#### write the word "off"
            $dimensions = imagettfbbox($fontSize, $angle, $font, "off!");
            $textWidth = abs($dimensions[4] - $dimensions[0]);
            $leftTextPos = ( $width - $textWidth ) / 2;
            imagettftext($im, 13, $angle, $leftTextPos + 4, 41, $white, $font, "off");
    
            //#### Fix transparency
            imagealphablending($im, false);
            imagesavealpha($im, true);
    
            //#### Save to cache
            imagepng($im, $root . "images\\Badges_Discounts\\" . str_replace("%","",$text) . ".png");
    
            //#### Output to browser
            imagepng($im);
    
            //#### tidy up
            imagedestroy($im);
        }
    }
    
    ?>
    

    【讨论】:

    • @tazotodua 完成 :)
    • 做,你刚刚使用imagealphablending($im, false); imagesavealpha($im, true); 来获得更好的文本质量?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2014-11-16
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多