PHP imagick COMPOSITE_DSTIN 结果有黑色背景

标签 php imagick

我尝试在此解决方案中设置文本渐变 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/

相关文章:

php - header ('P3P: CP="CAO PSA OUR"'); 有什么作用?

php sql 不工作

php - 增加 Imagicks 最大分辨率

带有轮廓文本问题的 PHP ImagickDraw

php - 从ajax调用输出图像的最快方法

php - 使用透明 PNG 作为剪贴蒙版

php - Codeigniter 3.x - 将 $data 与动态数据一起预加载到所有 View

PHP 使用 Cookie 在数据库中存储当前 session ID

javascript - 从另一个域加载脚本时,无法从 PHP 读取 Internet Explorer 中的 cookie

PHP 无法加载 Imagick 库 - PHP 启动 : Unable to load dynamic library