Sample code sharing for generating thumbnails in php (implemented using the gd library) _ php instance

Source: Internet
Author: User
Share an example of generating thumbnails using the GD library of php. For more information, see. The code is as follows:



Header ("content-type: text/html; charset = gbk ");
Ini_set ("date. timezone", "Asia/chong ");
// Determine whether the file is empty
If (empty ($ _ FILES )){
Echo "uploading files too large ";
Exit;
}
// Determine whether the file is uploaded incorrectly
If ($ _ FILES ['Pic '] ['error']) {
Echo "upload files ";
Exit;
}
// Determine whether the file type is illegal to obtain the file suffix
$ Allowtype = array ("jpg", "png", "jpeg", "gif ");
$ A = explode ('.', $ _ FILES ['Pic '] ['name']);
$ Index = count ($ a)-1;
$ Ex = strtolower ($ a [$ index]);
If (! In_array ($ ex, $ allowtype )){
Echo "Illegal File upload ";
Exit;
}
$ File = date ('ymdhis '). rand (). ".". $ ex;
$ Src = $ _ FILES ['Pic '] ['tmp _ name'];
$ Des = "upload/". $ file;
$ Rs = move_uploaded_file ($ src, $ des );

// Thumbnail
// Read the uploaded image
$ Image = imagecreatefromjpeg ($ des );
$ A = getimagesize ($ des );
$ W = $ a [0];
$ H = $ a [1];
If ($ w> $ h ){
$ Width = 300;
$ Height = $ width/$ w * $ h;
} Else if ($ w <$ h ){
$ Height = 300;
$ Width = $ height/$ h * $ w;
} Else {
$ Width = 300;
$ Height = 300;
} Www.jb51.net
// Create a blank image
$ Newimage = imagecreatetruecolor ($ width, $ height );
// Copy source image content copy New Image
Imagecopyresized ($ newimage, $ image, 0, 0, 0, $ width, $ height, $ w, $ h );
$ Filename = "upload/s _". $ file;
Imagejpeg ($ newimage, $ filename );

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.