PHP中实现图片的锐化

  • 来源: 互联网 作者: rocket   2008-03-17/09:56
  • <?

    //读取图像的类型

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

    $g = intval($src_clr2));

    $b = intval($src_clr2));

    $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))

    {

    set_time_limit(10*60);

    if (($ImageType=GetImageType($_POST))==false)

    die("指定文件不存在或不是有效的图片或不支持类型!");

    if ($ImageType==6) $ImageType = 0;

    if ($ImageType>3) die("不支持的图片类型!");

    $im1 = $ImageFunctions);

    $im2 = $ImageFunctions);

    //print_r(imagecolorsforindex($im, imagecolorat($im, 10, 10)));

    Sharp($im1, $im2, $_POST);

    header("Content-type: image/png");

    imagepng($im2);

    imagedestroy($im1);

    imagedestroy($im2);

    }

    ?>

    <form name="FormName" action="" method="post">

    请输入图片的本地路径或



    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多