Wednesday, October 8, 2025
HomeLanguagesPHP | ImagickKernel fromMatrix() Function

PHP | ImagickKernel fromMatrix() Function

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

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
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS