Very practical php Verification Code class, very practical php Verification Code

Source: Internet
Author: User

Very practical php Verification Code class, very practical php Verification Code

This example shares the php Verification Code class for your reference. The details are as follows:

<? Php/*** @ author Administrator **/class ValidateCode {private $ width; private $ height; private $ codeNum; private $ img_resouce; private $ disturbColorNum; private $ checkCode; function _ construct ($ width = 80, $ height = 20, $ codeNum = 4) {$ this-> width = $ width; $ this-> height = $ height; $ this-> codeNum = $ codeNum; $ this-> checkCode = $ this-> CreateCheckCode (); $ number = floor ($ width * $ height/25 ); if ($ number & gt; 240-$ code Num) {$ this-> disturbColorNum = 240-$ codeNum;} else {$ this-> disturbColorNum = $ number ;}} public function showImage ($ fontpath = '') {// create an image background $ this-> Img_resouce (); // var_dump ($ img_resouce); // set the interference element $ this-> setDistructcolor (); // randomly draw the text $ this-> outputtext ($ fontpath) to the image; // output the image $ this-> outputimage ();} /***** obtain the randomly created Verification Code */public function getCheckCode () {} private function Img_resouce () {// create a true color image $ this-> Img_resouce = imagecreatetruecolor ($ this-> width, $ this-> height); // randomly sets the image background $ backcolor = imagecolorallocate ($ this-> img_resouce, rand (225,255 ), rand (225,255), rand (225,255); // fill color imagefill ($ this-> img_resouce, 0, 0, $ backcolor ); // set the border background $ border = imagecolorallocate ($ this-> img_resouce, 0); // draw a rectangle imagerectangle ($ this-> img_resouce, $ this-> width-1, $ this-> height-1, $ border);} private function setDist Ructcolor () {// painting disturbance point for ($ I = 0; $ I <$ this-> disturbColorNum; $ I ++) {imagesetpixel ($ this-> img_resouce, rand (1, $ this-> width-2), rand (1, $ this-> height-2), rand (0,255 ));} // draw interference lines for ($ j = 0; $ j <3; $ j ++) {$ linecolor = imagecolorallocate ($ this-> img_resouce, rand (0,255 ), rand (0,255), rand (0,255); imagearc ($ this-> img_resouce, rand (0, $ this-> width), rand (0, $ this-> height), rand (10,225), rand (20,150), 55, 44, $ linecolor) ;}} private function CreateCheckCode () {$ code = '23456789abcdefghijkmnpqrstuvwxyzabcdefghijkmnpqrstuvwxyz '; $ string = ''; for ($ I = 0; $ I <$ this-> codeNum; $ I ++) {$ char = $ code {rand (0, strlen ($ code)-1)}; $ string. = $ char;} return $ string;} private function outputtext ($ fontpath = '') {for ($ I = 0; $ I <$ this-> codeNum; $ I ++) {$ fontcolor = imagecolorallocate ($ this-> img_resouce, rand (0,128), Rand (0,128), rand (0,128); if ($ fontpath = '') {$ fontsize = rand (3, 5 ); $ x = floor ($ this-> width/$ this-> codeNum) * $ I + 3; $ y = rand (0, $ this-> height-20 ); imagechar ($ this-> img_resouce, $ fontsize, $ x, $ y, $ this-> checkCode {$ I}, $ fontcolor );} else {$ fontsize = rand (12, 16); $ x = floor ($ this-> width-8)/$ this-> codeNum) * $ I + 8; $ y = rand ($ fontsize, $ this-> height-15); imagettftext ($ this-> img_resouce, $ fontsize, rand (-45, 45), $ x, $ y, $ fontcolor, fontpath, $ this-> checkCode {$ I}) ;}} private function outputimage () {if (imagetypes () & IMG_GIF) {header ("Content-type: image/gif"); imagegif ($ this-> img_resouce);} else if (imagetypes () & IMG_JPEG) {header ("Content-type: image/jpeg"); imagejpeg ($ this-> img_resouce);} else if (imagetypes () & IMG_PNG) {header ("Content-type: image/png"); imagepng ($ this-> img_resouce);} else {ec Ho "types not supported by PHP" ;}} private function _ destruct () {imagedestroy ($ this-> img_resouce) ;}}?>

The above is all the content of this article, hoping to help you learn.

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.