- ###=Content ID * ###1-###2. - ###1=Content ID ###2=Target Width * ###1-###2-###3. - ###1=Content ID ###2=Max Width ###3=Max Height * * @author Richard Sharp * @copyright 2008-2010 * @version 1.5 * @package Images */ //if set, display the "Image Not Found" image $bInvalidImage=false; //parse the received image name //get the file extension $sImageName=$_GET["ImageName"]; $aImageNameParts=explode(".",$sImageName); if(count($aImageNameParts)!=2) $bInvalidImage=true; $sImageType=strtolower($aImageNameParts[1]); //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("Pragma: cache"); @header("Cache-Control: public, max-age=$iExpiresSeconds"); if(!$bInvalidImage) { // pull the part of the filename before the dot $sImageName=$aImageNameParts[0]; //if there are hyphens, the image will need to be resized. if(substr_count($sImageName,"-")>0) { $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 FROM Content WHERE ID='.preg_replace("/[^0-9]/","",$sImageName).' 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 if($bInvalidImage) { $sFilePath='/var/www/vhosts/somedomain.com/httpdocs/images'; $sImageFilename=$sFilePath.'/image_not_available_250_250.jpg'; $sContentType="jpeg"; $sImageType="jpg"; } else { $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 if($sCachedFilename) { if(file_exists($sCachedFilename)) { $iFileModificationTime=filemtime($sCachedFilename); $sModifiedGMT = gmdate("D, d M Y H:i:s", $iFileModificationTime )." GMT"; $etag = md5_file($sCachedFilename); @header("Last-Modified: $sModifiedGMT"); @header("Etag: $etag"); //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"); exit; } //output the cached file header("Content-Type: image/$sContentType"); readfile($sCachedFilename); exit; } } $aImageInfo = getimagesize($sImageFilename); //$types = array(1 => "gif", 2 =>"jpg", 3 => "png", 4 => "swf"); $iOriginalWidth = $aImageInfo[0]; $iOriginalHeight = $aImageInfo[1]; if ($sImageType == "jpg") $oOriginalImage = imagecreatefromjpeg($sImageFilename); elseif ($sImageType == "png") $oOriginalImage = imagecreatefrompng($sImageFilename); elseif($sImageType == "gif") $oOriginalImage = imagecreatefromgif($sImageFilename); if($oOriginalImage) { $dAspectRatio = $iOriginalWidth/$iOriginalHeight; if($iTargetHeight && !$iTargetWidth) { $iNewHeight = $iTargetHeight; $iNewWidth = floor($iNewHeight * $dAspectRatio); } elseif($iTargetWidth && !$iTargetHeight) { $iNewWidth = $iTargetWidth; $iNewHeight = floor($iNewWidth/$dAspectRatio); } else { $dOffset=min($iTargetWidth/$iOriginalWidth, $iTargetHeight/$iOriginalHeight); $iNewWidth=$dOffset*$iOriginalWidth; $iNewHeight=$dOffset*$iOriginalHeight; } //create a new image object $oNewImage = imagecreatetruecolor($iNewWidth, $iNewHeight); imagecopyresampled($oNewImage, $oOriginalImage, 0, 0, 0, 0, $iNewWidth, $iNewHeight, $iOriginalWidth, $iOriginalHeight); //output headers header('Last-Modified: '.date('r')); //for cacheing header('Accept-Ranges: bytes'); //allows the client to request ranges of bytes set_time_limit(0); // if this was a GIF, and GIF support is enabled, create the new file as a GIF, otherwise create a JPEG. if ( $sImageType == "gif" && function_exists("imagegif") ) { //output the file type header and the image header("Content-Type: image/gif"); $ok = imagegif($oNewImage); //store a cached copy of this image for next time if($sCachedFilename && $ok) { @imagegif($oNewImage,$sCachedFilename); } } elseif($sImageType=="png") { //output the file type header and the image header("Content-Type: image/png"); $ok= imagepng($oNewImage); //store a cached copy of this image for next time if($sCachedFilename && $ok) @imagepng($oNewImage,$sCachedFilename); } else { //output the file type header and the image header("Content-Type: image/jpeg"); $ok = imagejpeg($oNewImage, "", 100); //store a cached copy of this image for next time if($sCachedFilename && $ok) { @imagejpeg($oNewImage,$sCachedFilename,100); } } //if we created a cached copy of this image, set the permissions so that we can delete it later if necessary if(file_exists($sCachedFilename)) @chmod($sCachedFilename,0770); //the image was sent, so end here if($oNewImage) imagedestroy($oNewImage); exit; } } //if here, there was no re-sizing to do, so just output the image @header("Last-Modified: $sModifiedGMT"); header("Content-Type: image/$sContentType"); @readfile($sImageFilename); exit; ?>