The Image class




The Image class is simply put a class with methods to make it easier to mainpulate images: Creating thumbnails, scaling, cropping and so on. To have any kind of use of this module the PHP Image extension needs to be installed. In most cases I believe it is by default.

Let's get on it!

Using the Image module

The first thing you need to is importing the Image module. This is done like this (in all examples I presume PLib.php is included):

1 line of PHP
  1. PLib::Import('Graphics.Image');

In the Graphics namespace there is two static members that you can set that will affect the quality/compression of PNG and JPEG images. By default the JPEG quality is set to 80 and the PNG compression is set to level 9. If you want other settings you can set these members before you manipulate your images:

3 lines of PHP
  1. PLib::Import('Graphics.Image'); 
  2. Graphics::$JPEG_QUALITY = 90; 
  3. Graphics::$PNG_QUALITY  = 1;

The Graphics namespace also has one static helper methods that can be useful to know about - Hex2RGB - which simply convert a hexadecimal color to RGB values and returns an associative array with three keys: r, g, b:

16 lines of PHP
  1. PLib::Import('Graphics.Image'); 
  2. $hex1 = "0x445566"; 
  3. $hex2 = "#445566"; 
  4. $hex3 = "456"; 
  5.  
  6. print_r(Graphics::Hex2RGB($hex1)); 
  7. print_r(Graphics::Hex2RGB($hex2)); 
  8. print_r(Graphics::Hex2RGB($hex3)); 
  9.  
  10. //! The result for all three variants will be
  11. Array 
  12. ( 
  13.     [r] => 68 
  14.     [g] => 85 
  15.     [b] => 102 
  16. )

Next: Manipulating images »