Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

How To Create and Apply Image Filters in PHP

Discover how image filters work, how they can spice up your images, how to take advantage of them in PHP, and how to create custom filters that go far beyond the built-in filter effects.


advertisement
ost likely, the first time you heard about image filters was when you had to edit your vacation pictures to remove the "red eye" effect or an undesirable face. For such basic purposes, a little practice and some specialized software such as Adobe Photoshop or Paint Shop Pro is sufficient to end up with great pictures. But if you want to go beyond the basics you need to understand what a picture actually is, what the main algorithms dedicated to image processing are, and how a programming language, as PHP per example, can help you. Images are wonderfully malleable, and after you understand those points, you can develop your own custom image filters with PHP. This article covers:

  • Introduction to the GD library, which provides PHP support for image processing
  • Creating custom image filters by modifying pixel colors
  • Implementing the "convolution" technique
  • Creating custom image filters by modifying pixel positions
Author's Note: All the examples apply filters to the JPG image (tree.jpg) shown in Figure 1.

 
Figure 1. Tree at Sunrise: This single image is used as the example for all the different image filters you'll see in this article.

Introduction to the GD Library
If you are using PHP 4.3.0, or newer, then the GD library is already available; otherwise, you have to download it and install it manually. Because it's is an extension you must activate it in php.ini by uncommenting the following line (just delete the leading semicolon):

;extension=php_gd2.dll

Author's Note: After making the modification you must restart the web server.

To see the GD library version you may use this short PHP script:

<?php phpinfo(); ?>

After enabling GD support, if you look at the GD section of php.ini you should see something like Figure 2.

 
Figure 2. The GD Library Version: Here's how the GD section of php.ini should look with GD support enabled.
Creating and Destroying Images in PHP
The main PHP functions for creating an image are:

  • resource imagecreate(int $width, int $height): This function returns a blank image that supports only 256 colors. The two arguments represent the width and height of the image.
  • resource imagecreatetruecolor(int $width, int $height): This function returns a blank image that supports any number of colors. The two arguments represent the width and height of the image.
To destroy an image use the following function:

  • bool imagedestroy(resource $image): This function frees the memory used by the image.
Assigning Pixel Colors
To assign a specific color to a pixel, use the RGBA color model and the following PHP functions:

  • int imagecolorallocate(resource $image, int $red, int $green, int $blue): This function returns a color identifier representing the color composed of the given RGB components.
  • int imagecolorallocatealpha (resource $image, int $red, int $green, int $blue, int $alpha): This function behaves like imagecolorallocate, but also specifies a transparency (alpha) parameter.
To de-allocate a color previously allocated with one of the functions described above, use this function:



bool imagecolordeallocate (resource $image, int $color)

Creating Images
A set of PHP functions lets you create new images from a source image, which is useful when you want to modify an existing image. The functions create image identifiers from GIF, JPEG, and PNG images:

  • resource imagecreatefromgif(string $filename): This function returns an image identifier representing the GIF image obtained from the given file.
  • resource imagecreatefromjpeg(string $filename)
  • resource imagecreatefrompng(string $filename)
Saving Images
You can save images into a file or send them directly to a browser by using the following GD functions:

  • bool imagejpeg(resource $image [, string $filename [, int $quality ]]): This function outputs or saves the given JPEG image.
  • bool imagepng (resource $image [, string $filename [, int $quality [, int $filters ]]])
  • bool imagegif (resource $image [, string $filename ])


For sending images directly to browser you have to set the "Content-type" header as shown in the following example (for GIFs, PNGs, or other formats just place the right MIME type as the value of the header):

//EXAMPLE header("Content-type: image/jpeg"); imagejpeg($image);

Getting Image Dimensions
For determine the width and height of an image you can use one of these functions:

  • int imagesx (resource $image): This function returns the width of the given image resource.
  • int imagesy (resource $image): This function returns the height of the given image resource.
  • array getimagesize (string $filename [, array &$imageinfo ]): This function returns an array that contains a set of image parameters. Index 0 stores the width, index 1 stores the height and the rest of indexes contain other parameters.
Copying Portions of Images
To copy only a portion (sub-rectangle) of an image, use the imagecopy function. Its prototype is:

  • bool imagecopy (resource $dst_im, resource $src_im, int $dst_x, int $dst_y, int $src_x, int $src_y, int $src_w, int $src_h): This function copies a piece from $src_im image into $dst_im image starting at the $src_x and $src_y coordinates, and with $src_w width and $src_h height. It places the copied portion at the $dst_x and $dst_y coordinates.


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap