这篇文章主要为大家详细介绍了PHP 缩略图锐化函数的简单示例,具有一定的参考价值,可以用来参考一下。
对PHP 缩略图锐化函数感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!
/**
* PHP 缩略图锐化函数
*
* @param
* @arrange 512-笔记网: www.q1010.com
**/
fun_imageconvolution_default($im,-10.9);
fun_imageconvolution_fast($im,0.4);
fun_imageconvolution($im,array(array(-1,-1,-1,),
array(-1,16,-1,),
array(-1,-1,-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, $r, $g, $b,$new_a);
if($new_clr)
imagesetpixel($im, $x, $y, $new_clr);
}
}
return true;
}
function fun_imageconvolution($src, $filter=array(array(-1,-1,-1,),
array(-1,16,-1,),
array(-1,-1,-1)), $filter_div=8, $offset=0){
if (!$src)
return false;
$sx = imagesx($src);
$sy = imagesy($src);
$srcback = ImageCreateTrueColor ($sx, $sy);
ImageCopy($srcback, $src,0,0,0,0,$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, 0), $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[0], $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_r+0, $new_g+0, $new_b+0, $new_a);
}
if (($y >= 0) && ($y < $sy)) {
imagesetpixel($src, $x, $y, $new_pxl);
}
}
}
imagedestroy($srcback);
return true;
}
/*** 来自四海网(www.q1010.com) ***/
php语言图片锐化的三个函数. 执行速度排序, 第一个函数最快. 0.2秒左右. 其它的会慢很多.本文来自:http://www.q1010.com/173/1280-0.html
注:关于PHP 缩略图锐化函数的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:缩略图
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。