Image Resize
Download this source
This is an image resizing function I wrote sometime back.
PHP
<?php /** * This function resizes a given image into the target file with the given width and height * * @author Ryan Pallas * @copyright 2010, released under GPL * * @example usage * $src = "/var/www/images/uploads/image.jpg"; * $targ = "/var/www/images/uploads/thumbs/image.jpg"; * if( imageResize($src,$targ,100,75) ) echo "Successfully resized thumbnail"; * * @param string $src Source input file to be resized * @param string $target Target output after resizing * @param int $newWidth Target width * @param int $newHeight Target height * @param bool $abs Whether newwidth/newheight are final or maximum measurements (Default: FALSE) * * @return bool */ function imageResize($src,$target,$newWidth,$newHeight,$abs=FALSE) { /** * @var array source image info */ $imgInfo = getimagesize($src); /** * return false if not an image */ if($imgInfo == FALSE) return FALSE; /** * @var int width * @var int height * @var constant img type */ list($origWidth,$origHeight,$imgtype) = $imgInfo; /** * absolute dimensions or max dimensions */ if( !$abs ) { /** * determine new width & height based on aspect ratio */ $ratio = (($origWidth / $origHeight) > ($newWidth / $newHeight)) ? $newWidth / $origWidth : $newHeight / $origHeight; $newWidth = (int) ($origWidth * $ratio); // set width with ratio $newHeight = (int) ($origHeight * $ratio); // set height with ratio /** * if original dimensions smaller than new, do not resize */ if( $origHeight <= $newHeight && $origWidth <= $newWidth) { $newHeight = $origHeight; $newWidth = $origWidth; } /** * @var int image offset for resampling */ $xOffset = 0; $yOffset = 0; } else { /** * set original sampling area based on exact new dimensions */ $ratio = (($origWidth / $origHeight) < ($newWidth / $newHeight)) ? $newWidth / $origWidth : $newHeight / $origHeight; $finalHeight = (int) floor($newHeight / $ratio); $finalWidth = (int) floor($newWidth / $ratio); /** * @var int offset to center resampling area on original source */ $yOffset = ($finalHeight == $origHeight) ? 0 : floor( ($origHeight-$finalHeight)/2 ); $xOffset = ($finalWidth == $origWidth) ? 0 : floor( ($origWidth-$finalWidth)/2 ); /** * @var int resampling area of original source */ $origHeight = $finalHeight; $origWidth = $finalWidth; } /** * determine file type for correct functions */ switch($imgtype) { case IMAGETYPE_JPEG: $create = "imagecreatefromjpeg"; $move = "imagejpeg"; break; case IMAGETYPE_GIF: $create = "imagecreatefromgif"; $move = "imagegif"; break; case IMAGETYPE_PNG: $create = "imagecreatefrompng"; $move = "imagepng"; break; case IMAGETYPE_WBMP: $create = "imagecreatefromwbmp"; $move = "imagewbmp"; break; default: return FALSE; } /** * @var image create image from source */ $base = $create($src); /** * @var image create image to new size */ $temp = imagecreatetruecolor($newWidth,$newHeight); /** * copy resampled original and resize to new image */ imagecopyresampled($temp,$base,0,0,$xOffset,$yOffset,$newWidth,$newHeight,$origWidth,$origHeight); /** * move new image to destination * return true on success, false on failure */ if ( $move($temp,$target) ) { return TRUE; } else { return FALSE; } }



