Source for file ImageProcessing.func.php
-
-
-
* A set of basic functions for processing uploaded images.
-
-
-
-
-
-
-
-
* Returns information about the image in an associative array.
-
* @param string The full path to the image file.
-
* @return mixed Returns an array of information on success (type, width, height), false on failure.
-
-
-
-
-
if(!$aInfo) return false;
-
-
if($aInfo[2]==
1) $return["type"]=
"gif";
-
elseif($aInfo[2]==
2) $return["type"]=
"jpg";
-
elseif($aInfo[2]==
3) $return["type"]=
"png";
-
elseif($aInfo[2]==
4) $return["type"]=
"swf";
-
-
-
$return["width"]=
$aInfo[0];
-
$return["height"]=
$aInfo[1];
-
-
-
-
-
-
* 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.
-
* @param string The source file.
-
* @param string The filename for the new image file that will be created. This should not include any extension.
-
* @param int The new height of the image.
-
* @param int The new width of the image.
-
* @param string The filename of the original image file. (If uploaded)
-
* @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.
-
* @return mixed The image type (extension) of the newly created/copied image. False on error.
-
-
function resizeImage($sSourceFilename, $sDestinationFilename, $iTargetHeight =
0, $iTargetWidth =
0, $sOriginalFilename =
"", $sWatermarkImage=
"")
-
-
//check for required values
-
if( !$sSourceFilename ||
!$sDestinationFilename) return(false);
-
-
-
-
$aTypes =
array(1 =>
"gif", 2 =>
"jpg", 3 =>
"png", 4 =>
"swf");
-
-
$dOriginalWidth =
$aImageInfo[0];
-
$dOriginalHeight =
$aImageInfo[1];
-
$sOriginalImageType =
$aTypes[$aImageInfo[2]];
-
-
//if unable to determine the type from the getimagesize function, try from the original filename
-
if( !$sOriginalImageType &&
$sOriginalFilename )
-
-
-
$ext =
$arrNameParts[count($arrNameParts) -
1];
-
-
-
-
-
$sOriginalImageType =
"gif";
-
-
-
$sOriginalImageType =
"jpg";
-
-
-
$sOriginalImageType =
"jpg";
-
-
-
$sOriginalImageType =
"png";
-
-
-
-
-
//if unable to determine the image type, or an invalid type, fail
-
if ( !$sOriginalImageType )
-
-
-
if(file_exists($sDestinationFilename.
".".
$sOriginalImageType))
-
unlink($sDestinationFilename.
".".
$sOriginalImageType);
-
-
//if provided a new width or height, manipulate the image
-
if( $iTargetHeight ||
$iTargetWidth )
-
-
if ($sOriginalImageType ==
"jpg")
-
-
elseif ($sOriginalImageType ==
"png")
-
-
elseif($sOriginalImageType ==
"gif")
-
-
-
-
-
$dAspectRatio =
$dOriginalWidth/
$dOriginalHeight;
-
if($iTargetHeight &&
!$iTargetWidth)
-
-
$dNewHeight =
$iTargetHeight;
-
$dNewHeight =
floor($dNewHeight *
$dAspectRatio);
-
-
elseif($iTargetWidth &&
!$iTargetHeight)
-
-
$dNewHeight =
$iTargetWidth;
-
$dNewHeight =
floor($dNewHeight/
$dAspectRatio);
-
-
-
-
$dNewHeight =
$iTargetHeight;
-
$dNewHeight =
$iTargetWidth;
-
-
-
-
-
//copy the old image to the new image with the new dimentions
-
imagecopyresampled($oNewImage, $oOriginalImage, 0, 0, 0, 0, $dNewHeight, $dNewHeight, $dOriginalWidth, $dOriginalHeight);
-
-
-
-
-
-
-
-
-
$iWatermarkWidth =
imagesx($oWatermark);
-
$iWatermarkHeight =
imagesy($oWatermark);
-
-
$iDestinationX =
$dNewHeight -
$iWatermarkWidth -
5;
-
$iDestinationY =
$dNewHeight -
$iWatermarkHeight -
5;
-
-
imagecopymerge($oNewImage, $oWatermark, $iDestinationX, $iDestinationY, 0, 0, $iWatermarkWidth, $iWatermarkHeight, 100);
-
-
-
-
// if this was a GIF, and GIF support is enabled, create the new file as a GIF, otherwise create a JPEG.
-
-
-
-
-
$ok =
imagegif($oNewImage, $sDestinationFilename.
".".
$sNewImageType);
-
-
-
-
-
$ok =
imagejpeg($oNewImage, $sDestinationFilename.
".".
$sNewImageType, 100);
-
-
-
-
-
//if $ok is not set, either there was no re-sizing to be done, or it failed, so just copy the image
-
-
-
$sNewImageType =
$sOriginalImageType;
-
$ok =
copy($sSourceFilename, $sDestinationFilename.
".".
$sNewImageType);
-
-
-
-
-
-
-
-
-