Instance 22: core image Verification Code
Copy codeThe Code is as follows:
<? Php
// Header ("content-type: image/png ");
$ Num = '000000 ';
$ Imagewidth = 60;
$ Imageheight = 18;
$ Numimage = imagecreate ($ imagewidth, $ imageheight );
Imagecolorallocate ($ numimage, 240,240,240 );
For ($ I = 0; $ I <strlen ($ num); $ 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
Copy codeThe Code is as follows:
<? Php
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 );
?>