PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php生成缩略图质量较差解决方法代码示例大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

近期弄个论坛,在首页要有个排名,显示评论最多的前十位用户的列表,列表有个略图是用户最新上传的作品,如果使用原来的图,图片过大,首页加载过慢,因此要使用略图

以上来使用imagecopyresized这个函数显示质量很差如图

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

后来改用imagecopyresampled效果明显改变效果如图

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

附上完整代码

//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);//清除句柄 

}
/**

  • @name get_thum_size 获得缩略图的尺寸
  • @param $width 图片
  • @param $height 图片
  • @param $max_width 最大宽度
  • @param $maxHeight 最大高度
  • @param array $size;
    /
    function get_thum_size($width,$height,$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);
    return $size;
    }
    /*
    @ name get_thum_name 获得略图的名称(在大图基础加_X)
    */
    function get_thum_name($img_name,$suffiX){
    $str=explode('#',$imgName);
    $str1=explode('.',$str[1]);
    return $str[0].'
    '.$suffix.'.'.$str1[1];
    }

大佬总结

以上是大佬教程为你收集整理的php生成缩略图质量较差解决方法代码示例全部内容,希望文章能够帮你解决php生成缩略图质量较差解决方法代码示例所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。