Sharpen images in PHP
// Read Image type
// 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 );}
// Sharpen the image
// $ Scr_im: image resource handle, $ degree: sharpen
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
";
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 ("The specified file does not exist or is not a valid image or does not support the type! ");
If ($ ImageType = 6) $ ImageType = 0;
If ($ ImageType> 3) die ("unsupported image type! ");
$ 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 );
}
?>
Changed the value and saved a $ 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
";
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 );
}
}