宝哥软件园

php生成的缩略图质量差的解决方案的代码示例

编辑:宝哥软件园 来源:互联网 时间:2021-09-02

在不久的将来获得一个论坛。首页要有排名,显示评论最多的前十名用户名单。列表中有一个缩略图,是用户上传的最新作品。如果用的是原图,图片太大,首页加载太慢,就用缩略图。

使用上面的imagecopyresized函数,显示质量非常差,如图所示

后来我切换到imagecopy重采样,效果变化明显,如图

附上完整的代码:

/** * @name thum缩略图函数* @param sting $img_name图片路径* @param int $max_width略图最大宽度* @param int $max_height略图最大高度* @ param sting $后缀略图后缀(如' img_x.jpg '代表小图,' img_m.jpg '代表中图,' img_l.jpg '代表大图)* @ return void */function thum($ img _ name,$max_width,$max_height,$后缀){ $ img _ infos=getimagesize($ img _ name);$ img _ height=$ img _ infos[0];//图片高$ img _ width=$ img _ infos[1];//图片宽$ img _ extension=//图片后缀名switch($ img _ infos[2]){ case 1: $ img _ extension=' gif ';打破;case 2: $ img _ extension=' JPEG ';打破;案例3: $ img _ extension=' png打破;默认值: $ img _ extension=' JPEG打破;} $ new _ img _ size=get _ thum _ size($ img _ width,$img_height,$max_width,$ max _ height);//新的图片尺寸//print _ r($ new _ img _ size);//die(' test ');$ img _ func=//函数名称$ img _ handle=//图片句柄$ thum _ handle=//略图图片句柄switch($ img _ extension){ case ' jpg ' : $ img _ handle=imagecreatefrom JPEG($ img _ name);$ img _ func=' imagejpeg打破;case ' JPEG ' : $ img _ handle=imagecreatefrom JPEG($ img _ name);$ img _ func=' imagejpeg打破;case ' png ' : $ img _ handle=imagecreatefrompng($ img _ name);$ img _ func=' imagepng打破;case ' gif ' : $ img _ handle=imagecreatefrom gif($ img _ name);$ img _ func=' imagegif打破;默认值: $ img _ handle=imagecreatefrom JPEG($ img _ name);$ img _ func=' imagejpeg打破;}/* * * */$ quality=100;//图片质量if($ img _ func==' image png '(str _ replace(')。),'',PHP_VERSION)=512)){//针对服务器端编程语言(专业超文本预处理器的缩写)版本大于5.12参数变化后的处理情况$ quality=9;}/* * * */$ thum _ handle=imagecreatetrue color($ new _ img _ size[' height '],$ new _ img _ size[' width ']);if(function _ exists(' image copy re sampled '){ image copy re sampled($ thum _ handle,$img_handle,0,0,0,$new_img_size['height'],$new_img_size['width'],$img_height,$ img _ width);} else { imagecopy已调整大小($ 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,$后缀),$ quality));图像破坏($ thum _ handle);//清除句柄图像销毁($ img _ handle);//清除句柄} /** * @name get_thum_size获得缩略图的尺寸* @param $width图片宽* @ param $高度图片高* @param $max_width最大宽度* @param $maxHeight最大高度* @param数组$ size*/函数get_thum_size($width,$height,$max_width,$ max _ height){ $ now _ width=$ width;//现在的宽度$ now _ height=$ height//现在的高度$ size=array();if($now_width$max_width){//如果现在宽度大于最大宽度$ now _ height *=number _ format($ max _ width/$ width,4);$ now _ width=$ max _ width } if($ now _ height $ max _ height){//如果现在高度大于最大高度$ now _ width *=number _ format($ max _ height/$ now _ height,4);$ now _ height=$ max _ height } $ size[' width ']=floor($ now _ width);$ size[' height ']=floor($ now _ height);返回$ size} /** *@ name get_thum_name获得略图的名称(在大图基础加_x) */函数get_thum_name($img_name,$后缀){ $str=explode('# ',$ img _ name);$ str1=爆炸('.$ str[1]);返回$str[0]._'.$后缀。'.$圣R1[1];} 总结

以上就是本文关于服务器端编程语言(专业超文本预处理器的缩写)生成缩略图质量较差解决方法代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

更多资讯
游戏推荐
更多+