RedVodkaJelly Logo

PHP Image Resize Class

POSTED AT 15:39 on 23rd May 2008

This is a PHP class that deals with resizing jpg, png and gif images. You just pass it the image you want to resize, a location to save the new image to and instructions on how to you want it resized and the class will do the rest.

Please note that your version of PHP must be compiled with the GD library in order for this class to work.

Source Code

  1. <?php
     
  2. /*
     
  3. * PHP Image Resize Class
     
  4. *
     
  5. * Class to deal with resizing images using PHP.
     
  6. * Will resize any JPG, GIF or PNG file.
     
  7. *
     
  8. * Written By Jacob Wyke - jacob@redvodkajelly.com - www.redvodkajelly.com
     
  9. *
     
  10. * LICENSE

     

  11. * ——-
     
  12. * Feel free to use this as you wish, just give me credit where credits due and drop me an email telling me what your using it for so I can check out all the cool ways its been used.
     
  13. *
     
  14. * USAGE
     
  15. * —–
     
  16. * To use this class simply call it with the following details:
     
  17. *
     
  18. *       Path to original image,
     
  19. *       Path to save new image,
     
  20. *       Resize type,
     
  21. *       Resize Data
     
  22. *    

     

  23. * The resize type can be one of four:
     
  24. *
     
  25. *       W   =   Width
     
  26. *       H   =   Height
     
  27. *       P   =   Percentage
     
  28. *       C   =   Custom

     

  29. *
     
  30. * All of these take integers except Custom that takes an array of two integers - for width and height.
     
  31. *
     
  32. *       $objResize = new RVJ_ImageResize(’myImage.png’, ‘myThumb.png’, ‘W’, ‘400′);
     
  33. *       $objResize = new RVJ_ImageResize(’myImage.jpg’, ‘myThumb.jpg’, ‘H’, ‘150′);
     
  34. *       $objResize = new RVJ_ImageResize(’myImage.gif’, ‘myThumb.gif’, ‘P’, ‘50′);
     
  35. *       $objResize = new RVJ_ImageResize(’myImage.png’, ‘myThumb.png’, ‘C’, array(’400′, ‘300′));
     
  36. *
     
  37. * When resizing by width, height and percentage, the image will keep its original ratio. Custom will simply resizes the image to whatever values you want - without keeping the original ratio.
     
  38. *
     
  39. * The class can handle jpg, png and gif images.
     
  40. *
     
  41. * The class will always save the image that it resizes, however you can also have it display the image:

     

  42. *
     
  43. *       $objResize->showImage($resize->im2);
     
  44. *
     
  45. * The class holds the original image in the variable ‘im’ and the new image in ‘im2′. Therefore the code above will show the newly created image.
     
  46. *
     
  47. * You can get information about the image by doing the following:
     
  48. *
     
  49. *       print_r($objResize->findResourceDetails($objResize->resOriginalImage));
     
  50. *       print_r($objResize->findResourceDetails($objResize->resResizedImage));

     

  51. *
     
  52. * This will be useful if you wish to retrieve any details about the images.
     
  53. * By default the class will stop you from enlarging your images (or else they will look grainy) and if you want to do this you must turn off the protection mode by passing a 5th parameter 
  54. *      $objResize = new RVJ_ImageResize(’myImage.gif’, ‘myEnlargedImage.gif’, ‘P’, ‘200′, false); 
  55. *
     
  56. */
     
  57.  
  58. class RVJ_ImageResize {
     
  59.  
  60.    var $strOriginalImagePath;
     
  61.    var $strResizedImagePath;
     
  62.    var $arrOriginalDetails;
     
  63.    var $arrResizedDetails;
     
  64.    var $resOriginalImage;

     

  65.    var $resResizedImage;
     
  66.    var $boolProtect = true; 
  67.  
  68.    /* 
  69.    * 
  70.    *   @Method:      __constructor 
  71.    *   @Parameters:   5
     
  72.    *   @Param-1:      strPath - String - The path to the image
     
  73.    *   @Param-2:      strSavePath - String - The path to save the new image to

     

  74.    *   @Param-3:      strType - String - The type of resize you want to perform
     
  75.    *   @Param-4:      value - Number/Array - The resize dimensions 
  76.    *   @Param-5:      boolProect - Boolen - Protects the image so that it doesnt resize an image if its already smaller 
  77.    *   @Description:   Calls the RVJ_Pagination method so its php 4 compatible 
  78.    * 
  79.    */
     
  80.    function __constructor($strPath, $strSavePath, $strType = ‘W’, $value = ‘150′, $boolProtect = true){
     
  81.       $this->RVJ_ImageResize($strPath, $strSavePath, $strType, $value);

     

  82.    }
     
  83.    
     
  84.    /* 
  85.    * 
  86.    *   @Method:      RVJ_ImageResize 
  87.    *   @Parameters:   5
     
  88.    *   @Param-1:      strPath - String - The path to the image
     
  89.    *   @Param-2:      strSavePath - String - The path to save the new image to
     
  90.    *   @Param-3:      strType - String - The type of resize you want to perform

     

  91.    *   @Param-4:      value - Number/Array - The resize dimensions 
  92.    *   @Param-5:      boolProect - Boolen - Protects the image so that it doesnt resize an image if its already smaller 
  93.    *   @Description:   Calls the RVJ_Pagination method so its php 4 compatible 
  94.    * 
  95.    */
     
  96.    function RVJ_ImageResize($strPath, $strSavePath, $strType = ‘W’, $value = ‘150′, $boolProtect = true){
     
  97.       //save the image/path details
     
  98.       $this->strOriginalImagePath = $strPath;
     
  99.       $this->strResizedImagePath = $strSavePath;

     

  100.       $this->boolProtect = $boolProtect; 
  101.       
     
  102.       //get the image dimensions
     
  103.       $this->arrOriginalDetails = getimagesize($this->strOriginalImagePath);
     
  104.       $this->arrResizedDetails = $this->arrOriginalDetails;
     
  105.       
     
  106.       //create an image resouce to work with
     
  107.       $this->resOriginalImage = $this->createImage($this->strOriginalImagePath);
     
  108.                                           

     

  109.       //select the image resize type
     
  110.       switch(strtoupper($strType)){
     
  111.          case ‘P’:
     
  112.             $this->resizeToPercent($value);
     
  113.             break;
     
  114.          case ‘H’:
     
  115.             $this->resizeToHeight($value);
     
  116.             break;
     
  117.          case ‘C’:
     
  118.             $this->resizeToCustom($value);
     
  119.             break;
     
  120.          case ‘W’:
     
  121.          default:
     
  122.             $this->resizeToWidth($value);

     

  123.             break;
     
  124.       } 
  125.    }
     
  126.  
  127.    /* 
  128.    * 
  129.    *   @Method:      findResourceDetails 
  130.    *   @Parameters:   1 
  131.    *   @Param-1:      resImage - Resource - The image resource you want details on 
  132.    *   @Description:   Returns an array of details about the resource identifier that you pass it 
  133.    * 
  134.    */

     

  135.    function findResourceDetails($resImage){
     
  136.       //check to see what image is being requested
     
  137.       if($resImage==$this->resResizedImage){                              
     
  138.          //return new image details
     
  139.          return $this->arrResizedDetails;
     
  140.       }else{
     
  141.          //return original image details
     
  142.          return $this->arrOriginalDetails;
     
  143.       }
     
  144.    }
     
  145.  
  146.    /* 
  147.    * 
  148.    *   @Method:      updateNewDetails    
  149.    *   @Parameters:   0 
  150.    *   @Description:   Updates the width and height values of the resized details array 
  151.    * 
  152.    */

     

  153.    function updateNewDetails(){
     
  154.       $this->arrResizedDetails[0] = imagesx($this->resResizedImage);
     
  155.       $this->arrResizedDetails[1] = imagesy($this->resResizedImage);
     
  156.    }
     
  157.       
     
  158.    /* 
  159.    * 
  160.    *   @Method:      createImage 
  161.    *   @Parameters:   1 
  162.    *   @Param-1:      strImagePath - String - The path to the image 
  163.    *   @Description:   Created an image resource of the image path passed to it 
  164.    * 
  165.    */

     

  166.    function createImage($strImagePath){
     
  167.       //get the image details
     
  168.       $arrDetails = $this->findResourceDetails($strImagePath);
     
  169.        
  170.       //choose the correct function for the image type 
  171.       switch($arrDetails['mime']){
     
  172.          case ‘image/jpeg’:
     
  173.             return imagecreatefromjpeg($strImagePath);
     
  174.             break;
     
  175.          case ‘image/png’:
     
  176.             return imagecreatefrompng($strImagePath);
     
  177.             break;
     
  178.          case ‘image/gif’:
     
  179.             return imagecreatefromgif($strImagePath);
     
  180.             break;
     
  181.       }
     
  182.    }

     

  183.       
     
  184.    /* 
  185.    * 
  186.    *   @Method:      saveImage 
  187.    *   @Parameters:   1 
  188.    *   @Param-1:      numQuality - Number - The quality to save the image at 
  189.    *   @Description:   Saves the resize image 
  190.    * 
  191.    */

     

  192.    function saveImage($numQuality = 85){
     
  193.       switch($this->arrResizedDetails['mime']){
     
  194.          case ‘image/jpeg’:
     
  195.             imagejpeg($this->resResizedImage, $this->strResizedImagePath, $numQuality);
     
  196.             break;
     
  197.          case ‘image/png’:
     
  198.             // imagepng = [0-9] (not [0-100])          
  199.             imagepng($this->resResizedImage, $this->strResizedImagePath, 7);
     
  200.             break;
     
  201.          case ‘image/gif’:
     
  202.             imagegif($this->resResizedImage, $this->strResizedImagePath, $numQuality);

     

  203.             break;
     
  204.       }
     
  205.    }
     
  206.    
     
  207.    /* 
  208.    * 
  209.    *   @Method:      showImage 
  210.    *   @Parameters:   1 
  211.    *   @Param-1:      resImage - Resource - The resource of the image you want to display 
  212.    *   @Description:   Displays the image resouce on the screen 
  213.    * 
  214.    */

     

  215.    function showImage($resImage){
     
  216.       //get the image details
     
  217.       $arrDetails = $this->findResourceDetails($resImage);
     
  218.        
  219.       //set the correct header for the image we are displaying 
  220.       header("Content-type: ".$arrDetails['mime']);
     
  221.        
  222.       switch($arrDetails['mime']){
     
  223.          case ‘image/jpeg’:
     
  224.             return imagejpeg($resImage);
     
  225.             break;
     
  226.          case ‘image/png’:
     
  227.             return imagepng($resImage);
     
  228.             break;
     
  229.          case ‘image/gif’:
     
  230.             return imagegif($resImage);

     

  231.             break;
     
  232.       }
     
  233.    }
     
  234.    
     
  235.    /* 
  236.    * 
  237.    *   @Method:      destroyImage 
  238.    *   @Parameters:   1 
  239.    *   @Param-1:      resImage - Resource - The image resource you want to destroy 
  240.    *   @Description:   Destroys the image resource and so cleans things up 
  241.    * 
  242.    */

     

  243.    function destroyImage($resImage){
     
  244.       imagedestroy($resImage);
     
  245.    }
     
  246.    
     
  247.    /* 
  248.    * 
  249.    *   @Method:      _resize 
  250.    *   @Parameters:   2 
  251.    *   @Param-1:      numWidth - Number - The width of the image in pixels 
  252.    *   @Param-2:      numHeight - Number - The height of the image in pixes 
  253.    *   @Description:   Resizes the image by creatin a new canvas and copying the image over onto it. DONT CALL THIS METHOD DIRECTLY - USE THE METHODS BELOW 
  254.    * 
  255.    */

     

  256.    function _resize($numWidth, $numHeight){
     
  257.       //check for image protection 
  258.       if($this->_imageProtect($numWidth, $numHeight)){    
  259.          if($this->arrOriginalDetails['mime']==’image/gif’){
     
  260.             //GIF image
     
  261.             $this->resResizedImage = imagecreate($numWidth, $numHeight);
     
  262.          }else if($this->arrOriginalDetails['mime']==’image/jpeg’){
     
  263.             //JPG image
     
  264.             $this->resResizedImage = imagecreatetruecolor($numWidth, $numHeight);
     
  265.          }else if($this->arrOriginalDetails['mime']==’image/png’){ 
  266.             //PNG image 
  267.             $this->resResizedImage = imagecreatetruecolor($numWidth, $numHeight); 
  268.             imagecolortransparent($this->resResizedImage, imagecolorallocate($this->resResizedImage, 0, 0, 0)); 
  269.             imagealphablending($this->resResizedImage, false); 
  270.             imagesavealpha($this->resResizedImage, true); 
  271.          } 
  272.     
  273.          //update the image size details 
  274.          $this->updateNewDetails(); 
  275.        
  276.          //do the actual image resize 
  277.          imagecopyresized($this->resResizedImage, $this->resOriginalImage, 0, 0, 0, 0, $numWidth, $numHeight, $this->arrOriginalDetails[0], $this->arrOriginalDetails[1]);

     

  278.  
  279.          //saves the image 
  280.          $this->saveImage(); 
  281.       } 
  282.    }
     
  283.  
  284.    /* 
  285.    * 
  286.    *   @Method:      _imageProtect 
  287.    *   @Parameters:   2 
  288.    *   @Param-1:      numWidth - Number - The width of the image in pixels 
  289.    *   @Param-2:      numHeight - Number - The height of the image in pixes 
  290.    *   @Description:   Checks to see if we should allow the resize to take place or not depending on the size the image will be resized to 
  291.    * 
  292.    */    
  293.    function _imageProtect($numWidth, $numHeight){ 
  294.       if($this->boolProtect AND ($numWidth > $this->arrOriginalDetails[0] OR $numHeight > $this->arrOriginalDetails[1])){ 
  295.          return 0; 
  296.       } 
  297.        
  298.       return 1; 
  299.    } 
  300.    

     

  301.    /* 
  302.    * 
  303.    *   @Method:      resizeToWidth 
  304.    *   @Parameters:   1 
  305.    *   @Param-1:      numWidth - Number - The width to resize to in pixels 
  306.    *   @Description:   Works out the height value to go with the width value passed, then calls the resize method. 
  307.    * 
  308.    */
     
  309.    function resizeToWidth($numWidth){

     

  310.       $numHeight=(int)(($numWidth*$this->arrOriginalDetails[1])/$this->arrOriginalDetails[0]);
     
  311.       $this->_resize($numWidth, $numHeight);   
     
  312.    }
     
  313.  
  314.    /* 
  315.    * 
  316.    *   @Method:      resizeToHeight 
  317.    *   @Parameters:   1 
  318.    *   @Param-1:      numHeight - Number - The height to resize to in pixels 
  319.    *   @Description:   Works out the width value to go with the height value passed, then calls the resize method. 
  320.    * 
  321.    */

     

  322.    function resizeToHeight($numHeight){
     
  323.       $numWidth=(int)(($numHeight*$this->arrOriginalDetails[0])/$this->arrOriginalDetails[1]);
     
  324.       $this->_resize($numWidth, $numHeight);   
     
  325.    }
     
  326.    
     
  327.    /* 
  328.    * 
  329.    *   @Method:      resizeToPercent 
  330.    *   @Parameters:   1 
  331.    *   @Param-1:      numPercent - Number - The percentage you want to resize to 
  332.    *   @Description:   Works out the width and height value to go with the percent value passed, then calls the resize method. 
  333.    * 
  334.    */

     

  335.    function resizeToPercent($numPercent){
     
  336.       $numWidth = (int)(($this->arrOriginalDetails[0]/100)*$numPercent);
     
  337.       $numHeight = (int)(($this->arrOriginalDetails[1]/100)*$numPercent);
     
  338.       $this->_resize($numWidth, $numHeight);   
     
  339.    }
     
  340.  
  341.    /* 
  342.    * 
  343.    *   @Method:      resizeToCustom 
  344.    *   @Parameters:   1 
  345.    *   @Param-1:      size - Number/Array - Either a number of array of numbers for the width and height in pixels 
  346.    *   @Description:   Checks to see if array was passed and calls the resize method with the correct values. 
  347.    * 
  348.    */

     

  349.    function resizeToCustom($size){
     
  350.       if(!is_array($size)){
     
  351.          $this->_resize((int)$size, (int)$size);
     
  352.       }else{
     
  353.          $this->_resize((int)$size[0], (int)$size[1]);
     
  354.       }
     
  355.    }
     
  356.    
     
  357. }
     
  358.  
  359. ?>