大佬教程收集整理的这篇文章主要介绍了php生成缩略图质量较差解决方法代码示例,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
近期弄个论坛,在首页要有个排名,显示评论最多的前十位用户的列表,列表有个略图是用户最新上传的作品,如果使用原来的图,图片过大,首页加载过慢,因此要使用略图
以上来使用imagecopyresized这个函数,显示质量很差如图
后来改用imagecopyresampled效果明显改变效果如图
附上完整代码:
//print_r($new_img_sizE);
//die('test');
$img_func='';//函数名称
$img_handle='';//图片句柄
$thum_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;//图片质量
if($img_func==='imagepng' && (str_replace('.','',php_VERSION)>= 512)){//针对<a href="http://code.js-code.com/tag/php/" target="_blank" class="keywords">php</a>版本大于5.12参数变化后的处理情况
$quality=9;
}
/****/
$thum_handle=imagecreatetruecolor($new_img_size['height'],$new_img_size['width']);
if(function_exists('imagecopyresampled')){
imagecopyresampled($thum_handle,$img_handle,$new_img_size['height'],$new_img_size['width'],$img_width);
}else{
imagecopyresized($thum_handle,$img_width);
}
call_user_func_array($img_func,array($thum_handle,get_thum_name($img_name,$suffiX),$quality));
imagedestroy($thum_handlE);//清除句柄
imagedestroy($img_handlE);//清除句柄
}
/**
以上是大佬教程为你收集整理的php生成缩略图质量较差解决方法代码示例全部内容,希望文章能够帮你解决php生成缩略图质量较差解决方法代码示例所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。