我尝试在此解决方案中设置文本渐变 https://www.sitepoint.com/community/t/gd-text-gradient/82127/9
但是最终图像的背景颜色是黑色,我尝试了 $im->flattenImages
和 $img->setBackgroundColor
但它不起作用。
$im = new Imagick();
$draw = new ImagickDraw();
$draw->setFontSize(90);
$draw->setFillColor(new ImagickPixel("black"));
$draw->setTextEncoding('UTF-8');
$draw->setGravity(Imagick::GRAVITY_CENTER);
$metric = $im->queryFontMetrics($draw, $text);
$width = $metric['textWidth'];
$height = $metric['textHeight'];
/* Create and save the gradiant */
$Imagick = new Imagick();
$Imagick->newPseudoImage($height, $width, "gradient:#FB7F4C-#FF409C");
/*** rotate the image ***/
$Imagick->rotateImage(new ImagickPixel(), 270);
$Imagick->setImageFormat('png');
$Imagick->writeImage("gradiant.png");
/* Create and save the canvas */
$im->newPseudoImage($width, $height, "null:");
$im->setImageFormat('png');
$im->writeImage("canvas.png");
/* Add the text to the canvas ( Make the mask )*/
$im = new Imagick("canvas.png");
// Write the text on the image
$im->annotateImage($draw, 0, 0, 0, $text);
$im->setImageBackgroundColor("transparent"); // <= Here
/* Final image */
$canvas = new Imagick("gradiant.png");
$canvas->compositeImage($im, imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA);
$canvas->setImageFormat('png');
$canvas->writeImage(__DIR__ . "/../../final.png");
header('Content-Type: image/' . $im->getImageFormat());
echo $canvas;
unlink("canvas.png");
unlink("gradiant.png");
我发现在COMPOSITE_DSTIN
之后背景颜色变黑,我尝试了很多方法但它不起作用
如何去除黑色背景?
最佳答案
我通过添加 alphachannel 解决了这个问题
我尝试 $canvas->setImageAlphaChannel(Imagick::ALPHACHANNEL_RESET);
之后背景保持透明
更新
在我更新了 imagick 库后,它停止工作并搜索类似的问题,并发现了这个 Imagemagick compose image inverted
这里的代码运行良好
$canvas->transformImageColorspace(Imagick::COLORSPACE_SRGB);
关于PHP imagick COMPOSITE_DSTIN 结果有黑色背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56677423/