For images uploaded on the front end of the website, it is necessary to scale them during background processing to generate uniformly sized thumbnails. In PHP, you can easily use the GD library to accomplish this task. The static method CreateThumbnail() function of the CImage class below can accept the original image file name, thumbnail width and height, generated thumbnail file name and other parameters to generate a thumbnail that maintains the aspect ratio of the original image. The function basically embodies the method of generating thumbnails in PHP, and the function can be further expanded, such as generating thumbnails in a specified format based on the name of the thumbnail file.
The complete code is as follows (Win7+XAMPP2.5 and Linuxdev 2.6.18-128+apache passed the test):
//by MoreWindows ( http://www.BkJia.com )
class CImage
{
/**
* Generate thumbnails that maintain the aspect ratio of the original image, supporting .png .jpg .gif
* * Thumbnail types are unified into .png format
* $srcFile Original image file name
* $toW Thumbnail width
* $toH Thumbnail height
* $toFile The name of the thumbnail file. If it is empty, it will overwrite the original image file
* @return bool
*/
Public static function CreateThumbnail($srcFile, $toW, $toH, $toFile="")
{
if ($toFile == "")
$toFile = $srcFile;
}
$info = "";
//Return an array containing 4 cells, 0-width, 1-height, 2-image type, 3-text description of width and height.
//Return false and generate a warning on failure.
$data = getimagesize($srcFile, $info);
If (!$data)
return false;
//Load the file into the resource variable im
switch ($data[2]) //1-GIF, 2-JPG, 3-PNG
case 1:
If(!function_exists("imagecreatefromgif"))
echo "the GD can't support .gif, please use .jpeg or .png! back";
exit();
$im = imagecreatefromgif($srcFile);
break;
case 2:
If(!function_exists("imagecreatefromjpeg"))
echo "the GD can't support .jpeg, please use other picture! back";
exit();
$im = imagecreatefromjpeg($srcFile);
break;
case 3:
$im = imagecreatefrompng($srcFile);
break;
}
//Calculate the width and height of the thumbnail
$srcW = imagesx($im);
$srcH = imagesy($im);
$toWH = $toW / $toH;
$srcWH = $srcW / $srcH;
If ($toWH <= $srcWH)
$ftoW = $toW;
$ftoH = (int)($ftoW * ($srcH / $srcW));
}
else
$ftoH = $toH;
$ftoW = (int)($ftoH * ($srcW / $srcH));
}
If (function_exists("imagecreatetruecolor"))
$ni = imagecreatetruecolor($ftoW, $ftoH); //Create a new true color image
If ($ni)
// Resample and copy part of the image and resize it to maintain better clarity
imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
else
//Copy part of the image and resize
$ni = imagecreate($ftoW, $ftoH);
Imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
}
else
$ni = imagecreate($ftoW, $ftoH);
imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
}
//Save to file in .png format
imagepng($ni, $toFile); //Output the image to the browser or file in PNG format
ImageDestroy($ni);
ImageDestroy($im);
return true;
}
Excerpted from MoreWindows
http://www.bkjia.com/PHPjc/478474.html
www.bkjia.com
TechArticle