PHP Imagick颜色灰度渐变

I'am new to Imagick and PHP. The thing I want to make is to put a RGB color on gray-scale gradient.

Input:

Input

Output:

enter image description here

The output color may be different. Any idea would be helpful.

Thanks in advance! Greetigns,

There is the colorizeImage function which would do what you asked.

function colorizeImage($imagePath, $color, $opacity) {
    $imagick = new \Imagick(realpath($imagePath));
    //TBH - not sure if opacity is meant to be on the color or colorize call
    //neither seem to have much effect.
    $opacity = $opacity / 255.0;
    $opacityColor = new \ImagickPixel("rgba(0, 0, 0, $opacity)");
    $imagick->colorizeImage($color, $opacityColor);
    header("Content-Type: image/jpg");
    echo $imagick->getImageBlob();
}


$color = "rgb(252, 38, 231)";
$opacity = 0.5
colorizeImage("someImage.png", $color, opacity);

However, why do you want to do it like that? You can just create a colorized gradient directly.

$opacity = new \Imagick();
$opacity->newPseudoImage(100, 50, "gradient:rgb(255,128,128,0.5)-none");

//Gradients are created down
$opacity->rotateimage('black', 90);

Which creates a perfect gradient in one step.