PHP中实现图片的锐化
<?
//读取图像的类型
//1 = gif, 2 = jpg, 3 = png, 4 = swf, 5 = psd, 6 = bmp, 7 = tiff(intel byte order), 8 = tiff(motorola byte order), 9 = jpc, 10 = jp2, 11 = jpx, 12 = jb2, 13 = swc, 14 = iff
function getimagetype($filename) {return (($imginfo=@getimagesize($filename))!=null ? $imginfo[2] : null);}
//图像锐化
//$scr_im:图像资源句柄,$degree:锐化度数
function sharp(&$src_im, &$dst_im, $degree)
{
$src_x = imagesx($src_im);
$src_y = imagesy($src_im);
//$dst_im = imagecreate($src_x, $src_y);
//imagecopy($dst_im, $src_im, 0, 0, 0, 0, $src_x, $src_y);
$cnt = 0;
for ($x=1; $x<$src_x; $x++)
for ($y=1; $y<$src_y; $y++)
{
$src_clr1 = imagecolorsforindex($src_im, imagecolorat($src_im, $x-1, $y-1));
$src_clr2 = imagecolorsforindex($src_im, imagecolorat($src_im, $x, $y));
$r = intval($src_clr2["red"]+$degree*($src_clr2["red"]-$src_clr1["red"]));
$g = intval($src_clr2["green"]+$degree*($src_clr2["green"]-$src_clr1["green"]));
$b = intval($src_clr2["blue"]+$degree*($src_clr2["blue"]-$src_clr1["blue"]));
$r = min(255, max($r, 0));
$g = min(255, max($g, 0));
$b = min(255, max($b, 0));
//echo "r:$r, g:$g, b:$b<br/>";
if (($dst_clr=imagecolorexact($dst_im, $r, $g, $b))==-1)
$dst_clr = imagecolorallocate($dst_im, $r, $g, $b);
$cnt++;
if ($dst_clr==-1) die("color allocate faile at $x, $y ($cnt).");
imagesetpixel($dst_im, $x, $y, $dst_clr);
}
return $dst_im;
}
$imagefunctions = array("imagecreatefromwbmp", "imagecreatefromgif", "imagecreatefromjpeg", "imagecreatefrompng");
if (!empty($_post["imagename"]))
{
set_time_limit(10*60);
if (($imagetype=getimagetype($_post["imagename"]))==false)
die("指定文件不存在或不是有效的图片或不支持类型!");
if ($imagetype==6) $imagetype = 0;
if ($imagetype>3) die("不支持的图片类型!");
$im1 = $imagefunctions[$imagetype]($_post["imagename"]);
$im2 = $imagefunctions[$imagetype]($_post["imagename"]);
//print_r(imagecolorsforindex($im, imagecolorat($im, 10, 10)));
sharp($im1, $im2, $_post["degree"]);
header("content-type: image/png");
imagepng($im2);
imagedestroy($im1);
imagedestroy($im2);
}
?>
<form name="formname" action="" method="post">
请输入图片的本地路径或url:<br/>
<input name="imagename" type="text" value="<?=$_post["imagename"]?>" size=32><br/>
锐化度数(例:0.6、3.0):<br/>
<input name="degree" type="text" value="<?=$_post["degree"]?>"><br/>
<input type="submit" value="提交">
</form>
改了一下,省了一个$im:
function sharp2(&$im, $degree)
{
$cnt = 0;
for ($x=imagesx($im)-1; $x>0; $x--)
for ($y=imagesy($im)-1; $y>0; $y--)
{
$clr1 = imagecolorsforindex($im, 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));
//echo "r:$r, g:$g, b:$b<br>";
if (($new_clr=imagecolorexact($im, $r, $g, $b))==-1)
$new_clr = imagecolorallocate($im, $r, $g, $b);
$cnt++;
if ($new_clr==-1) die("color allocate faile at $x, $y ($cnt).");
imagesetpixel($im, $x, $y, $new_clr);
}
}
上一篇: 用Socket发送电子邮件