PHP | Imagick filter() Function

The Imagick::filter() function is an inbuilt function in PHP which is used to apply custom convolution kernel to the image. A kernel, convolution matrix, or mask is a small matrix. It is used for blur, sharpen, embossing, edge detection, and many more.

Syntax:

bool Imagick::filter( $ImagickKernel, $channel = Imagick::CHANNEL_UNDEFINED )

Parameters: This function accepts two parameters as mentioned above and described below:



  • $ImagickKernel: It is a kernel or a linked series of multiple kernels that are used to be apply on the image.
  • $channel: It is a constant that is used on basis of the modes. It is of integer type and the default value of channel is Imagick::CHANNEL_DEFAULT.

Return Value: It returns true when the filter to the image is successfully applied.

Below program illustrates the Imagick::filter() function in PHP:

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declare an imagick object
$imagick = new Imagick(
  
// Declare convolution matrix
$matrix = [
    [-1, 0, -1],
    [0,  5,  0],
    [-1, 0, -1],
];
       
$kernel = \ImagickKernel::fromMatrix($matrix);
  
$strength = 0.5;
  
// Scaling the image on kernel
$kernel->scale($strength, \Imagick::NORMALIZE_KERNEL_VALUE);
  
// Use addUnityKernel() function
$kernel->addUnityKernel(1 - $strength);
   
// Use filter() function 
$imagick->filter($kernel);
  
header("Content-Type: image/jpg");
  
// Display the output image
echo $imagick->getImageBlob();
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/imagick.filter.php



My Personal Notes arrow_drop_up

Im a final year MCA student at Panjab University, Chandigarh, one of the most prestigious university of India I am skilled in various aspects related to Web Development and AI I have worked as a freelancer at upwork and thus have knowledge on various aspects related to NLP, image processing and web

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.