Example
function colorMatrixImage($image_path, $color_matrix)
{
$imagick = new \Imagick(realpath($image_path));
$imagick->setImageAlpha(1);
// Imagick expects the values to be in a flat (single dimension)
// array, but we have a 2d array, so flatten the values.
$color_matrix_values = [];
foreach ($color_matrix as $row => $values) {
foreach ($values as $value) {
$color_matrix_values[] = $value;
}
}
//A color matrix should look like:
// $colorMatrix = [
// 1.5, 0.0, 0.0, 0.0, 0.0, -0.157,
// 0.0, 1.0, 0.5, 0.0, 0.0, -0.157,
// 0.0, 0.0, 1.5, 0.0, 0.0, -0.157,
// 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
// 0.0, 0.0, 0.0, 0.0, 1.0, 0.0,
// 0.0, 0.0, 0.0, 0.0, 0.0, 1.0
// ];
$background = new \Imagick();
$background->newPseudoImage(
$imagick->getImageWidth(),
$imagick->getImageHeight(),
"pattern:checkerboard"
);
$background->setImageFormat('png');
$imagick->setImageFormat('png');
$imagick->colorMatrixImage($color_matrix_values);
$background->compositeImage($imagick, \Imagick::COMPOSITE_ATOP, 0, 0);
header("Content-Type: image/png");
echo $background->getImageBlob();
}