Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PHP | Imagick colorMatrixImage() Function

  • Last Updated : 23 Aug, 2019

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:




   
<?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();
   
?>

Output:

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!