PHP 缩略图锐化函数的简单示例

前端之家收集整理的这篇文章主要介绍了PHP 缩略图锐化函数的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP 缩略图锐化函数感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
跟据PHP版本不同,可能效果不同,请把三个函数都试试,看哪个效果更佳就选择用哪个. 让缩略图不再模糊.
  1. /**
  2. * PHP 缩略图锐化函数
  3. *
  4. * @param
  5. * @arrange 512-笔记网: www.www.jb51.cc
  6. **/
  7. fun_imageconvolution_default($im,-10.9);
  8. fun_imageconvolution_fast($im,0.4);
  9. fun_imageconvolution($im,array(array(-1,-1,),array(-1,16,-1)),8,0);
  10. # $im 为资源值 imagecreatetruecolor() 返回.
  11. function fun_imageconvolution_default(&$im,$degree=-10.9){
  12. if (!$im)
  13. return false;
  14. imagefilter($im,IMG_FILTER_SMOOTH,-10.9);
  15. imageantialias ($im,true);
  16. return true;
  17. }
  18. function fun_imageconvolution_fast(&$im,$degree=0.4){
  19. if (!$im)
  20. return false;
  21. for ($x = imagesx($im)-1; $x>0; $x--){
  22. for ($y = imagesy($im)-1; $y>0; $y--){
  23. $clr1 = imagecolorsforindex($im,$alpha = imagecolorat($im,$x-1,$y-1));
  24. $clr2 = imagecolorsforindex($im,imagecolorat($im,$x,$y));
  25. $r = intval($clr2["red"]+$degree*($clr2["red"]-$clr1["red"]));
  26. $g = intval($clr2["green"]+$degree*($clr2["green"]-$clr1["green"]));
  27. $b = intval($clr2["blue"]+$degree*($clr2["blue"]-$clr1["blue"]));
  28. $r = min(255,max($r,0));
  29. $g = min(255,max($g,0));
  30. $b = min(255,max($b,0));
  31. $new_a = $alpha >> 24;
  32. if (($new_clr=ImageColorAllocateAlpha($im,$r,$g,$b,$new_a)) == -1)
  33. $new_clr = ImageColorClosestAlpha($im,$new_a);
  34. if($new_clr)
  35. imagesetpixel($im,$y,$new_clr);
  36. }
  37. }
  38. return true;
  39. }
  40. function fun_imageconvolution($src,$filter=array(array(-1,$filter_div=8,$offset=0){
  41. if (!$src)
  42. return false;
  43. $sx = imagesx($src);
  44. $sy = imagesy($src);
  45. $srcback = ImageCreateTrueColor ($sx,$sy);
  46. ImageCopy($srcback,$src,$sx,$sy);
  47. if(!$srcback)
  48. return false;
  49. #FIX HERE
  50. #$pxl array was the problem so simply set it with very low values
  51. $pxl = array(1,1);
  52. #this little fix worked for me as the undefined array threw out errors
  53. for ($y=0; $y<$sy; ++$y){
  54. for($x=0; $x<$sx; ++$x){
  55. $new_r = $new_g = $new_b = 0;
  56. for ($j=0; $j<3; ++$j) {
  57. $yv = min(max($y - 1 + $j,0),$sy - 1);
  58. for ($i=0; $i<3; ++$i) {
  59. $pxl = array(min(max($x - 1 + $i,$sx - 1),$yv);
  60. $rgb = imagecolorat($srcback,$pxl[0],$pxl[1]);
  61. $new_r += (($rgb >> 16) & 0xFF) * $filter[$j][$i];
  62. $new_g += (($rgb >> 8) & 0xFF) * $filter[$j][$i];
  63. $new_b += ($rgb & 0xFF) * $filter[$j][$i];
  64. }
  65. }
  66. $new_r = ($new_r/$filter_div)+$offset;
  67. $new_g = ($new_g/$filter_div)+$offset;
  68. $new_b = ($new_b/$filter_div)+$offset;
  69. $new_r = ($new_r > 255)? 255 : (($new_r < 0)? 0:$new_r);
  70. $new_g = ($new_g > 255)? 255 : (($new_g < 0)? 0:$new_g);
  71. $new_b = ($new_b > 255)? 255 : (($new_b < 0)? 0:$new_b);
  72. $alpha = imagecolorat($srcback,$pxl[1]);
  73. $new_a = $alpha >> 24;
  74. $new_pxl = ImageColorAllocateAlpha($src,$new_r+0,$new_g+0,$new_b+0,$new_a);
  75. if ($new_pxl == -1) {
  76. $new_pxl = ImageColorClosestAlpha($src,$new_a);
  77. }
  78. if (($y >= 0) && ($y < $sy)) {
  79. imagesetpixel($src,$new_pxl);
  80. }
  81. }
  82. }
  83. imagedestroy($srcback);
  84. return true;
  85. }
  86. /*** 来自编程之家 jb51.cc(jb51.cc) ***/
PHP语言图片锐化的三个函数. 执行速度排序,第一个函数最快. 0.2秒左右. 其它的会慢很多.
功能与作用:
生成缩略图时,会令图片非常模糊不清,这是PHPcms,discuz所遇到的问题,而纵观QQ的缩略图却清晰得多,(可能它用C语言处理了). 这三个函数,任何一个都可以令图片增加清晰度,也就是提升锐度

猜你在找的PHP相关文章