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

Source: Internet
Author: User
Tags imagejpeg

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
[Php]
/**
* @ 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 a graph, and "img_l.jpg" indicates a large 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]; // Image Width
$ Img_extension = ''; // image suffix
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 = ''; // a thumbnail 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 = imagecreatefrompng ($ 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 of php version changes greater than 5.12
$ 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 the handle
}
 
/**
* @ Name get_thum_size obtains the size of the thumbnail.
* @ 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; // current width
$ Now_height = $ height; // 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 Basic Graph)
*/Www.2cto.com
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.