A PHP Image Resizer Class

0
525
PHP image resize

Looking for a utility to resize your images? Here’s a PHP class leveraging the GD extension to make your life easier.

Please make sure that you have the GD 2 library installed on your server. This is a PHP extension that will allow you to do image manipulation.

The code below handles manipulation of JPEG, GIF, and PNG image file types only. Feel free to use this class to inspire you to create useful image utilities. For example, use this class to create an automated batch image processor to generate thumbnails in different sizes.

  1. /*******************************************************
  2. File:       ImageResizer.php
  3. Purpose:    An image resizer class
  4. Company:    Kobashi Computing
  5. Programmer: Kerry Kobashi
  6.  
  7. Notes:
  8.   Resizes JPEG, GIF, and PNG file types
  9.   Default output is PNG
  10. *******************************************************/
  11.  
  12. define("IMAGE_RESIZER_FILE_DOESNT_EXIST", 1);
  13. define("IMAGE_RESIZER_FILE_NO_SIZE", 2);
  14. define("IMAGE_RESIZER_FILE_SIZE_TOO_BIG", 3);
  15.  
  16. define("IMAGE_RESIZER_JPEG_QUALITY_MIN", 0);
  17. define("IMAGE_RESIZER_JPEG_QUALITY_MAX", 100);
  18. define("IMAGE_RESIZER_DEFAULT_JPEG_QUALITY", 80);
  19.  
  20. define("IMAGE_RESIZER_DEFAULT_OUTPUT_DIR", "./output/");
  21. define("IMAGE_RESIZER_MAX_FILE_SIZE", 10240000);
  22.  
  23. class ImageResizer {
  24.   var $image;
  25.   var $jpegQuality;
  26.   var $outputDir;
  27.   var $imageType;
  28.  
  29.   function ImageResizer() {
  30.     $this->image = NULL;
  31.     $this->imageType = IMAGETYPE_PNG;
  32.     $this->jpegQuality = IMAGE_RESIZER_DEFAULT_JPEG_QUALITY;
  33.     $this->outputDir = IMAGE_RESIZER_DEFAULT_OUTPUT_DIR;
  34.   }
  35.  
  36.   function SetOutputDirectory($outputDir) {
  37.     $this->outputDir = $outputDir;
  38.     if (strlen($outputDir) > 0) {
  39.       $lastChar = substr($outputDir, -1, 1);
  40.       if ($lastChar != DIRECTORY_SEPARATOR)
  41.         $this->outputDir .= DIRECTORY_SEPARATOR;
  42.     }
  43.   }
  44.  
  45.   function GetOutputDirectory() {
  46.     return($this->outputDir);
  47.   }
  48.  
  49.   function SetJpegQuality($quality) {
  50.     $this->jpegQuality = DEFAULT_JPEG_QUALITY;
  51.     if ($quality >= JPEG_QUALITY_MIN && $quality <= JPEG_QUALITY_MAX)
  52.       $this->jpegQuality = $quality;
  53.   }
  54.  
  55.   function GetJpegQuality() {
  56.     return($this->jpegQuality);
  57.   }
  58.  
  59.   function SetOutputFileType($fileType) {
  60.     switch(strtolower($fileType)) {
  61.       case "gif":
  62.         $this->imageType = IMAGETYPE_GIF;
  63.         break;
  64.       case "jpg":
  65.         $this->imageType = IMAGETYPE_JPEG;
  66.         break;
  67.       case "png":
  68.       default:
  69.         $this->imageType = IMAGETYPE_PNG;
  70.         break;
  71.     }
  72.   }
  73.  
  74.   function Open($filename) {
  75.     if (!file_exists($filename))
  76.       return(IMAGE_RESIZER_FILE_DOESNT_EXIST);
  77.     $fileSize = filesize($filename);
  78.     if ($fileSize == 0)
  79.       return(IMAGE_RESIZER_FILE_NO_SIZE);
  80.     if ($fileSize > IMAGE_RESIZER_MAX_FILE_SIZE)
  81.       return(IMAGE_RESIZER_FILE_SIZE_TOO_BIG);
  82.  
  83.     $sizeInfo = getimagesize($filename);
  84.     $imageType = $sizeInfo[2];
  85.     switch($imageType) {
  86.       case IMAGETYPE_GIF:
  87.         $this->image = imagecreatefromgif($filename);
  88.         break;
  89.       case IMAGETYPE_JPEG:
  90.         $this->image = imagecreatefromjpeg($filename);
  91.         break;
  92.       case IMAGETYPE_PNG:
  93.         $this->image = imagecreatefrompng($filename);
  94.         break;
  95.       default:
  96.         $this->image = NULL;
  97.         break;
  98.     }
  99.   }
  100.  
  101.   function Save($outputPath) {
  102.     switch($this->imageType) {
  103.       case IMAGETYPE_GIF:
  104.         imagegif($this->image, $outputPath);
  105.         break;
  106.       case IMAGETYPE_JPEG:
  107.         imagejpeg($this->image, $outputPath, $this->jpegQuality);
  108.         break;
  109.       case IMAGETYPE_PNG:
  110.       default:
  111.         imagepng($this->image, $outputPath);
  112.         break;
  113.     }
  114.   }
  115.  
  116.   function GetWidth() {
  117.     if (!is_null($this->image))
  118.       return(imagesx($this->image));
  119.     return(0);
  120.   }
  121.  
  122.   function GetHeight() {
  123.     if (!is_null($this->image))
  124.       return(imagesy($this->image));
  125.     return(0);
  126.   }
  127.  
  128.   function Resize($newWidth, $newHeight) {
  129.     $image = imagecreatetruecolor($newWidth, $newHeight);
  130.     $width = $this->GetWidth();
  131.     $height = $this->GetHeight();
  132.     imagecopyresampled($image, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
  133.     $this->image = $image;
  134.   }
  135.  
  136.   function ResizeByHeight($height) {
  137.     // scale width in proportion
  138.     $heightRatio = $height / $this->GetHeight();
  139.     $width = $this->GetWidth() * $heightRatio;
  140.     $this->Resize((int) $width, (int) $height);
  141.   }
  142.  
  143.   function ResizeByWidth($width) {
  144.     // scale height in proportion
  145.     $widthRatio = $width / $this->GetWidth();
  146.     $height = (int) $this->Getheight() * $widthRatio;
  147.     $this->Resize((int) $width, (int) $height);
  148.   }
  149.  
  150.   function ResizeByPercent($pct) {
  151.     // scale width and height in proportion by percent
  152.     $width = $this->GetWidth() * $pct/100.0;
  153.     $height = $this->Getheight() * $pct/100.0; 
  154.     $this->Resize((int) $width, (int) $height);
  155.   }
  156. }

Of Interest