PHP scaling Image

Source: Internet
Author: User

For images uploaded at the front end of a website, it is necessary to scale the images in the background to generate a thumbnail of 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 ):

// By MoreWindows (http://www.bkjia.com)


Class CImage
{
/**
* Generate an image with the source image portrait to support. PNG. jpg. gif.
* The graphic layout is in. PNG format.
* $ SrcFile Original Image File Name
* $ ToW thumbnail width
* $ ToH thumbnail height
* $ ToFile: the name of the thumbnail file. If it is null, it overwrites the original image file.
* @ Return bool
*/
Public static function CreateThumbnail ($ srcFile, $ toW, $ toH, $ toFile = "")
{
If ($ toFile = "")
{
$ ToFile = $ srcFile;
}
$ Info = "";
// Return an array containing four units, 0-width, 1-height, 2-image type, and 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 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! <A href = 'javascript: history. back (); '> back </a> ";
Exit ();
}
$ Im = imagecreatefromgif ($ srcFile );
Break;

Case 2:
If (! Function_exists ("imagecreatefromjpeg "))
{
Echo "the GD can't support. jpeg, please use other picture! <A href = 'javascript: history. back (); '> back </a> ";
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)
{
// Copy part of the image and adjust the size to ensure better clarity.
Imagecopyresampled ($ ni, $ im, 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 the file to the. PNG format.
Imagepng ($ ni, $ toFile); // output the image to a browser or file in PNG format
ImageDestroy ($ ni );
ImageDestroy ($ im );
Return true;
}


From MoreWindows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.