Open In App

PHP | ImagickKernel getMatrix() Function

Last Updated : 14 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
The ImagickKernel::getMatrix() function is an inbuilt function in PHP which is used to get the 2D matrix of values used in a kernel. The elements are either float or 'false' if element should be skipped. Syntax:
array ImagickKernel::getMatrix( void )
Parameters:This function doesn’t accepts any parameter. Return Value: This function returns an array value containing the matrix. Below programs illustrate the ImagickKernel::getMatrix() function in PHP: Program 1: This program uses getMatrix() function to get the matrix from user-defined matrix. php
<?php

// Create a new imagick object
$imagick = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');

$matrix = [
    [-1, 0, 0],
    [4, -1, 6],
    [7, 8, 6]
];

// 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] => 0
            [2] => 0
        )

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

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

)
Program 2 (Get matrix from built-in matrix): php
<?php

// Create a new imagick object
$imagick = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');

// Create a kernel from built-in matrix
$kernel = ImagickKernel::fromBuiltIn(Imagick::KERNEL_DISK, "2");

// Get the matrix
$matrix = $kernel->getMatrix();

foreach ($matrix as $row) {
    foreach ($row as $cell) {
        if ($cell === false) {
            $output .= 0;
        } else {
            $output .= $cell;
        }
    }
    $output .= "<br>";
}
echo $output;
?>
Output:
00100
01110
11111
01110
00100
Reference: https://www.php.net/manual/en/imagickkernel.getmatrix.php

Explore