$intMaxWidth) || ($intOriginalHeight > $intMaxHeight)) { if ($intCrop === 1) { $fltRatioComputed = $intOriginalWidth / $intOriginalHeight; $intThumbWidth = $intMaxWidth; $intThumbHeight = $intMaxHeight; if ($intMaxWidth != $intMaxHeight) $fltCropRatio = $intMaxWidth / $intMaxHeight; else $fltCropRatio = 1; if ($fltRatioComputed < $fltCropRatio) { // Image is too tall so we will crop the top and bottom $intOriginalHeight1 = $intOriginalHeight; $intOriginalHeight = $intOriginalWidth / $fltCropRatio; $intOffsetY = ($intOriginalHeight1 - $intOriginalHeight) / 2; } elseif ($fltRatioComputed > $fltCropRatio) { // Image is too wide so we will crop off the left and right sides $intOriginalWidth1 = $intOriginalWidth; $intOriginalWidth = $intOriginalHeight * $fltCropRatio; $intOffsetX = ($intOriginalWidth1 - $intOriginalWidth) / 2; } } else { // Setting up the ratios needed for resizing. We will compare these below to determine how to // resize the image (based on height or based on width) $fltRatioX = $intMaxWidth / $intOriginalWidth; $fltRatioY = $intMaxHeight / $intOriginalHeight; if ($fltRatioX * $intOriginalHeight < $intMaxHeight) { // Resize the image based on width $intThumbHeight = ceil($fltRatioX * $intOriginalHeight); $intThumbWidth = $intMaxWidth; } else { // Resize the image based on height $intThumbWidth = ceil($fltRatioY * $intOriginalWidth); $intThumbHeight = $intMaxHeight; } } // We don't want to run out of memory ini_set('memory_limit', '100M'); // Set up a blank canvas for our resized image (destination) $strDestination = imagecreatetruecolor($intThumbWidth, $intThumbHeight); // Set up the appropriate image handling functions based on the original image's mime type switch ($tabSize['mime']) { case 'image/gif': // We will be converting GIFs to PNGs to avoid transparency issues when resizing GIFs // This is maybe not the ideal solution, but IE6 can suck it $strCreationFunction = 'imagecreatefromgif'; $strOutputFunction = 'imagepng'; $strMime = 'image/png'; // We need to convert GIFs to PNGs $intQuality = round(10 - ($intQuality / 10)); // We are converting the GIF to a PNG and PNG needs a compression level of 0 (no compression) through 9 $blnDoSharpen = false; break; case 'image/x-png': case 'image/png': $strCreationFunction = 'imagecreatefrompng'; $strOutputFunction = 'imagepng'; $intQuality = round(10 - ($intQuality / 10)); // PNG needs a compression level of 0 (no compression) through 9 $blnDoSharpen = false; break; default: $strCreationFunction = 'imagecreatefromjpeg'; $strOutputFunction = 'imagejpeg'; $blnDoSharpen = true; break; } // Read in the original image $strSource = $strCreationFunction($strPath . $strImage . '.' . $strExt); $strResized = $strPath . $strImage . '_' . $strSuffixe . '.' . $strExt; if (in_array($tabSize['mime'], array('image/gif', 'image/png'))) { // If this is a GIF or a PNG, we need to set up transparency imagealphablending($strDestination, false); imagesavealpha($strDestination, true); } // Resample the original image into the resized canvas we set up earlier imagecopyresampled($strDestination, $strSource, 0, 0, $intOffsetX, $intOffsetY, $intThumbWidth, $intThumbHeight, $intOriginalWidth, $intOriginalHeight); if ($blnDoSharpen) { // Sharpen the image based on two things: // (1) the difference between the original size and the final size // (2) the final size $intSharpness = findsharp($intOriginalWidth, $intThumbWidth); $tabSharpenMatrix = array( array(-1, -2, -1), array(-2, $intSharpness + 12, -2), array(-1, -2, -1) ); $intDivisor = $intSharpness; $intOffset = 0; imageconvolution($strDestination, $tabSharpenMatrix, $intDivisor, $intOffset); } // Write the resized image to the cache $strOutputFunction($strDestination, $strResized, $intQuality); // Clean up the memory imagedestroy($strSource); imagedestroy($strDestination); return str_replace($strPath, '', $strResized); } else return $strImage . '.' . $strExt; } // function from Ryan Rud (http://adryrun.com) function findSharp($orig, $final) { $final = $final * (750.0 / $orig); $a = 52; $b = -0.27810650887573124; $c = .00047337278106508946; $result = $a + $b * $final + $c * $final * $final; return max(round($result), 0); } ?>