Php imagecreatefrompng();imagettftext()低质量文本-如何防止别名
拍摄以下基本图像(PNG-24): 我们正在尝试将文本写入图像,如下所示: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);
<?
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);
^^^^