PHP programming fastest: php image verification code and thumbnails

Source: Internet
Author: User
Tags imagejpeg
This is an example of outputting four verification codes. for Chinese characters, you need the font file and the imagettftext function. you can search for them online. Instance 22: Core Image Verification code
The code is as follows:
// Header ("content-type: image/png ");
$ Num = '000000 ';
$ Imagewidth = 60;
$ Imageheight = 18;

$ Numimage = imagecreate ($ imagewidth, $ imageheight );
Imagecolorallocate ($ numimage, 240,240,240 );
For ($ I = 0; $ I $ X = mt_rand (1, 8) + $ imagewidth * $ I/4;
$ Y = mt_rand (1, $ imageheight/4 );
$ Color = imagecolorallocate ($ numimage, mt_rand (0,150), mt_rand (0,150), mt_rand (0,150 ));
Imagestring ($ numimage, 5, $ x, $ y, $ num [$ I], $ color );
}

For ($ I = 0; I I <200; $ I ++ ){
$ Randcolor = imagecolorallocate ($ numimage, rand (200,255), rand (200,255), rand (200,255 ));
Imagesetpixel ($ numimage, rand () % 70, rand () % 20, $ randcolor );
}
Imagepng ($ numimage );
Imagedestroy ($ numimage );
?>

This is an example of outputting four verification codes. for Chinese characters, you need the font file and the imagettftext function. you can search for them online. If you want to generate a random number, there is the mt_rand function. you also need to use session to save the random number. if you want to convert it to UTF-8, you need the iconv function.

Instance 23 thumbnail
The code is as follows:
Class SimpleImage {
Var $ image;
Var $ image_type;
Function load ($ filename ){
$ Image_info = getimagesize ($ filename );
$ This-> image_type = $ image_info [2];
If ($ this-> image_type = IMAGETYPE_JPEG ){
$ This-> image = imagecreatefromjpeg ($ filename );
} Elseif ($ this-> image_type = IMAGETYPE_GIF ){
$ This-> image = imagecreatefromgif ($ filename );
} Elseif ($ this-> image_type = IMAGETYPE_PNG ){
$ This-> image = imagecreatefrompng ($ filename );
}
}
Function save ($ filename, $ image_type = IMAGETYPE_JPEG, $ compression = 75, $ permissions = null ){
If ($ image_type = IMAGETYPE_JPEG ){
Imagejpeg ($ this-> image, $ filename, $ compression );
} Elseif ($ image_type = IMAGETYPE_GIF ){
Imagegif ($ this-> image, $ filename );
} Elseif ($ image_type = IMAGETYPE_PNG ){
Imagepng ($ this-> image, $ filename );
}
If ($ permissions! = Null ){
Chmod ($ filename, $ permissions );
}
}
Function output ($ image_type = IMAGETYPE_JPEG ){
If ($ image_type = IMAGETYPE_JPEG ){
Imagejpeg ($ this-> image );
} Elseif ($ image_type = IMAGETYPE_GIF ){
Imagegif ($ this-> image );
} Elseif ($ image_type = IMAGETYPE_PNG ){
Imagepng ($ this-> image );
}
}
Function getWidth (){
Return imagesx ($ this-> image );
}
Function getHeight (){
Return imagesy ($ this-> image );
}
Function resizeToHeight ($ height ){
$ Ratio = $ height/$ this-> getHeight ();
$ Width = $ this-> getWidth () * $ ratio;
$ This-> resize ($ width, $ height );
}
Function resizeToWidth ($ width ){
$ Ratio = $ width/$ this-> getWidth ();
$ Height = $ this-> getheight () * $ ratio;
$ This-> resize ($ width, $ height );
}
Function scale ($ scale ){
$ Width = $ this-> getWidth () * $ scale/100;
$ Height = $ this-> getheight () * $ scale/100;
$ This-> resize ($ width, $ height );
}
Function resize ($ width, $ height ){
$ New_image = imagecreatetruecolor ($ width, $ height );
Imagecopyresampled ($ new_image, $ this-> image, 0, 0, 0, 0, $ width, $ height, $ this-> getWidth (), $ this-> getHeight ());
$ This-> image = $ new_image;
}
}

$ Newfile = UPLOAD_DIR. "/icons/". md5 ($ _ SESSION ['user']-> email). ". jpg"; // upload the directory to which the file is saved
$ Image = new SimpleImage ();
$ Image-> load ($ _ FILES ['icons'] ['tmp _ name']); // Upload temporary file name
$ Image-> resizeToWidth (80); set the width
$ Image-> save ($ newfile );
?>

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.