Open In App

PHP | ImagickKernel fromMatrix() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The ImagickKernel::fromMatrix() function is an inbuilt function in PHP which is used to create a kernel from an 2d matrix of values. The value of 2d matrix is either float if element is used otherwise false if element is skipped.

Syntax:

ImagickKernel ImagickKernel::fromMatrix( array $matrix, array $origin )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $matrix: It specifies the matrix of values that define the kernel. Values can be float or false.
  • $origin (optional): It specifies the element of kernel that should be used as the origin pixel. This is only required in case of non-square matrix.

Return Value: This function returns a new ImagickKernel object on success.

Below programs illustrate the ImagickKernel::fromMatrix() function in PHP:

Program 1:




<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
$matrix = [
    [-1.5, -1, -0.2],
    [0, 1, 1],
    [0, 0.5, 1],
];
  
// Create a kernel from matrix
$kernel = ImagickKernel::fromMatrix($matrix);
  
// Add the filter
$imagick->filter($kernel);
  
// Show the output
$imagick->setImageFormat('png');
header("Content-Type: image/png");
echo $imagick->getImageBlob();
?>


Output:

Program 2:




<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
$matrix = [
    [1, 2, -3],
    [4, 5, 6],
    [7, 8, 9]
];
  
// Create a kernel from matrix
$kernel = ImagickKernel::fromMatrix($matrix);
  
// Get the matrix
$matrix = $kernel->getMatrix();
  
print("<pre>".print_r($matrix, true)."</pre>");
?>


Output:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => -3
        )

    [1] => Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )

    [2] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
        )

)

Reference: https://www.php.net/manual/en/imagickkernel.frommatrix.php



Last Updated : 14 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads