PHP | ImagickKernel getMatrix() Function
Last Updated :
14 Jan, 2020
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
$imagick = new Imagick(
$matrix = [
[-1, 0, 0],
[4, -1, 6],
[7, 8, 6]
];
$kernel = ImagickKernel::fromMatrix( $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
$imagick = new Imagick(
$kernel = ImagickKernel::fromBuiltIn(Imagick::KERNEL_DISK, "2" );
$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
Share your thoughts in the comments
Please Login to comment...