
- Drop me a line! Contact me here.
Convolution Filters In GD
Image filters are very useful things to have available. They enable you to take and image, and transform it to be a new work very simply, even automatically in many cases. They're an essential part of any image processing application. But wouldn't it be useful to give your users the ability to filter images with a range of different filter effects automatically when they upload an image? Hell yes.
Convolution filters allow you to do just that. Convolution is a mathematical process in which you multiply a matrix of numbers with a matrix of data to get a single result.

The maths may look complex, but its really just multiplication and addition many times over.
The first step to convolving an image is to open it. This is simply done using the usual image opening functions, usually imagecreatefromjpeg(). Once we have it open we also need to create an output image to draw our results onto. Ideally you will need to create the destination image in true colour mode as the convolution process tends to generate a large number of colours. This means that this image code is unsuitable for use on servers using GD 1.6 or lower.
An image is made up of 3 or 4 seperate colour channels depending whether or not the image has an alpha transparency layer. In order to convolve an image we have to work on each channel seperately. I will skip over how the different channels are accessed as I have covered it in depth in a previous article.
In order to carry out the convolution we must loop through the pixels that make up the source image one by one convolving the pixel with its neighbours as we go, and drawing the results to our destination image.
The reason for starting at pixel 1 and ending on pixel imagesx($source)-1 is due to using a 3*3 convolution matrix. We don't want to convolve the pixels at the edges as this will give a messy border.
The next stage of the process is to extract the colour of the pixels we're dealing with. For a 3*3 matrix we need to get the colours of 9 pixels in total, the middle pixel which we're calculating the value for, and its neighbours whose values affect it. There are two ways to do this depending whether the source image is true colour or indexed colour. JPG images are true colour, so we'll use the method for that.
Convolution filters allow you to do just that. Convolution is a mathematical process in which you multiply a matrix of numbers with a matrix of data to get a single result.

The maths may look complex, but its really just multiplication and addition many times over.
The first step to convolving an image is to open it. This is simply done using the usual image opening functions, usually imagecreatefromjpeg(). Once we have it open we also need to create an output image to draw our results onto. Ideally you will need to create the destination image in true colour mode as the convolution process tends to generate a large number of colours. This means that this image code is unsuitable for use on servers using GD 1.6 or lower.
<?php
$source = imagecreatefromjpeg($file);
$image = imagecreatetruecolor(imagesx($source), imagesy($source));
?>
An image is made up of 3 or 4 seperate colour channels depending whether or not the image has an alpha transparency layer. In order to convolve an image we have to work on each channel seperately. I will skip over how the different channels are accessed as I have covered it in depth in a previous article.
In order to carry out the convolution we must loop through the pixels that make up the source image one by one convolving the pixel with its neighbours as we go, and drawing the results to our destination image.
<?php
for ($y=1;$y<imagesy($source)-1;$y++) {
for ($x=1;$x<imagesx($source)-1;$x++) {
//convolution process
}
}
?>
The reason for starting at pixel 1 and ending on pixel imagesx($source)-1 is due to using a 3*3 convolution matrix. We don't want to convolve the pixels at the edges as this will give a messy border.
The next stage of the process is to extract the colour of the pixels we're dealing with. For a 3*3 matrix we need to get the colours of 9 pixels in total, the middle pixel which we're calculating the value for, and its neighbours whose values affect it. There are two ways to do this depending whether the source image is true colour or indexed colour. JPG images are true colour, so we'll use the method for that.
Comments
Comments are not currently being accepted for this article.
Sidebar
- © Ooer.com Chris Neale 2007
- PHPGD.com
- Powered by PHP
- Database by MySQL
- DB Queries: 9
- DB Time: 0.0676 seconds
