PHP建立縮圖造成圖片品質低上的完美解決辦法

來源:互聯網
上載者:User
PHP建立縮圖造成圖片品質低下的完美解決方案

近期弄個論壇,在首頁要有個排名,顯示評論最多的前十位使用者的列表,列表有個略圖是使用者最新上傳的作品,如果使用原來的圖,圖片過大,首頁載入過慢,因此要使用略圖

以上來使用imagecopyresized這個函數,顯示品質很差

後來改用imagecopyresampled效果明顯改變效果

付上完整代碼

/*** @namethum 縮圖函數* @paramsting$img_name圖片路徑* @paramint$max_width略圖最大寬度* @paramint$max_height略圖最大高度* @paramsting$suffix 略圖尾碼(如"img_x.jpg"代表小圖,"img_m.jpg"代表中圖,"img_l.jpg"代表大圖)* @returnvoid*/function thum($img_name,$max_width,$max_height,$suffix){$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';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);//新的圖片尺寸//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)){//針對php版本大於5.12參數變化後的處理情況$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);//清除控制代碼        imagedestroy($img_handle);//清除控制代碼}/*** @name get_thum_size 獲得縮圖的尺寸* @param$width圖片寬* @param    $height 圖片高* @param$max_width 最大寬度* @param$maxHeight 最大高度* @paramarray $size;*/function 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);return $size;}/***@ name get_thum_name 獲得略圖的名稱(在大圖基礎加_x)*/function get_thum_name($img_name,$suffix){$str=explode('#',$img_name);$str1=explode('.',$str[1]);return $str[0].'_'.$suffix.'.'.$str1[1];}



  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.