拍摄以下基本影像(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中放大),我可以看到我写的文字没有抗锯齿,写的像素几乎是透明的?
造成这种情况的原因是什么?如何获得流畅的文字?
答案 0 :(得分:8)
imagealphablending
时必须打开{p> imagettftext
,否则根据图像的托盘颜色而不是每个目标像素的颜色计算锯齿。
正确(显式)设置为:
//#### Load the base image
$im = imagecreatefrompng("images/SpecialClearanceBlank.png");
imagealphablending($im, true);
^^^^
默认情况下,您的图片已启用,因此将其设置为false
之前创建的非锯齿效果。
答案 1 :(得分:1)
想出来:
我对imagealphablending()
和imagesavealpha()
的电话是造成它的原因!如果我在写完文本之后给它们打电话就没事了!
(不知道为什么 - 会对解释感兴趣)
以下是产生此代码的工作代码:
<?
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);
}
}
?>