Share a piece of program code: Use PHP for image sharpening [absolutely easy to use]

Source: Internet
Author: User
Tags imagejpeg

Create a thumbnail program for the album. After uploading an image, you must automatically generate a thumbnail and sharpen it.
I searched the internet and the results were not good at all. Why is the same code in the world?
No way, I had to go to a foreigner to Tao Yi Tao, and found that this is very good. Now I want to share it with you.

<? PHP

Function gdthrowerror ($ message)
{
// Don't throw plain text errors in a function that's supposed to return an image
// As per "the principle of least astonishment": the user is expecting
// A JPEG, and they'll be less astonished to get a jpeg error message
// Than they wocould be if they got plain text. Imagine this was called
// From an image tag, which makes sense, that's how you use images:
// Plain text errors won't even reach most users, they 'd have to copy
// The img src into the address bar. It probably wont 'even occur
// Them, as it's not typically helpful to view the source of an image
// Resource.
$ Font = 2;
// Create a canvas with a bit of padding
$ Errimg = imagecreate (imagefontwidth ($ font) * strlen ($ message) + 20, imagefontheight ($ font) + 10 );
$ BG = imagecolorallocate ($ errimg, 255,255,255 );
$ Textcol = imagecolorallocate ($ errimg, 0, 0 );
Imagestring ($ errimg, 2, 10, 5, $ message, $ textcol );
Header ('content-type: image/JPEG ');
Imagejpeg ($ errimg );
Imagedestroy ($ errimg );
}

Function gdmakeappslooklikecrap ($ target)
{
// Image dimensions are no longer needed (see below), but getimagesize can do some simple Validation
If ($ dims = @ getimagesize ($ target) === false | $ dims ['mime ']! = 'Image/JPEG ')
{
Gdthrowerror ('the file you specified couldn \'t be found or is not a valid JPEG image. Make sure you spelled it correctly and provided the correct path .');
Return (false );
}
// The original function creates a new image and resamples the source to it using the same height and width here.
// I imagine this was to add future resizing functionality but as it is it does nothing but waste resources
$ Image = imagecreatefromjpeg ($ target );
// Don't really know/care what this is. If you're interested SEE http://us2.php.net/imageconvolution
// Try tweaking these three vars for different effects, but there is a sharpening function in the PHP Docs (abve links) and it's not a trivial operation
$ Spnmatrix = array (Array (-1,-1,-1 ,),
Array (-1, 16,-1 ,),
Array (-1,-1,-1 ));
$ Divisor = 8;
$ Offset = 0;
Imageconvolution ($ image, $ spnmatrix, $ divisor, $ offset );
// I like to send headers as late as possible to avoid already sent errors and duplicate header content
Header ('content-type: image/JPEG ');
Imagejpeg ($ image, null, 100 );
Imagedestroy ($ image );
}


// Example call
$ S_image = (isset ($ _ Get ['image'])? $ _ Get ['image']: NULL;

If (preg_match ('/\. (JPG | JPEG) $/I', $ s_image ))
{
Gdmakedomainlooklikecrap ($ s_image );
}
Else
{
Gdthrowerror ('Please specify a jpeg file to sharpen in the form: '. $ _ server ['php _ Self']. '? Image%filename.jpg ');
}
?>

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.