Source for file ImageProcessing.func.php

  1. <?php
  2.     /**
  3.     * A set of basic functions for processing uploaded images.
  4.     * @author Richard Sharp
  5.     * @copyright 2005
  6.     * @version 2.0
  7.     * @package Images
  8.     */
  9.     
  10.      /**
  11.      * Returns information about the image in an associative array.
  12.      * @param string The full path to the image file.
  13.      * @return mixed Returns an array of information on success (type, width, height), false on failure.
  14.      */ 
  15.     function getImageInfo($sFile)
  16.     {
  17.         $aInfo=@getimagesize($sFile);
  18.         if(!$aInforeturn false;
  19.         
  20.         if($aInfo[2]==1$return["type"]="gif";
  21.         elseif($aInfo[2]==2$return["type"]="jpg";
  22.         elseif($aInfo[2]==3$return["type"]="png";
  23.         elseif($aInfo[2]==4$return["type"]="swf";
  24.         else return(false);
  25.         
  26.         $return["width"]=$aInfo[0];
  27.         $return["height"]=$aInfo[1];
  28.         return($return);
  29.     }
  30.     
  31.     
  32.      /**
  33.      * Creates a resized copy of an image file. Either the new height, the new width, or both can be provided. If only one is provided, the other is adjusted proportionately.
  34.      * @param string The source file.
  35.      * @param string The filename for the new image file that will be created. This should not include any extension.
  36.      * @param int The new height of the image.
  37.      * @param int The new width of the image.
  38.      * @param string The filename of the original image file. (If uploaded)
  39.      * @param string If set, a watermark will be placed on this image. This paramter is the full path to the watermark image. Must be a PNG file. The watermark should be in the lower-right of the image.
  40.      * @return mixed The image type (extension) of the newly created/copied image. False on error.
  41.      */ 
  42.     function resizeImage($sSourceFilename$sDestinationFilename$iTargetHeight 0$iTargetWidth 0$sOriginalFilename ""$sWatermarkImage="")
  43.     {
  44.         //check for required values
  45.         if!$sSourceFilename || !$sDestinationFilenamereturn(false);
  46.             
  47.         $aImageInfo getimagesize($sSourceFilename);
  48.         
  49.         $aTypes array(=> "gif"=>"jpg"=> "png"=> "swf");
  50.         
  51.         $dOriginalWidth $aImageInfo[0];
  52.         $dOriginalHeight $aImageInfo[1];
  53.         $sOriginalImageType $aTypes[$aImageInfo[2]];
  54.         
  55.         //if unable to determine the type from the getimagesize function, try from the original filename
  56.         if!$sOriginalImageType && $sOriginalFilename 
  57.         {
  58.             $arrNameParts explode("."strtolower($sOriginalFilename));
  59.             $ext $arrNameParts[count($arrNameParts1];
  60.             
  61.             switch $ext )
  62.             {
  63.                 case "gif":
  64.                     $sOriginalImageType "gif";
  65.                     break;
  66.                 case "jpg":
  67.                     $sOriginalImageType "jpg";
  68.                     break;
  69.                 case "jpeg":
  70.                     $sOriginalImageType "jpg";
  71.                     break;
  72.                 case "png":
  73.                     $sOriginalImageType "png";
  74.                     break;
  75.             }
  76.         }
  77.         
  78.         //if unable to determine the image type, or an invalid type, fail
  79.         if !$sOriginalImageType )
  80.             return(false);
  81.         
  82.         if(file_exists($sDestinationFilename.".".$sOriginalImageType)) 
  83.             unlink($sDestinationFilename.".".$sOriginalImageType);
  84.         
  85.         //if provided a new width or height, manipulate the image
  86.         if$iTargetHeight || $iTargetWidth 
  87.         {
  88.             if ($sOriginalImageType == "jpg"
  89.                 $oOriginalImage imagecreatefromjpeg($sSourceFilename);
  90.             elseif ($sOriginalImageType == "png"
  91.                 $oOriginalImage imagecreatefrompng($sSourceFilename);
  92.             elseif($sOriginalImageType == "gif"
  93.                 $oOriginalImage imagecreatefromgif($sSourceFilename);
  94.             
  95.             if($oOriginalImage)
  96.             {
  97.                 $dAspectRatio $dOriginalWidth/$dOriginalHeight;
  98.                 if($iTargetHeight && !$iTargetWidth)
  99.                 {
  100.                     $dNewHeight $iTargetHeight;
  101.                     $dNewHeight floor($dNewHeight $dAspectRatio);
  102.                 }
  103.                 elseif($iTargetWidth && !$iTargetHeight)
  104.                 {
  105.                     $dNewHeight $iTargetWidth;
  106.                     $dNewHeight floor($dNewHeight/$dAspectRatio);
  107.                 }
  108.                 else
  109.                 {
  110.                     $dNewHeight $iTargetHeight;
  111.                     $dNewHeight $iTargetWidth;
  112.                 }
  113.                 
  114.                 $oNewImage imagecreatetruecolor($dNewHeight$dNewHeight);
  115.                 
  116.                 //copy the old image to the new image with the new dimentions
  117.                 imagecopyresampled($oNewImage$oOriginalImage0000$dNewHeight$dNewHeight$dOriginalWidth$dOriginalHeight);
  118.                 
  119.                 if($sWatermarkImage)
  120.                 {
  121.                     //now, add the watermark
  122.                     $oWatermark @imagecreatefrompng($sWatermarkImage);
  123.                     
  124.                     if($oWatermark)
  125.                     {
  126.                         $iWatermarkWidth imagesx($oWatermark);
  127.                         $iWatermarkHeight imagesy($oWatermark);
  128.                         
  129.                         $iDestinationX $dNewHeight $iWatermarkWidth 5;
  130.                         $iDestinationY $dNewHeight $iWatermarkHeight 5;
  131.                         
  132.                         imagecopymerge($oNewImage$oWatermark$iDestinationX$iDestinationY00$iWatermarkWidth$iWatermarkHeight100);
  133.                     }
  134.                 }
  135.                 
  136.                 // if this was a GIF, and GIF support is enabled, create the new file as a GIF, otherwise create a JPEG.
  137.                 $ok=false;
  138.                 if $sOriginalImageType == "gif" && function_exists("imagegif") ) 
  139.                 {
  140.                     $sNewImageType "gif";
  141.                     $ok imagegif($oNewImage$sDestinationFilename.".".$sNewImageType);
  142.                 }
  143.                 if(!$ok)
  144.                 {
  145.                     $sNewImageType "jpg";
  146.                     $ok imagejpeg($oNewImage$sDestinationFilename.".".$sNewImageType100);
  147.                 }
  148.             }
  149.         }
  150.         
  151.         //if $ok is not set, either there was no re-sizing to be done, or it failed, so just copy the image
  152.         if(!$ok
  153.         {
  154.             $sNewImageType $sOriginalImageType;
  155.             $ok copy($sSourceFilename$sDestinationFilename.".".$sNewImageType);
  156.         }
  157.             
  158.         if(!$ok)
  159.             unset($sNewImageType);
  160.             
  161.         return($sNewImageType);
  162.         
  163.     }
  164. ?>