Friday, December 12, 2025
HomeLanguagesPHP | ImagickKernel getMatrix() Function

PHP | ImagickKernel getMatrix() Function

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
  
// Create a new imagick object
$imagick = new Imagick(
  
$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
  
// Create a new imagick object
$imagick = new Imagick(
  
// 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

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

Dominic
32445 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12029 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7199 POSTS0 COMMENTS
Thapelo Manthata
6895 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS