PHP generates image thumbnails and displays them in a browser. this feature is flexible and practical.

Source: Internet
Author: User
: This article describes how to generate image thumbnails in PHP and display them in a browser. it is flexible and practical. if you are interested in PHP tutorials, refer to it. As an individual needs to upload images to a website album and display them in the form of thumbnails, many of Baidu's source code has been repeatedly tried and defeated, and he has written a function, which has been successfully run. The thumbnail is displayed in the browser by directing the image's src address to a php file and calling this function to display it.

/*** _ Thumb () function for generating thumbnails */function _ thumb ($ _ filename, $ _ max_size) {// (Image source address, maximum width or height) // get the file suffix $ _ n = explode ('. ', $ _ filename); // Generate the header of the png header file ('content-type: image/png'); // Obtain the length and height of the file list ($ _ width, $ _ height) = getimagesize ($ _ filename); // generates the length and height of the thumbnail. $ _ percent = $ _ max_size/($ _ width> $ _ height )? $ _ Width: $ _ height); $ _ new_width = $ _ width * $ _ percent; $ _ new_height = $ _ height * $ _ percent; // Create a thumbnail canvas $ _ new_image = imagecreatetruecolor ($ _ new_width, $ _ new_height); // Create a canvas switch based on the existing image ($ _ n [1]) {case 'jpg ': $ _ image = imagecreatefromjpeg ($ _ filename); break; case 'PNG': $ _ image = imagecreatefrompng ($ _ filename); break; case 'GIF': $ _ image = imagecreatefromgif ($ _ filename); break;} // after the source image is collected and copied to the graph again, imagecopyresampled ($ _ new_image, $ _ image, 0, 0, 0, $ _ new_width, $ _ new_height, $ _ width, $ _ height); imagepng ($ _ new_image); imagedestroy ($ _ new_image ); imagedestroy ($ _ image );}

The above describes how to generate image thumbnails in PHP and display them in a browser. it is flexible and practical, and includes some content. I hope to help anyone who is interested in PHP tutorials.

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.