源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
查看: 336|回复: 13

[PHP编程] 用PHP实现图象锐化代码

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2007-6-14 00:00:00 | 显示全部楼层 |阅读模式
<?  

//读取图像的类型  

  //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);  

   }  

  }
回复

使用道具 举报

2

主题

1万

回帖

221

积分

中级会员

Rank: 3Rank: 3

积分
221
发表于 2022-10-30 10:44:29 | 显示全部楼层
看到这帖子真是高兴!
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

68

积分

注册会员

Rank: 2

积分
68
发表于 2022-12-29 08:39:29 | 显示全部楼层
哦哦哦哦哦哦哦哦哦
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-4-16 09:18:47 | 显示全部楼层
很不错的源码论坛
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-8-10 00:45:44 | 显示全部楼层
管灌灌灌灌灌灌灌灌灌灌
回复 支持 反对

使用道具 举报

2

主题

1万

回帖

347

积分

中级会员

Rank: 3Rank: 3

积分
347
发表于 2023-8-21 05:36:33 | 显示全部楼层
挺不错的东西
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-10-9 00:55:58 | 显示全部楼层
哟哟哟哟哟以偶
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

115

积分

注册会员

Rank: 2

积分
115
发表于 2023-10-28 00:15:03 | 显示全部楼层
而非为吾问无为谓娃娃
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

68

积分

注册会员

Rank: 2

积分
68
发表于 2023-11-13 18:03:57 | 显示全部楼层
谢谢您的分享!
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-12-1 03:25:40 | 显示全部楼层
好东西可以可以可以可以
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-9-20 15:33 , Processed in 0.172667 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表