PHP Create thumbnails the perfect solution for poor image quality _php tutorial

Source: Internet
Author: User
Tags imagejpeg
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

Pay the full code
[PHP]
/**
* @name Thum thumbnail function
* @param sting $img _name picture path
* @param int $max _width sketch Max width
* @param int $max _height sketch Maximum height
* @param sting $suffix sketch suffix (such as "img_x.jpg" for small plots, "img_m.jpg" for the middle figure, "img_l.jpg" for the 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 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 picture size


Print_r ($new _img_size);
Die (' Test ');
$img _func= ";//Function name
$img _handle= ";//Picture handle
$thum _handle= ";//Sketch 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 changed
$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[' height '], $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 to get thumbnail size
* @param $width 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;//Width now
$now _height= $height;//The current 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 title of the sketch (on the big chart base plus _x)
*/www.2cto.com
function Get_thum_name ($img _name, $suffix) {
$str =explode (' # ', $img _name);
$str 1=explode ('. ', $str [1]);
return $str [0]. ' _ '. $suffix. '. $str 1[1];
}

http://www.bkjia.com/PHPjc/477906.html www.bkjia.com true http://www.bkjia.com/PHPjc/477906.html techarticle 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 of works, if the original diagram, pictures too ...

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