Sample Code for generating thumbnails with php with poor quality

Source: Internet
Author: User
Tags imagejpeg

Sample Code for generating thumbnails with php with poor quality

I recently got a forum and asked for a ranking on the homepage to display the list of the top 10 users with the most comments. a thumbnail of the list is the latest File Uploaded By the user. If I use the original image, the picture is too large and the homepage loading is too slow. Therefore, you need to use a thumbnail.

The above uses the imagecopyresized function, which shows poor quality.

Later, the imagecopyresampled effect was significantly changed.

Complete code is attached:

/*** @ Name thum thumbnail function * @ param sting $ img_name image path * @ param int $ max_width: the maximum width of a thumbnail * @ param int $ max_height: the maximum height of a thumbnail * @ param sting $ suffix thumbnail suffix (for example, "img_x.jpg" indicates a thumbnail, "img_m.jpg" indicates the graph, "img_l.jpg" indicates the big image. * @ return void */function thum ($ img_name, $ max_width, $ max_height, $ suffix) {$ img_infos = getimagesize ($ img_name); $ img_height = $ img_infos [0]; // picture height $ img_width = $ img_infos [1]; // picture width $ img_extension = ''; // picture extension name switch ($ img_infos [2]) {case 1: $ img_extension = 'gif'; break; case 2: $ img_extension = 'jpeg '; break; case 3: $ img_extension = 'png'; break; default: $ img_extension = 'jpeg '; break ;} $ new_img_size = get_thum_size ($ img_width, $ img_height, $ max_width, $ max_height); // new image size // print_r ($ new_img_size ); // die ('test'); $ img_func = ''; // function name $ img_handle =''; // image handle $ thum_handle = ''; // skip graph image handle switch ($ img_extension) {case 'jpg ': $ img_handle = imagecreatefromjpeg ($ img_name); $ img_func = 'imagejpeg'; break; case 'jpeg ': $ img_handle = imagecreatefromjpeg ($ img_name); $ img_func = 'imagejpeg '; break; case 'png': $ img_handle = hour ($ img_name); $ img_func = 'imagepng '; break; case 'gif': $ img_handle = imagecreatefromgif ($ img_name); $ img_func = 'imagegif'; break; default: $ img_handle = imagecreatefromjpeg ($ img_name ); $ img_func = 'imagejpeg '; break;}/*****/$ quality = 100; // image quality if ($ img_func === 'imagepng '& (str_replace ('. ', '', PHP_VERSION)> = 512) {// processing status after the php version is greater than 5.12 parameter change $ quality = 9 ;} /***/$ thum_handle = imagecreatetruecolor ($ new_img_size ['height'], $ new_img_size ['width']); if (function_exists ('imagecopyresampled ')) {imagecopyresampled ($ thum_handle, $ img_handle, 0, 0, 0, $ new_img_size ['height'], $ new_img_size ['width'], $ img_height, $ img_width);} else {imagecopyresized ($ thum_handle, $ img_handle, 0, 0, 0, $ new_img_size ['height'], $ new_img_size ['width'], $ img_height, $ img_width);} call_user_func_array ($ img_func, array ($ thum_handle, get_thum_name ($ img_name, $ suffix), $ quality); imagedestroy ($ thum_handle ); // clear the handle imagedestroy ($ img_handle ); // clear handle}/*** @ name get_thum_size obtain the thumbnail size * @ param $ width Image width * @ param $ height image height * @ param $ max_width maximum width * @ param $ maxHeight maximum height * @ param array $ size; */function get_thum_size ($ width, $ height, $ max_width, $ max_height) {$ now_width = $ width; // The current width $ now_height = $ height; // The current height $ size = array (); if ($ now_width> $ max_width) {// if the current width is greater than the maximum width $ now_height * = number_format ($ max_width/$ width, 4); $ now_width = $ max_width;} if ($ now_height> $ max_height) {// if the current height is greater than the maximum height $ now_width * = number_format ($ max_height/$ now_height, 4); $ now_height = $ max_height ;} $ size ['width'] = floor ($ now_width); $ size ['height'] = floor ($ now_height); return $ size ;} /*** @ name get_thum_name get the name of the thumbnail (add _ x to the base of the larger graph) */function get_thum_name ($ img_name, $ suffix) {$ str = explode ('#', $ img_name); $ str1 = explode ('. ', $ str [1]); return $ str [0]. '_'. $ suffix. '. '. $ str1 [1];}

Summary

The above is all the content of the sample code about the poor quality solution for generating thumbnails in php. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.