PHP imagick COMPOSITE_DSTIN 结果有黑色背景
PHP imagick COMPOSITE_DSTIN result has black background
我尝试通过此处的解决方案设置文本渐变 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 库后它停止工作并搜索类似的问题并找到这个
这里是运行良好的代码
$canvas->transformImageColorspace(Imagick::COLORSPACE_SRGB);
我尝试通过此处的解决方案设置文本渐变 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 库后它停止工作并搜索类似的问题并找到这个
这里是运行良好的代码
$canvas->transformImageColorspace(Imagick::COLORSPACE_SRGB);