【发布时间】: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 通道的透明图像上绘制文本,然后将其合并到绿色按钮(或者您称其为绿色的东西)。