PHP generated thumbnail quality poor solution

Source: Internet
Author: User
Tags imagejpeg
Believe that the large in the development of the problem of poor thumbnail quality, this article mainly introduces PHP generated thumbnail quality solution, has a certain value for reference, the need for friends can refer to, hope to help everyone.

Recently get a forum, on the first page to have a ranking, showing the top 10 users of the list of comments, the list has a sketch is the user's latest upload works, if the original diagram, the picture is too large, the home page loaded too slow, so to use the sketch

Above to use imagecopyresized this function, the display quality is very poor

Later use imagecopyresampled effect to change the effect obviously

Enclose the complete code:


/** * @name thum thumbnail function * @param sting $img _name picture path * @param int $max _width sketch maximum width * @param int $max _height sketch Most Large Height * @param sting $suffix sketch suffix (e.g. "img_x.jpg" for small plots, "img_m.jpg" for the middle figure, "img_l.jpg" for larger image) * @return void */function Thum ($img _     Name, $max _width, $max _height, $suffix) {$img _infos=getimagesize ($img _name); $img _height= $img _infos[0];//Pictures High $img _width= $img _infos[1];//picture width $img _extension= ";//Picture 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 picture size//print_r ($new _img_size);     Die (' Test ');         $img _func= ";//Function name $img _handle=";//Picture handle $thum _handle= ";//thumbnail picture 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;//picture quality if ($img _func=== ' imagepng ' && (Str_replace ('. ', ', php_version) >= 512       ) {//For processing after PHP version is greater than 5.12 parameter $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, 0, $new _img_size[' H       Eight '], $new _img_size[' width '), $img _height, $img _width);         }else{Imagecopyresized ($thum _handle, $img _handle, 0, 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 handle Imagedestroy ($img _handle),//Clear Handle}/** * @name get_thum_size get thumbnail size * @param $w Idth Picture Width * @param $height picture high * @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;//   Now the height $size =array ();     if ($now _width> $max _width) {//If the width is now greater than the maximum width $now _height*=number_format ($max _width/$width, 4);     $now _width= $max _width;     if ($now _height> $max _height) {//If the height is now 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 sketch (on the big picture base plus _x) */function Get_thum_name ($img _name, $suffix) {$str =explode (' # ', $img _name);     $str 1=explode ('. ', $str [1]); return $str [0]. ' _ '. $suffix. '.   $str 1[1]; }

How did everyone learn? Give it a try.

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.