Verification code, php verification code _ PHP Tutorial

Source: Internet
Author: User
Verification code, php verification code. Verification code class, php verification code class familiar with related image processing functions, simple verification code class 1codeclass2classValidateCode {3 private $ charsetabcdefghkmnprstuvwxyzABCDEFGHKMNP verification code class, php verification code class
Familiar with image processing functions and simple verification code

1 //code class 2 class ValidateCode { 3 private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //random effects 4 private $code; //code 5 private $codelen = 4 ; //code length 6 private $width = 130; //width 7 private $heigth = 50; //height 8 private $img; //image handle 9 private $font; //font file10 private $fontsize = 20; //font size11 private $fontcolor; //font color12 13 //the construct initialization14 public function __construct(){15 $this->font = ROOT_PATH.'/font/elephant.ttf';16 }17 18 //create random code from $charset19 private function createCode(){20 $_len = strlen($this->charset);21 for($i=1;$i<=$this->codelen;$i++){22 $this->code .= $this->charset[mt_rand(0,$_len)];23 }24 }25 26 //create background27 private function createBg(){28 $this->img = imagecreatetruecolor($this->width, $this->heigth);29 $_color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));30 imagefilledrectangle($this->img,0,0,$this->width,$this->heigth,$_color);31 }32 33 //create font34 private function createFont(){35 $_x = $this->width / $this->codelen;36 for($i=0;$i<$this->codelen;$i++){37 $this->fontcolor = imagecolorallocate($this->img, mt_rand(0,156), mt_rand(0,156), mt_rand(0,156));38 imagettftext($this->img, $this->fontsize, mt_rand(-30,30), $_x*$i+mt_rand(1,5), $this->heigth/1.4, $this->fontcolor, $this->font, $this->code[$i]);39 }40 }41 42 //create line,snowflake43 private function createLine(){44 for($i=0;$i<6;$i++){45 $_color = imagecolorallocate($this->img, mt_rand(0,156), mt_rand(0,156), mt_rand(0,156));46 imageline($this->img, mt_rand(0,$this->width), mt_rand(0, $this->heigth),mt_rand(0,$this->width), mt_rand(0, $this->heigth), $_color);47 }48 for($i=0;$i<100;$i++){49 $_color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));50 imagestring($this->img, mt_rand(1, 5), mt_rand(0,$this->width), mt_rand(0,$this->heigth), '*', $_color);51 }52 }53 54 //export image55 private function outPut(){56 header('Content-type:image/png');57 imagepng($this->img);58 imagedestroy($this->img);59 }60 61 //display62 public function doimg(){63 $this->createBg();64 $this->createCode();65 $this->createLine();66 $this->createFont();67 $this->outPut();68 }69 70 //get code71 public function getCode(){72 return strtolower($this->code);73 }74 75 }76

NLP is familiar with related image processing functions. simple verification code class 1 // code class 2 class ValidateCode {3 private $ charset = 'abcdefghkmnprstuvwxyzabcdefghkmnp...

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.