Home  >  Article  >  Backend Development  >  PHP创建缩略图造成图片质量低下的完美解决办法

PHP创建缩略图造成图片质量低下的完美解决办法

WBOY
WBOYOriginal
2016-06-13 10:58:371112browse

PHP创建缩略图造成图片质量低下的完美解决方法

    1. /**?
    2. [email protected]?
    3. [email protected]$img_name???图片路径?
    4. [email protected]$max_width??略图最大宽度?
    5. [email protected]$max_height?略图最大高度?
    6. [email protected]$suffix?略图后缀(如"img_x.jpg"代表小图,"img_m.jpg"代表中图,"img_l.jpg"代表大图)?
    7. [email protected]?
    8. */??
    9. function?thum($img_name,$max_width,$max_height,$suffix){??
    10. ????????$img_infos=getimagesize($img_name);??
    11. ????????$img_height=$img_infos[0];//图片高??
    12. ????????$img_width=$img_infos[1];//图片宽??
    13. ????????$img_extension='';//图片后缀名??
    14. ????????switch($img_infos[2]){??
    15. ????????????case?1:??
    16. ????????????????$img_extension='gif';??
    17. ????????????????break;??
    18. ????????????case?2:??
    19. ????????????????$img_extension='jpeg';??
    20. ????????????????break;??
    21. ?????????case?3:??
    22. ????????????????$img_extension='png';??
    23. ????????????????break;??
    24. ????????????default:??
    25. ????????????????$img_extension='jpeg';??
    26. ????????????????break;??
    27. ????????????}??
    28. ????????$new_img_size=get_thum_size($img_width,$img_height,$max_width,$max_height);//新的图片尺寸??
    29. ??????????
    30. ??????????
    31. ????????//print_r($new_img_size);??
    32. ????????//die('test');??
    33. ????????$img_func='';//函数名称??
    34. ????????$img_handle='';//图片句柄??
    35. ????????$thum_handle='';//略图图片句柄??
    36. ????????switch($img_extension){??
    37. ????????????case?'jpg':??
    38. ????????????????$img_handle=imagecreatefromjpeg($img_name);??
    39. ????????????????$img_func='imagejpeg';??
    40. ????????????????break;??
    41. ????????????case?'jpeg':??
    42. ????????????????$img_handle=imagecreatefromjpeg($img_name);??
    43. ????????????????$img_func='imagejpeg';??
    44. ???????????????break;??
    45. ????????????case?'png':??
    46. ????????????????$img_handle=imagecreatefrompng($img_name);??
    47. ????????????????$img_func='imagepng';??
    48. ????????????????break;??
    49. ????????????case?'gif':??
    50. ????????????????$img_handle=imagecreatefromgif($img_name);??
    51. ????????????????$img_func='imagegif';??
    52. ????????????????break;??
    53. ????????????default:??
    54. ????????????????$img_handle=imagecreatefromjpeg($img_name);??
    55. ????????????????$img_func='imagejpeg';?
    56. ????????????????break;??
    57. ????????????}??
    58. ????????/****/????
    59. ????????$quality=100;//图片质量??
    60. ????????if($img_func==='imagepng'?&&?(str_replace('.',?'',?PHP_VERSION)>=?512)){//针对php版本大于5.12参数变化后的处理情况??
    61. ????????????$quality=9;??
    62. ????????????}???
    63. ????????/****/??
    64. ????????$thum_handle=imagecreatetruecolor($new_img_size['height'],$new_img_size['width']);??
    65. ????????if(function_exists('imagecopyresampled')){??
    66. ????????????imagecopyresampled($thum_handle,$img_handle,?0,?0,?0,?0,$new_img_size['height'],$new_img_size['width'],$img_height,$img_width);??
    67. ????????????}else{??
    68. ????????????????imagecopyresized($thum_handle,$img_handle,?0,?0,?0,?0,$new_img_size['height'],$new_img_size['width'],$img_height,$img_width);??
    69. ????????????}??
    70. ????????call_user_func_array($img_func,array($thum_handle,get_thum_name($img_name,$suffix),$quality));??
    71. ????????imagedestroy($thum_handle);//清除句柄??
    72. ????????imagedestroy($img_handle);//清除句柄??
    73. ????}??
    74. ??
    75. /**?
    76. [email protected]_thum_size?获得缩略图的尺寸?
    77. [email protected]$width??图片宽?
    78. [email protected]$height?图片高?
    79. [email protected]$max_width?最大宽度?
    80. [email protected]$maxHeight?最大高度?
    81. [email protected]$size;?
    82. */??
    83. function?get_thum_size($width,$height,$max_width,$max_height){??
    84. ????$now_width=$width;//现在的宽度??
    85. ????$now_height=$height;//现在的高度??
    86. ????$size=
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn