Image sharpening with PHP code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Use PHP to sharpen the image code .? Image types: 1GIF, 2JPG, 3PNG, 4SWF, 5PSD, 6BMP, 7 TIFF (intelbyteorder), 8 TIFF (motorolabyteorder), 9JPC, 10JP2, 11JPX, 12JB2, 13SWC, 14 IFFfunctionGetImageType ($ filen
// 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 );

}

}

Http://www.bkjia.com/PHPjc/318293.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/318293.htmlTechArticle? // Read Image type // 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF (intelbyteorder ), 8 = TIFF (motorolabyteorder), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF functionGetImageType ($ filen...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.