Make image version

Wednesday, March 17th, 2010

This is a function I use to make multiple versions such as thumbnails of uploaded images.
There’s three methods for resizing available in it (mask, fit & limit).

  1. // Function for making a new resized copy of a file
  2. // mask makes the shortest edge match the mask, so it will be bigger than the mask, always resizes!
  3. // fit makes the longest edge match, always resizes!
  4. // limit makes the image fit, but won't stretch if it's smaller.
  5. function makeImageVersion($newWidth, $newHeight, $filePath, $newName, $fileExtension, $method){
  6.  
  7.         // Find out the original size of the image
  8.         list($wO, $hO) = getimagesize($filePath);
  9.  
  10.         // Get the ratios it would take to resize
  11.         $wR = $newWidth / $wO;
  12.         $hR = $newHeight / $hO;
  13.  
  14.         // Figure out which ratio we need
  15.         switch($method){
  16.                 case 'mask':
  17.                         if($wR > $hR){ $ratio = $wR; }
  18.                         else{ $ratio = $hR; }
  19.                         break;
  20.                 case 'limit':
  21.                         if($wR < 1 && $wR < $hR){ $ratio = $wR; }
  22.                         elseif($hR < 1){ $ratio = $hR; }
  23.                         else{ $ratio = 1; }
  24.                         break;
  25.                 case 'fit':
  26.                 default:
  27.                         if($wR > $hR){ $ratio = $hR; }
  28.                         else{ $ratio = $wR; }
  29.                         break;
  30.         }
  31.  
  32.         $wN = round($wO * $ratio);
  33.         $hN = round($hO * $ratio);
  34.  
  35.         $iP = imagecreatetruecolor($wN, $hN);
  36.         imageAlphaBlending($iP, false);
  37.         imageSaveAlpha($iP, true);
  38.  
  39.         if($fileExtension == 'jpg'){ $iI = imagecreatefromjpeg($filePath); }
  40.         elseif($fileExtension == 'png'){        $iI = imagecreatefrompng($filePath); }
  41.         else{ $iI = imagecreatefromgif($filePath); }
  42.  
  43.         imagecopyresampled($iP, $iI, 0, 0, 0, 0, $wN, $hN, $wO, $hO);
  44.  
  45.         if($fileExtension == 'jpg'){ imagejpeg($iP, $newName, 90); }
  46.         elseif($fileExtension == 'png'){ imagepng($iP, $newName); }
  47.         else{ imagegif($iP, $newName); }
  48. }

Display clean php code for copying

// Function for making a new resized copy of a file
// mask makes the shortest edge match the mask, so it will be bigger than the mask, always resizes!
// fit makes the longest edge match, always resizes!
// limit makes the image fit, but won't stretch if it's smaller.
function makeImageVersion($newWidth, $newHeight, $filePath, $newName, $fileExtension, $method){

	// Find out the original size of the image
	list($wO, $hO) = getimagesize($filePath);

	// Get the ratios it would take to resize
	$wR = $newWidth / $wO;
	$hR = $newHeight / $hO;

	// Figure out which ratio we need
	switch($method){
		case 'mask':
			if($wR > $hR){ $ratio = $wR; }
			else{ $ratio = $hR; }
			break;
		case 'limit':
			if($wR < 1 && $wR < $hR){ $ratio = $wR; }
			elseif($hR < 1){ $ratio = $hR; }
			else{ $ratio = 1; }
			break;
		case 'fit':
		default:
			if($wR > $hR){ $ratio = $hR; }
			else{ $ratio = $wR; }
			break;
	}

	$wN = round($wO * $ratio);
	$hN = round($hO * $ratio);

	$iP = imagecreatetruecolor($wN, $hN);
	imageAlphaBlending($iP, false);
	imageSaveAlpha($iP, true);

	if($fileExtension == 'jpg'){ $iI = imagecreatefromjpeg($filePath); }
	elseif($fileExtension == 'png'){	$iI = imagecreatefrompng($filePath); }
	else{ $iI = imagecreatefromgif($filePath); }

	imagecopyresampled($iP, $iI, 0, 0, 0, 0, $wN, $hN, $wO, $hO);

	if($fileExtension == 'jpg'){ imagejpeg($iP, $newName, 90); }
	elseif($fileExtension == 'png'){ imagepng($iP, $newName); }
	else{ imagegif($iP, $newName); }
}
click here to close

Help keep these files free,
and support further development!