Source for file ViewImage.php

  1. <?php
  2.     /**
  3.     * This script serves images to the client browser. The images are re-sized on the fly if necessary, or served from a cache.
  4.     * The image name is passed in a GET variable named ImageName.
  5.     * Possible formats for ImageName:
  6.     * ###.<type> - ###=Content ID
  7.     * ###1-###2.<type> - ###1=Content ID ###2=Target Width
  8.     * ###1-###2-###3.<type> - ###1=Content ID ###2=Max Width ###3=Max Height
  9.     * 
  10.     * @author Richard Sharp
  11.     * @copyright 2008-2010
  12.     * @version 1.5
  13.     * @package Images
  14.     */
  15.     
  16.     //if set, display the "Image Not Found" image
  17.     $bInvalidImage=false;
  18.     
  19.     //parse the received image name
  20.     //get the file extension
  21.     $sImageName=$_GET["ImageName"];
  22.     $aImageNameParts=explode(".",$sImageName);
  23.     if(count($aImageNameParts)!=2$bInvalidImage=true;
  24.     $sImageType=strtolower($aImageNameParts[1]);
  25.     
  26.     //determine the content mime type to send in the header
  27.     if($sImageType=="jpg"$sContentType="jpeg";
  28.     elseif($sImageType=="gif"$sContentType="gif";
  29.     elseif($sImageType=="png"$sContentType="png";
  30.     else $bInvalidImage=true;
  31.     
  32.     //send some headers for cache control
  33.     $iExpiresSeconds 60 60 24 30//30 days
  34.     $sExpiresGMT gmdate("D, d M Y H:i:s"time($iExpiresSeconds )." GMT";
  35.     $sModifiedGMT gmdate("D, d M Y H:i:s"time((3600 * -24 365) )." GMT";
  36.     
  37.     @header("Expires: $sExpiresGMT");
  38.     @header("Pragma: cache");
  39.     @header("Cache-Control: public, max-age=$iExpiresSeconds");
  40.     
  41.     if(!$bInvalidImage)
  42.     {
  43.         // pull the part of the filename before the dot
  44.         $sImageName=$aImageNameParts[0];
  45.         
  46.         //if there are hyphens, the image will need to be resized.
  47.         if(substr_count($sImageName,"-")>0)
  48.         {
  49.             $aNameParts=explode("-",$sImageName);
  50.             $sImageName=$aNameParts[0];
  51.             $iTargetWidth=$aNameParts[1];
  52.             $iTargetHeight=$aNameParts[2];
  53.         }
  54.         
  55.         //get the info on this image from the database
  56.         $query='SELECT Image,ClientID
  57.         FROM Content
  58.         WHERE ID='.preg_replace("/[^0-9]/","",$sImageName).'
  59.         AND ImageType="'.$oDB->Escape(strtolower($sImageType)).'"
  60.         ';
  61.         $result=$oDB->Select($query);
  62.         if(!$result[0]$bInvalidImage=true;
  63.     }
  64.     
  65.     //if the image in anvalid, serve the "Not Found" image
  66.     if($bInvalidImage)
  67.     {
  68.         $sFilePath='/var/www/vhosts/somedomain.com/httpdocs/images';
  69.         $sImageFilename=$sFilePath.'/image_not_available_250_250.jpg';
  70.         $sContentType="jpeg";
  71.         $sImageType="jpg";
  72.     }
  73.     else
  74.     {
  75.         $sFilePath='/var/www/vhosts/somedomain.com/httpsdocs/content/'.$result[0]["ClientID"];
  76.         $sImageFilename=$sFilePath.'/'.$result[0]["Image"];
  77.         $sCachedFilename=$sImageFilename.'-'.$iTargetWidth.'X'.$iTargetHeight;
  78.     }
  79.     
  80.     //if a width or height is specified, re-size and output
  81.     if($iTargetHeight || $iTargetWidth //we must manipulate this image
  82.     {
  83.         //first, see if there is a cached version of this image in this size
  84.         if($sCachedFilename)
  85.         {
  86.             if(file_exists($sCachedFilename))
  87.             {
  88.                 $iFileModificationTime=filemtime($sCachedFilename);
  89.                 $sModifiedGMT gmdate("D, d M Y H:i:s"$iFileModificationTime )." GMT";
  90.                 $etag md5_file($sCachedFilename);
  91.                 @header("Last-Modified: $sModifiedGMT");
  92.                 @header("Etag: $etag");
  93.                 
  94.                 //if not modified, send a 304 not modified and end
  95.                 if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']== $iFileModificationTime || trim($_SERVER['HTTP_IF_NONE_MATCH']== $etag
  96.                 
  97.                     header("HTTP/1.1 304 Not Modified")
  98.                     exit
  99.                 
  100.                 
  101.                 //output the cached file
  102.                 header("Content-Type: image/$sContentType");
  103.                 readfile($sCachedFilename);
  104.                 exit;
  105.             }
  106.         }
  107.         $aImageInfo getimagesize($sImageFilename);
  108.         
  109.         //$types = array(1 => "gif", 2 =>"jpg", 3 => "png", 4 => "swf");
  110.         
  111.         $iOriginalWidth $aImageInfo[0];
  112.         $iOriginalHeight $aImageInfo[1];
  113.         
  114.         if ($sImageType == "jpg"
  115.             $oOriginalImage imagecreatefromjpeg($sImageFilename);
  116.         elseif ($sImageType == "png"
  117.             $oOriginalImage imagecreatefrompng($sImageFilename);
  118.         elseif($sImageType == "gif"
  119.             $oOriginalImage imagecreatefromgif($sImageFilename);
  120.         
  121.         if($oOriginalImage)
  122.         {
  123.             $dAspectRatio $iOriginalWidth/$iOriginalHeight;
  124.             if($iTargetHeight && !$iTargetWidth)
  125.             {
  126.                 $iNewHeight $iTargetHeight;
  127.                 $iNewWidth floor($iNewHeight $dAspectRatio);
  128.             }
  129.             elseif($iTargetWidth && !$iTargetHeight)
  130.             {
  131.                 $iNewWidth $iTargetWidth;
  132.                 $iNewHeight floor($iNewWidth/$dAspectRatio);
  133.             }
  134.             else
  135.             {
  136.                 $dOffset=min($iTargetWidth/$iOriginalWidth$iTargetHeight/$iOriginalHeight);
  137.                 $iNewWidth=$dOffset*$iOriginalWidth;
  138.                 $iNewHeight=$dOffset*$iOriginalHeight;
  139.             }
  140.             
  141.             //create a new image object
  142.             $oNewImage imagecreatetruecolor($iNewWidth$iNewHeight);
  143.             
  144.             imagecopyresampled($oNewImage$oOriginalImage0000$iNewWidth$iNewHeight$iOriginalWidth$iOriginalHeight);
  145.             
  146.             //output headers
  147.             header('Last-Modified: '.date('r'))//for cacheing
  148.             header('Accept-Ranges: bytes')//allows the client to request ranges of bytes
  149.             set_time_limit(0)
  150.             
  151.             // if this was a GIF, and GIF support is enabled, create the new file as a GIF, otherwise create a JPEG.
  152.             if $sImageType == "gif" && function_exists("imagegif") ) 
  153.             {
  154.                 //output the file type header and the image
  155.                 header("Content-Type: image/gif");
  156.                 $ok imagegif($oNewImage);
  157.                 
  158.                 //store a cached copy of this image for next time
  159.                 if($sCachedFilename && $ok
  160.                 {
  161.                     @imagegif($oNewImage,$sCachedFilename);
  162.                 }
  163.             }
  164.             elseif($sImageType=="png")
  165.             {
  166.                 //output the file type header and the image
  167.                 header("Content-Type: image/png");
  168.                 $okimagepng($oNewImage);
  169.                 
  170.                 //store a cached copy of this image for next time
  171.                 if($sCachedFilename && $ok@imagepng($oNewImage,$sCachedFilename);
  172.             }
  173.             else
  174.             {
  175.                 //output the file type header and the image
  176.                 header("Content-Type: image/jpeg");
  177.                 $ok imagejpeg($oNewImage""100);
  178.                 
  179.                 //store a cached copy of this image for next time
  180.                 if($sCachedFilename && $ok
  181.                 {
  182.                     @imagejpeg($oNewImage,$sCachedFilename,100);
  183.                 }
  184.             }
  185.             
  186.             //if we created a cached copy of this image, set the permissions so that we can delete it later if necessary
  187.             if(file_exists($sCachedFilename)) @chmod($sCachedFilename,0770);
  188.             
  189.             //the image was sent, so end here
  190.             if($oNewImageimagedestroy($oNewImage);
  191.             exit;
  192.         }
  193.     }
  194.     
  195.     //if here, there was no re-sizing to do, so just output the image
  196.     @header("Last-Modified: $sModifiedGMT");
  197.     header("Content-Type: image/$sContentType");
  198.     @readfile($sImageFilename);
  199.     exit;
  200. ?>