/* * * Thumbnailer function by fluidByte (http://www.fluidbyte.net) * * Example: * ------------------------------------------------------------------ * buildThumbnail('/website/images/somefile.jpg','width',200,5,false); * * * Passed Variables: * ------------------------------------------------------------------ * 1.) Path (from root) to the image * 2.) Type of resize - i.e. by 'width' or 'height' * 3.) Maximum size of thumbnail * 4.) Quality (1-10) * 5.) Square crop (true/false) * * * ------------------------------------------------------------------ * * NOTE: In order for this function to work you must have a folder * called "thumbs" inside the directory where your image resides. * This folder must have write permissions. * * */ function buildThumbnail($img,$type,$max,$quality,$square_crop){ // Split image parts (path and file) $imgSplit = explode("/",$img); $sourceName = end($imgSplit); $imgpath = str_ireplace($sourceName, "", $img); // Paths $sourcePath = $_SERVER['DOCUMENT_ROOT'] . $imgpath; $sourceUrl = $imgpath; // Get extension $split = explode(".",$sourceName); $ext = strtolower($split[1]); $thumbUrl = $sourceUrl . 'thumbs/'; $thumbName = "thm_$sourceName"; $thumbPath = $sourcePath . 'thumbs'; $thumbQuality = $quality; // If the thumbnail doesn't exist, create the mofo! ################################# if(!file_exists("$thumbPath/$thumbName")){ switch($ext){ case('jpg'): $sourceImage = imagecreatefromjpeg("$sourcePath/$sourceName"); break; case('jpeg'): $sourceImage = imagecreatefromjpeg("$sourcePath/$sourceName"); break; case('png'): $sourceImage = imagecreatefrompng("$sourcePath/$sourceName"); break; case('gif'): $sourceImage = imagecreatefromgif("$sourcePath/$sourceName"); break; } $sourceWidth = imagesx($sourceImage); $sourceHeight = imagesy($sourceImage); // Determine specs based on type if(strtolower($type)=="width"){ $thumbWidth = $max; $thumbHeight = $sourceHeight/($sourceWidth/$thumbWidth); } else{ $thumbHeight = $max; $thumbWidth = $sourceWidth/($sourceHeight/$thumbHeight); } // Determine specs if crop applied $srcX = 0; $srcY = 0; $sourceNewWidth = $sourceWidth; $sourceNewHeight = $sourceHeight; $dest = $sourceImage; if($square_crop==true){ $thumbWidth = $max; $thumbHeight = $max; if($sourceHeight>$sourceWidth){ $srcX = 0; $srcY = floor(($sourceHeight-$sourceWidth)/2); $sourceNewHeight = $sourceWidth; $sourceNewWidth = $sourceWidth; } if($sourceWidth>$sourceHeight){ $srcX = floor(($sourceWidth-$sourceHeight)/2); $srcY = 0; $sourceNewHeight = $sourceHeight; $sourceNewWidth = $sourceHeight; } // Create new image with a new width and height. $dest = imagecreatetruecolor($sourceNewWidth, $sourceNewHeight); // Copy new image to memory after cropping. imagecopy($dest, $sourceImage, 0, 0, $srcX, $srcY, $sourceNewWidth, $sourceNewHeight); } $targetImage = imagecreatetruecolor($thumbWidth,$thumbHeight); imagecopyresampled($targetImage,$dest,0,0,0,0,$thumbWidth,$thumbHeight,$sourceNewWidth,$sourceNewHeight); switch($ext){ case('jpg'): imagejpeg($targetImage, "$thumbPath/$thumbName", $thumbQuality * 10); break; case('jpeg'): imagejpeg($targetImage, "$thumbPath/$thumbName", $thumbQuality * 10); break; case('png'): imagepng($targetImage, "$thumbPath/$thumbName", $thumbQuality); break; case('gif'): imagegif($targetImage, "$thumbPath/$thumbName"); break; } } // Return the thumbnail ############################################################# return(""); imagedestroy($dest); imagedestroy($targetImage); }