PHP中实现图片的锐化办法

  • A+
所属分类:PHP编程

PHP中实现图片锐化办法

  1. <?
  2.   //读取图像的类型
  3.   //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
  4.   function GetImageType($filename) {return (($imginfo=@getimagesize($filename))!=null ? $imginfo[2] : null);}
  5.   //图像锐化
  6.   //$scr_im:图像资源句柄,$degree:锐化度数
  7.   function Sharp(&$src_im, &$dst_im, $degree)
  8.   {
  9.    $src_x = imagesx($src_im);
  10.    $src_y = imagesy($src_im);
  11.    //$dst_im = imagecreate($src_x, $src_y);
  12.    //imagecopy($dst_im, $src_im, 0, 0, 0, 0, $src_x, $src_y);
  13.    $cnt = 0;
  14.    for ($x=1; $x<$src_x; $x++)
  15.    for ($y=1; $y<$src_y; $y++)
  16.    {
  17.    $src_clr1 = imagecolorsforindex($src_im, imagecolorat($src_im, $x-1, $y-1));
  18.    $src_clr2 = imagecolorsforindex($src_im, imagecolorat($src_im, $x, $y));
  19.    $r = intval($src_clr2["red"]+$degree*($src_clr2["red"]-$src_clr1["red"]));
  20.    $g = intval($src_clr2["green"]+$degree*($src_clr2["green"]-$src_clr1["green"]));
  21.    $b = intval($src_clr2["blue"]+$degree*($src_clr2["blue"]-$src_clr1["blue"]));
  22.    $r = min(255, max($r, 0));
  23.    $g = min(255, max($g, 0));
  24.    $b = min(255, max($b, 0));
  25.    //echo "r:$r, g:$g, b:$b<br/>";
  26.    if (($dst_clr=imagecolorexact($dst_im, $r, $g, $b))==-1)
  27.    $dst_clr = Imagecolorallocate($dst_im, $r, $g, $b);
  28.    $cnt++;
  29.    if ($dst_clr==-1) die("color allocate faile at $x, $y ($cnt).");
  30.    imagesetpixel($dst_im, $x, $y, $dst_clr);
  31.    }
  32.    return $dst_im;
  33.   }
  34.   $ImageFunctions = array("imagecreatefromwbmp""imagecreatefromgif""imagecreatefromjpeg""imagecreatefrompng");
  35.   if (!empty($_POST["ImageName"]))
  36.   {
  37.    set_time_limit(10*60);
  38.    if (($ImageType=GetImageType($_POST["ImageName"]))==false)
  39.    die("指定文件不存在或不是有效的图片或不支持类型!");
  40.    if ($ImageType==6) $ImageType = 0;
  41.    if ($ImageType>3) die("不支持的图片类型!");
  42.    $im1 = $ImageFunctions[$ImageType]($_POST["ImageName"]);
  43.    $im2 = $ImageFunctions[$ImageType]($_POST["ImageName"]);
  44.    //print_r(imagecolorsforindex($im, imagecolorat($im, 10, 10)));
  45.    Sharp($im1, $im2, $_POST["Degree"]);
  46.    header("Content-type: image/png");
  47.    imagepng($im2);
  48.    imagedestroy($im1);
  49.    imagedestroy($im2);
  50.   }
  51.   ?>
  52.   <form name="FormName" action="" method="post">
  53.   请输入图片的本地路径或URL:<br/>
  54.   <input name="ImageName" type="text" value="<?=$_POST["ImageName"]?>" size=32><br/>
  55.   锐化度数(例:0.63.0):<br/>
  56.   <input name="Degree" type="text" value="<?=$_POST["Degree"]?>"><br/>
  57.   <input type="submit" value="提交">
  58.   </form>
  59.    改了一下,省了一个$im:
  60.    function Sharp2(&$im, $degree)
  61.   {
  62.    $cnt = 0;
  63.    for ($x=imagesx($im)-1; $x>0; $x--)
  64.    for ($y=imagesy($im)-1; $y>0; $y--)
  65.    {
  66.    $clr1 = imagecolorsforindex($im, imagecolorat($im, $x-1, $y-1));
  67.    $clr2 = imagecolorsforindex($im, imagecolorat($im, $x, $y));
  68.    $r = intval($clr2["red"]+$degree*($clr2["red"]-$clr1["red"]));
  69.    $g = intval($clr2["green"]+$degree*($clr2["green"]-$clr1["green"]));
  70.    $b = intval($clr2["blue"]+$degree*($clr2["blue"]-$clr1["blue"]));
  71.    $r = min(255, max($r, 0));
  72.    $g = min(255, max($g, 0));
  73.    $b = min(255, max($b, 0));
  74.    //echo "r:$r, g:$g, b:$b<br>";
  75.    if (($new_clr=imagecolorexact($im, $r, $g, $b))==-1)
  76.    $new_clr = Imagecolorallocate($im, $r, $g, $b);
  77.    $cnt++;
  78.    if ($new_clr==-1) die("color allocate faile at $x, $y ($cnt).");
  79.    imagesetpixel($im, $x, $y, $new_clr);
  80.    }
  81.   }