Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php imagecreatefrompng();imagettftext()低质量文本-如何防止别名_Php_Image_Gd - Fatal编程技术网

Php imagecreatefrompng();imagettftext()低质量文本-如何防止别名

Php imagecreatefrompng();imagettftext()低质量文本-如何防止别名,php,image,gd,Php,Image,Gd,拍摄以下基本图像(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);

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

?>
找到了答案:

是我对
imagealphabling()
imagesavealpha()
的调用导致了它!如果我在写完课文后打电话给他们就好了

(不确定原因-希望得到解释)

以下是生成此文件的工作代码:

对真彩色图像使用
imagettftext
时,必须启用
imagealphablending
,否则将根据图像的圆形颜色而不是每个目标像素的颜色计算混叠

正确的(明确的)设置为:

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

您的图片在默认情况下已启用,这就是为什么将其设置为
false
之前创建了无锯齿效果的原因。

我不能完全确定,因为我无法测试它,但您是否尝试更改
imagecolortransparent
的值?您是否尝试更改正在使用的字体?缩放的图片显示存在抗锯齿,但不是反对绿党,而是反对100%的透明度。你可能需要先用alpha通道在一个透明的图像上绘制文本,然后将其合并到绿色按钮上(或者你称之为绿色的东西)。@tazotodua Done:)你只需要使用
imagealphablending($im,false);imagesavealpha($im,true)以获得更好的文本质量?
//#### Load the base image
$im = imagecreatefrompng("images/SpecialClearanceBlank.png");
imagealphablending($im, true);
                        ^^^^