: 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.