For images uploaded by the front-end of a website, it is necessary to scale the images in the background to generate a thumbnail with a uniform size. In PHP, you can easily use the GD library to complete this task. The following CImage static method CreateThumbnail () function can use parameters such as the original image file name, thumbnail width and height, and the generated thumbnail file name to generate a thumbnail that maintains the aspect ratio of the original image. The function basically shows the PHP method for generating thumbnails, which can be further expanded, for example, generating thumbnails in the specified format based on the thumbnail file name.
The complete code is as follows (Win7 + XAMPP2.5 and Linuxdev 2.6.18-128 + apache have passed the test ):
[Php] view plain copy
// By MoreWindows ( http://blog.csdn.net/MoreWindows ) Class CImage {/*** generate an image that maintains the horizontal comparison of the source image, supporting .png. jpg. gif * images are in. PNG format * $ srcFile original image file name * $ toW thumbnail width * $ toH Thumbnail height * $ toFile thumbnail file name, overwrite the original image file * @ return bool */public static function CreateThumbnail ($ srcFile, $ toW, $ toH, $ toFile = "") {if ($ toFile = "") {$ toFile = $ srcFile;} $ info = ""; // returns an array containing four units, 0-width, 1-height, 2-image type, 3-width text description. // If the request fails, false is returned and a warning is generated. $ Data = getimagesize ($ srcFile, $ info); if (! $ Data) return false; // load the file to 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 true color image if ($ ni) {// re-sample and copy part of the image and adjust the size to maintain a good definition of imagecopyresampled ($ ni, $ im, 0, 0, 0, 0, 0, $ ftoW, $ ftoH, $ srcW, $ srcH);} else {// Copy part of the image and resize it $ 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 it to the file in the. PNG format imagepng ($ ni, $ toFile); // output the image to the browser or file ImageDestroy ($ ni) in PNG format ); imageDestroy ($ im); return true ;}}
Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/7062506