4

采取以下基本图像(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 中放大)后,我可以看到我正在编写的文本没有抗锯齿并且正在写入的像素几乎是透明的?

是什么导致了这个 - 我如何获得流畅的文本?

在此处输入图像描述

4

2 回答 2

9

说明

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

正确的(显式)设置是:

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

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

于 2012-11-09T17:16:08.893 回答
2

弄清楚了:

我的电话imagealphablending()imagesavealpha()造成它的原因!如果我在写完文字后打电话给这些,那很好!

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

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

以下代码中的示例 PNG

<?
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);
    }
}

?>
于 2012-11-09T17:00:57.517 回答