Source for file ViewImage.php
-
-
-
* This script serves images to the client browser. The images are re-sized on the fly if necessary, or served from a cache.
-
* The image name is passed in a GET variable named ImageName.
-
* Possible formats for ImageName:
-
* ###.<type> - ###=Content ID
-
* ###1-###2.<type> - ###1=Content ID ###2=Target Width
-
* ###1-###2-###3.<type> - ###1=Content ID ###2=Max Width ###3=Max Height
-
-
-
-
-
-
-
-
//if set, display the "Image Not Found" image
-
-
-
//parse the received image name
-
-
$sImageName=
$_GET["ImageName"];
-
$aImageNameParts=
explode(".",$sImageName);
-
if(count($aImageNameParts)!=
2) $bInvalidImage=
true;
-
-
-
//determine the content mime type to send in the header
-
if($sImageType==
"jpg") $sContentType=
"jpeg";
-
elseif($sImageType==
"gif") $sContentType=
"gif";
-
elseif($sImageType==
"png") $sContentType=
"png";
-
else $bInvalidImage=
true;
-
-
//send some headers for cache control
-
$iExpiresSeconds =
60 *
60 *
24 *
30; //30 days
-
$sExpiresGMT =
gmdate("D, d M Y H:i:s", time() +
$iExpiresSeconds ).
" GMT";
-
$sModifiedGMT =
gmdate("D, d M Y H:i:s", time() +
(3600 * -
5 *
24 *
365) ).
" GMT";
-
-
@header("Expires: $sExpiresGMT");
-
-
@header("Cache-Control: public, max-age=$iExpiresSeconds");
-
-
-
-
// pull the part of the filename before the dot
-
$sImageName=
$aImageNameParts[0];
-
-
//if there are hyphens, the image will need to be resized.
-
-
-
$aNameParts=
explode("-",$sImageName);
-
$sImageName=
$aNameParts[0];
-
$iTargetWidth=
$aNameParts[1];
-
$iTargetHeight=
$aNameParts[2];
-
-
-
//get the info on this image from the database
-
$query=
'SELECT Image,ClientID
-
-
-
AND ImageType="'.
$oDB->Escape(strtolower($sImageType)).
'"
-
-
$result=
$oDB->Select($query);
-
if(!$result[0]) $bInvalidImage=
true;
-
-
-
//if the image in anvalid, serve the "Not Found" image
-
-
-
$sFilePath=
'/var/www/vhosts/somedomain.com/httpdocs/images';
-
$sImageFilename=
$sFilePath.
'/image_not_available_250_250.jpg';
-
-
-
-
-
-
$sFilePath=
'/var/www/vhosts/somedomain.com/httpsdocs/content/'.
$result[0]["ClientID"];
-
$sImageFilename=
$sFilePath.
'/'.
$result[0]["Image"];
-
$sCachedFilename=
$sImageFilename.
'-'.
$iTargetWidth.
'X'.
$iTargetHeight;
-
-
-
//if a width or height is specified, re-size and output
-
if($iTargetHeight ||
$iTargetWidth ) //we must manipulate this image
-
-
//first, see if there is a cached version of this image in this size
-
-
-
-
-
$iFileModificationTime=
filemtime($sCachedFilename);
-
$sModifiedGMT =
gmdate("D, d M Y H:i:s", $iFileModificationTime ).
" GMT";
-
-
@header("Last-Modified: $sModifiedGMT");
-
-
-
//if not modified, send a 304 not modified and end
-
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) ==
$iFileModificationTime ||
trim($_SERVER['HTTP_IF_NONE_MATCH']) ==
$etag)
-
-
header("HTTP/1.1 304 Not Modified");
-
-
-
-
-
header("Content-Type: image/$sContentType");
-
-
-
-
-
-
-
//$types = array(1 => "gif", 2 =>"jpg", 3 => "png", 4 => "swf");
-
-
$iOriginalWidth =
$aImageInfo[0];
-
$iOriginalHeight =
$aImageInfo[1];
-
-
if ($sImageType ==
"jpg")
-
-
elseif ($sImageType ==
"png")
-
-
elseif($sImageType ==
"gif")
-
-
-
-
-
$dAspectRatio =
$iOriginalWidth/
$iOriginalHeight;
-
if($iTargetHeight &&
!$iTargetWidth)
-
-
$iNewHeight =
$iTargetHeight;
-
$iNewWidth =
floor($iNewHeight *
$dAspectRatio);
-
-
elseif($iTargetWidth &&
!$iTargetHeight)
-
-
$iNewWidth =
$iTargetWidth;
-
$iNewHeight =
floor($iNewWidth/
$dAspectRatio);
-
-
-
-
$dOffset=
min($iTargetWidth/
$iOriginalWidth, $iTargetHeight/
$iOriginalHeight);
-
$iNewWidth=
$dOffset*
$iOriginalWidth;
-
$iNewHeight=
$dOffset*
$iOriginalHeight;
-
-
-
//create a new image object
-
-
-
imagecopyresampled($oNewImage, $oOriginalImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeight);
-
-
-
header('Last-Modified: '.
date('r')); //for cacheing
-
header('Accept-Ranges: bytes'); //allows the client to request ranges of bytes
-
-
-
// if this was a GIF, and GIF support is enabled, create the new file as a GIF, otherwise create a JPEG.
-
-
-
//output the file type header and the image
-
header("Content-Type: image/gif");
-
-
-
//store a cached copy of this image for next time
-
if($sCachedFilename &&
$ok)
-
-
-
-
-
elseif($sImageType==
"png")
-
-
//output the file type header and the image
-
header("Content-Type: image/png");
-
-
-
//store a cached copy of this image for next time
-
if($sCachedFilename &&
$ok) @imagepng($oNewImage,$sCachedFilename);
-
-
-
-
//output the file type header and the image
-
header("Content-Type: image/jpeg");
-
-
-
//store a cached copy of this image for next time
-
if($sCachedFilename &&
$ok)
-
-
-
-
-
-
//if we created a cached copy of this image, set the permissions so that we can delete it later if necessary
-
-
-
//the image was sent, so end here
-
-
-
-
-
-
//if here, there was no re-sizing to do, so just output the image
-
@header("Last-Modified: $sModifiedGMT");
-
header("Content-Type: image/$sContentType");
-
-
-