Saturday, November 22, 2025
HomeLanguagesPHP | ImagickKernel addKernel() Function

PHP | ImagickKernel addKernel() Function

The ImagickKernel::addKernel() function is an inbuilt function in PHP which is used to attach another kernel to this kernel. Using this function we can attach multiple matrices to the same kernel and use whichever we want.

Syntax:

void ImagickKernel::addKernel( ImagickKernel $ImagickKernel )

Parameters: This function accepts a single parameter $ImagickKernel which holds the kernel.

Return Value: This function returns TRUE on success.

Exceptions: This function throws ImagickException on error.

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

Program 1:




<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
$matrix1 = [
    [-1, -1, -1],
    [4, 4, 4],
    [1, 1, 1],
];
  
$matrix2 = [
    [-1, 0, 0],
    [0, 0, 1],
    [-1, 0, 1],
];
  
// Create ImagickKernel objects from matrices
$kernel1 = ImagickKernel::fromMatrix($matrix1);
$kernel2 = ImagickKernel::fromMatrix($matrix2);
  
// Add the kernel
$kernel1->addKernel($kernel2);
  
// Apply filter from second attached kernel
$imagick->filter($kernel1->separate()[1]);
header("Content-Type: image/jpg");
echo $imagick->getImageBlob();
?>


Output:

Program 2:




<?php
  
// Create a new imagick object
$imagick = new Imagick(
  
$matrix1 = [
    [-1, -1, -1],
    [4, 4, 4],
    [1, 1, 1],
];
  
$matrix2 = [
    [-1, 0, 0],
    [0, 0, 1],
    [-1, 0, 1],
];
  
$matrix3 = [
    [-1, 1, 0],
    [0, 0, 1],
    [-1, 0, 1],
];
  
$matrix4 = [
    [0, 1, 0],
    [0, 0, 1],
    [-1, 0, 1],
];
  
// Create ImagickKernel objects from matrices
$kernel1 = ImagickKernel::fromMatrix($matrix1);
$kernel2 = ImagickKernel::fromMatrix($matrix2);
$kernel3 = ImagickKernel::fromMatrix($matrix3);
$kernel4 = ImagickKernel::fromMatrix($matrix4);
  
// Add the kernels
$kernel1->addKernel($kernel2);
$kernel1->addKernel($kernel3);
$kernel1->addKernel($kernel4);
  
$kernelList = $kernel1->separate();
  
echo 'Total number of attached kernels are: ';
echo count($kernelList);
?>


Output:

Total number of attached kernels are: 4

Reference: https://www.php.net/manual/en/imagickkernel.addkernel.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
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6783 POSTS0 COMMENTS
Nicole Veronica
11929 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11999 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6863 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS