advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Rate this item | 0 users have rated this item.
 Print Print
 
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.
Page 1 of 7
advertisement
  Next Page: Color Bitwise Operations
Page 1: IntroductionPage 5: Non-RGB Filters
Page 2: Color Bitwise OperationsPage 6: Predefined Filters in PHP
Page 3: Color-Altering FiltersPage 7: Developing Filters with the Convolution Technique
Page 4: Color-Altering Filters (continued) 
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES