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

前端之家收集整理的这篇文章主要介绍了PHP 缩略图锐化函数的简单示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
PHP 缩略图锐化函数感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!
跟据PHP版本不同,可能效果不同,请把三个函数都试试,看哪个效果更佳就选择用哪个. 让缩略图不再模糊.

/**
 * PHP 缩略图锐化函数
 *
 * @param 
 * @arrange 512-笔记网: www.www.jb51.cc
 **/
fun_imageconvolution_default($im,-10.9);
fun_imageconvolution_fast($im,0.4);
fun_imageconvolution($im,array(array(-1,-1,),array(-1,16,-1)),8,0);
# $im 为资源值 imagecreatetruecolor() 返回.
function fun_imageconvolution_default(&$im,$degree=-10.9){
if (!$im)
return false;
imagefilter($im,IMG_FILTER_SMOOTH,-10.9);
imageantialias ($im,true);
return true;
}
function fun_imageconvolution_fast(&$im,$degree=0.4){
if (!$im)
return false;
for ($x = imagesx($im)-1; $x>0; $x--){
for ($y = imagesy($im)-1; $y>0; $y--){
$clr1 = imagecolorsforindex($im,$alpha = imagecolorat($im,$x-1,$y-1)); 
$clr2 = imagecolorsforindex($im,imagecolorat($im,$x,$y)); 
$r = intval($clr2["red"]+$degree*($clr2["red"]-$clr1["red"])); 
$g = intval($clr2["green"]+$degree*($clr2["green"]-$clr1["green"])); 
$b = intval($clr2["blue"]+$degree*($clr2["blue"]-$clr1["blue"])); 
$r = min(255,max($r,0)); 
$g = min(255,max($g,0)); 
$b = min(255,max($b,0)); 
$new_a = $alpha >> 24;
if (($new_clr=ImageColorAllocateAlpha($im,$r,$g,$b,$new_a)) == -1) 
$new_clr = ImageColorClosestAlpha($im,$new_a);
if($new_clr)
imagesetpixel($im,$y,$new_clr); 
}
}
return true;
}
function fun_imageconvolution($src,$filter=array(array(-1,$filter_div=8,$offset=0){
if (!$src)
return false;
$sx = imagesx($src);
$sy = imagesy($src);
$srcback = ImageCreateTrueColor ($sx,$sy);
ImageCopy($srcback,$src,$sx,$sy);
if(!$srcback)
return false;
#FIX HERE
#$pxl array was the problem so simply set it with very low values
$pxl = array(1,1);
#this little fix worked for me as the undefined array threw out errors
for ($y=0; $y<$sy; ++$y){
for($x=0; $x<$sx; ++$x){
$new_r = $new_g = $new_b = 0;
for ($j=0; $j<3; ++$j) {
$yv = min(max($y - 1 + $j,0),$sy - 1);
for ($i=0; $i<3; ++$i) {
$pxl = array(min(max($x - 1 + $i,$sx - 1),$yv);
$rgb = imagecolorat($srcback,$pxl[0],$pxl[1]);
$new_r += (($rgb >> 16) & 0xFF) * $filter[$j][$i];
$new_g += (($rgb >> 8) & 0xFF) * $filter[$j][$i];
$new_b += ($rgb & 0xFF) * $filter[$j][$i];
}
}
$new_r = ($new_r/$filter_div)+$offset;
$new_g = ($new_g/$filter_div)+$offset;
$new_b = ($new_b/$filter_div)+$offset;
$new_r = ($new_r > 255)? 255 : (($new_r < 0)? 0:$new_r);
$new_g = ($new_g > 255)? 255 : (($new_g < 0)? 0:$new_g);
$new_b = ($new_b > 255)? 255 : (($new_b < 0)? 0:$new_b);
$alpha = imagecolorat($srcback,$pxl[1]);
$new_a = $alpha >> 24;
$new_pxl = ImageColorAllocateAlpha($src,$new_r+0,$new_g+0,$new_b+0,$new_a);
if ($new_pxl == -1) {
$new_pxl = ImageColorClosestAlpha($src,$new_a);
}
if (($y >= 0) && ($y < $sy)) {
imagesetpixel($src,$new_pxl);
}
}
}
imagedestroy($srcback);
return true;
}
/***   来自编程之家 jb51.cc(jb51.cc)   ***/
PHP语言图片锐化的三个函数. 执行速度排序,第一个函数最快. 0.2秒左右. 其它的会慢很多.
功能与作用:
生成缩略图时,会令图片非常模糊不清,这是PHPcms,discuz所遇到的问题,而纵观QQ的缩略图却清晰得多,(可能它用C语言处理了). 这三个函数,任何一个都可以令图片增加清晰度,也就是提升锐度 原文链接:https://www.f2er.com/php/528101.html

猜你在找的PHP相关文章