gpt4 book ai didi

PHP imagettftext 在透明背景上创建黑边

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:09:21 26 4
gpt4 key购买 nike

我有这个代码:

$im = imagecreatetruecolor(70, 25);

$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);

imagecolortransparent($im, imagecolorallocate($im, 0,0,0));

$font = 'font.ttf';

imagettftext($im, 20, 0, 3, 22, $white, $font, $randomnr);

header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);

就像我在标题中所说的那样,它在文本周围创建了一些黑边。我还尝试了 imagealphablending/imagesavealpha 并且得到了相同的结果(我在透明背景上使用了白色文本,以便您可以看到我在说什么) :

black edges

更新:解决方案是:

$im = imagecreatetruecolor(70, 25);
$font = 'font.ttf';
//antialiasing:
$almostblack = imagecolorallocate($im,254,254,254);
imagefill($im,0,0,$almostblack);
$black = imagecolorallocate($im,0,0,0);
imagecolortransparent($im,$almostblack);

imagettftext($im, 20, 0, 3, 22, $white, $font, $randomnr);
...

最佳答案

像这样的东西应该可以完成工作:

$width = 70;
$height = 25;

$im = imagecreatetruecolor($width, $height);

$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 221, 221, 221);

imageSaveAlpha($im, true);
imageAlphaBlending($im, false);

$transparent = imageColorAllocateAlpha($im, 0, 0, 0, 127);
imagefilledrectangle($im, 0, 0, $width-1, $height-1, $transparent);
imageAlphaBlending($im, true);

$font = 'font.ttf';
$randomnr = "1234";
imagettftext($im, 20, 0, 3, 22, $white, $font, $randomnr);

header("Expires: Wed, 1 Jan 1997 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

header ("Content-type: image/png");
imagepng($im);
imagedestroy($im);

关于PHP imagettftext 在透明背景上创建黑边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12672920/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com