The perfect solution to low image quality caused by creating thumbnails in PHP

Source: Internet
Author: User
Tags imagejpeg
A perfect solution for low image quality caused by creating thumbnails in PHP. Recently, a forum will be created. The top 10 users with the most comments will be listed on the homepage, A thumbnail in the list is the latest file uploaded by the user. if the original image is used, the image is too large and the homepage loading is too slow, you must use the imagecopyresized function above the thumbnail, poor display quality. later, I switched to imagecopyresampled. The effect was significantly changed. I paid for the complete code. PHP creates a thumbnail. this is a perfect solution for low image 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

/*** @ Namethum thumbnail function * @ paramsting $ img_name Image path * @ paramint $ max_width the maximum width of a thumbnail * @ paramint $ max_height the maximum height of a thumbnail * @ paramsting $ suffix the suffix of a thumbnail (for example "img_x.jpg" indicates a thumbnail, "img_m.jpg" indicates the graph, "img_l.jpg" indicates the big image. * @ returnvoid */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 * @ paramarray $ 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];}



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.