Wednesday, September 25, 2024
Google search engine
HomeLanguagesPHP | Imagick colorMatrixImage() Function

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:




   
<?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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments