PHP GD文本不流畅
问题截图:
我试图获得相同的字体质量,如Font Squirrel’s sample fonts widget,但字体不断出现粗糙.它在Photoshop中很流畅.注意:“懒狗”部分并没有被我加粗,它本身就是这样做的. 这是PHP: <?php putenv('GDFONTPATH=' . realpath('.')); $font = $_GET['font'] . '.ttf'; $text = 'The Quick Brown Fox Jumps over the Lazy Dog'; // Create the image function imageCreateTransparent($x,$y) { $imageOut = imagecreate($x,$y); $colourBlack = imagecolorallocate($imageOut,0); imagecolortransparent($imageOut,$colourBlack); return $imageOut; } $image = imageCreateTransparent(600,800); // Create some colors $white = imagecolorallocate($image,255,255); $grey = imagecolorallocate($image,128,128); $black = imagecolorallocate($image,0); imagefilledrectangle($image,399,29,$white); // Add the text imagettftext($image,20,10,$black,$font,$text); imagepng($image); imagealphablending($image,true); imagedestroy($image); ?> HTML:< img src =“fontgen.php?font = Aller_Rg”alt =“”/> 如何获得字体的高质量结果? 解决方法
您只将背景的一部分设置为白色,其余部分是透明的.
当在白色背景上绘制字体时,黑色文本会消除锯齿,使其看起来很平滑,这会导致字体周围的像素被绘制为两种颜色之间的混合,这也使字体看起来更小. 在右侧没有背景颜色,因此抗锯齿功能无法正常工作.绘图算法不是在字体颜色和背景颜色之间进行混合,而是使用原始字体颜色来处理甚至部分被字母覆盖的任何像素. 这使得字母看起来“粗体”,因为边缘像素现在是黑色,而不是灰色阴影. 正确解决此问题的方法是使用具有适当背景颜色的图像,即使该背景颜色是透明的.这使得图像库使用适当的alpha通道(这是进行alpha混合的唯一合理方式)而不是使用基于索引的alpha,其中只有一个’颜色’是透明的而所有其他颜色都是完全不透明的. $font = '../../fonts/Aller_Rg.ttf'; $text = 'The Quick Brown Fox Jumps over the Lazy Dog'; // Create the image function imageCreateTransparent($x,$y) { $imageOut = imagecreatetruecolor($x,$y); $backgroundColor = imagecolorallocatealpha($imageOut,127); imagefill($imageOut,$backgroundColor); return $imageOut; } $image = imageCreateTransparent(600,0); imagefilledrectangle($image,$white); //// Add the text imagettftext($image,$text); //imagealphablending($image,true); //not needed as we created the image with alpha imagesavealpha($image,true); //imagepng($image,'../../var/log/wtf5.png'); imagepng($image); imagedestroy($image); 这将使字体大小正确,因为抗锯齿将正常工作*并且图像在适当的情况下将是透明的,例如,使用上面的代码创建的图像,显??示在红色背景上. 具有白色背景的图像的位是白色的,图像的透明位使红色通过,并且文本对两者都正确地消除锯齿. *假设您想要对背景颜色设置为反别名,但情况并非总是如此,但可能就在这里. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |