PHP | Imagick colorMatrixImage() Function

The Imagick::colorMatrixImage() function is an inbuilt function in PHP which is used to apply color transformation to the images. This function induces saturation changes, hue rotation, luminance to alpha, and various other effects. This function uses variable-size transformation matrices i.e. 5×5 matrix for RGBA and 6×6 matrix for CMYKA.

Syntax:

bool Imagick::colorMatrixImage( array $color_matrix = Imagick::CHANNEL_DEFAULT )

Parameters: This function accepts single parameter $color_matrix which is used to hold a 5×5 matrix for RGBA with the rows meaning red, green, blue, alpha output, and the columns being red, green, blue, alpha input, whereas last row and columns for brightness adjustment. In 6×6 matrix for CMYKA with the rows meaning Cyan, Magenta, Yellow, Key or black, alpha output, and the columns being Cyan, Magenta, Yellow, Key or black, alpha input, whereas alpha for brightness adjustment similarly in RGBA, CMYKA also have last row and columns for brightness adjustment.



Return Value: This function returns True on success and return False on failure.

Below program illustrates the Imagick::colorMatrixImage() function in PHP:

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

   
<?php
   
// 6x6 color matrix for CMYKA
$colorMatrix = [
    1.5, 0.0, 0.0, 0.0, 0.0, -0.157,
    0.0, 0.0, 0.5, 0.0, 0.0, -0.157,
    0.0, 0.0, 0.0, 0.0, 0.5, -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.5, 0.0,  1.0
];
  
// Create Imagick object 
$imagick = new \Imagick(
     
// Set image opacity
$imagick->evaluateImage(
    Imagick::EVALUATE_MULTIPLY, 
    0.6, 
    Imagick::CHANNEL_ALPHA
);
   
// Create new Imagick object
$background = new \Imagick();
  
// Creating new pseudo image with hexagon pattern
$background->newPseudoImage(
    $imagick->getImageWidth(), 
    $imagick->getImageHeight(),  
    "pattern:hexagons"
);
   
// Set the image format
$background->setImageFormat('png');
$imagick->setImageFormat('png');
  
// Use Imagick::colorMatrixImage() function
$imagick->colorMatrixImage($colorMatrix);
  
// Use Imagick::compositeImage() function     
$background->compositeImage(
    $imagick
    \Imagick::COMPOSITE_SRCATOP,
    0,
    0
);
  
header("Content-Type: image/png");
  
// Display the output image
echo $background->getImageBlob();
   
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/imagick.colormatriximage.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.