imagecreatefrompng()+ imagettftext()低质量文本 - 如何反别名

Hea*_*ore 4 php gd image

拍摄以下基本图像(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);
}

?>
Run Code Online (Sandbox Code Playgroud)

这会产生低质量的文本(非常块状) - 如何对文本进行反别名,使其看起来流畅?

这是块状版本:

在此输入图像描述

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

造成这种情况的原因是什么?如何获得流畅的文字?

在此输入图像描述

hak*_*kre 8

说明:

imagealphablendingimagettftext在真彩色图像上使用时必须打开,否则根据图像的托盘颜色而不是每个目标像素的颜色计算混叠.

正确(显式)设置将是:

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

您的图片默认启用,这就是为什么将其设置为false先前创建的非锯齿效果.