Oct
23
2010
PHP // web

PHP image resizer class

For the longest time I had been using a function I wrote some time ago to handle resizing of images through PHP. I decided it was time to convert it into a class and give it it’s own special place in my application workspace. It works so well I figured the least I could do is share with the world.

Download

Downlod the PHPImgSizer Zip File which includes the class.imgsizer.php file and some examples of usage. It also has some nice wallpapers I used to test it :-)

Requirements

This class requires PHP 5+ to run correctly along with support for the PHP GD Library.

Usage & Properties

The following is an example of the usage of this class:

require_once('class.imgsizer.php');

 $imgSizer = new imgSizer();
 $imgSizer->type = "width";
 $imgSizer->max = 100;
 $imgSizer->quality = 8;
 $imgSizer->square = true;
 $imgSizer->prefix = "sml_";
 $imgSizer->folder = "_sized/";

 // Single image ##################################################

 $imgSizer->image = "/PHP_ImageSizer/images/test.jpg";
 echo("<img src="" . $imgSizer->resize() . "" /><br /><br />");

 // Multiple images (loop) ########################################

 $arrImages[0] = "/PHP_ImageSizer/images/test1.jpg";
 $arrImages[1] = "/PHP_ImageSizer/images/test2.jpg";
 $arrImages[2] = "/PHP_ImageSizer/images/test3.jpg";

 foreach($arrImages as $img){
 $imgSizer->image = $img;
     echo("<img src="" . $imgSizer->resize() . "" /><br />");
 }

The following properties can be set for this class:

  • image – the full path to the image (from root, beginning slash required)
  • type – the type of crop (width or height based)
  • max – maximum pixels (width or height) based on the type property
  • quality – image quality for output (1-10)
  • square – whether the image should be square-cropped (true/false)
  • prefix – text that can be prepended to the output image file name
  • folder – new folder where the output image will be placed (requires trailing slash)

Thoughts & Conclusion

I’ve been using this code (in function format) for some time now. I consider it completely free and open-source; I just didn’t hassle with applying a license, figured this note was good enough. It works really well for creating image galleries on dynamic sites and has been tied into several standard jquery lightbox plugins.

The one thing to note (especially if you’re using large images) is that the first time the script runs against those images it may take a little while for it to create the resized versions, so if you upload a bunch of images you may have to wait 30 seconds or so on first load. After the images are created though you shouldn’t have any issues with load time.

It also works well for resizing images when uploaded. You can have it create the new images in a new folder or simply leave the ‘folder’ and ‘prefix’ propertie blank, ensure the directory the original resides in has write permissions, and simply overwrite the image with a resized version.

Leave a comment