How to apply a 3×3 convolution matrix using imageconvolution() in PHP?


imageconvolution() is an inbuilt function in PHP that is used to apply a 3×3 convolution matrix, using the coefficient and offset in the image.

Syntax

bool imageconvolution ( $image, $matrix, $div, $offset)

Parameters

imageconvolution() takes four parameters: $image, $matrix, $div, and $offset.

  • $image − This parameter is used to create the size of the image by using an image creation function such as imagecreatetruecolor().

  • $matrix − This parameter contains an array of 3×3 matrix of floats.

  • $div − It is used for normalization.

  • $offset − This parameter is used to set the color offset.

Return Values

imageconvolution() returns True on success and False on failure.

Example 1

<?php
   // load the PNG image by using imagecreatefrompng function.
   $image = imagecreatefrompng('C:\xampp\htdocs\Images\img59.png');
   
   // Applied the 3X3 array matrix
   $matrix = array(
      array(2, 0, 0),
      array(0, -1, 0),
      array(0, 0, -1)
   );
   // imageconvolution function to modify image elements
   imageconvolution($image, $matrix, 1, 127);

   // show the output image in the browser
   header('Content-Type: image/png');
   imagepng($image, null, 9);
?>

Output

Input PNG image before using imageconvolution() function

Output PNG image after using imageconvolution() function

Example 2

<?php
   $image = imagecreatetruecolor(700, 300);
   
   // Writes the text and apply a gaussian blur on the image
   imagestring($image, 50, 25, 8, 'Gaussian Blur Text image', 0x00ff00);
   $gaussian = array(
      array(1.0, 2.0, 1.0),
      array(2.0, 4.0, 2.0),
      array(1.0, 2.0, 1.0)
   );
   imageconvolution($image, $gaussian, 16, 0);

   // Rewrites the text for comparison
   imagestring($image, 15, 20, 18, 'Gaussian Blur Text image', 0x00ff00);
   header('Content-Type: image/png');
   imagepng($image, null, 9);
?>

Output

Updated on: 09-Aug-2021

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements