PHP Captcha Generator code example

Source: Internet
Author: User
Tags php code generator
Verification code Generator is a simple, easy-to-use, functional Verification code generation tool, users can use the tool to easily generate verification code.

Captcha Generator Feature Description:
1, Verification code length: the number of characters generated verification code, the default is 4;
2, background point density: that is, in the background to generate n points, the default is 50;
3, the character rotation angle: The character vertical is 0 degrees, namely the normal placement is 0 degrees, rotates left to right, the default is 55;
4, Verification: that is, verify that the characters are correct;
5. If the path is empty, no picture information is generated. If you select a path, you must select a picture format;
6. Can't see Clearly, change one: When the path and picture format is selected, click once to generate a picture;
7. Generated picture information: is to verify that the content is the file name.

This article is mainly for you to introduce the PHP code generator related codes, with a certain reference value, interested in small partners can refer to

This code runs on the WinDOS server platform

<?php$ic = new Idcode (5,60,30), $iC->createpng (), class idcode{Private $words = Array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' G ' , ' h ', ' I ', ' j ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' h ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 '  );  Private $fonts;  private $count;//The number of authentication code characters private $height;  Private $width; Private $path = '..  \myfolder\fonts ';  Private $keys;    Constructor public function construct ($count, $width, $height) {$this->count = $count;    $this->getfonts ();    $this->height = $height;  $this->width = $width;    } Private Function Getfonts () {$dir = Dir ($this->path); while (false!== ($file = $dir->read ())) {if ($file! = '. ' && $file! = ')        {$this->fonts[count ($this->fonts)] = basename ($file);  }} $dir->close (); } Private Function Createkeys () {for ($i = 0; $i < $this->count; $i + +){$this->keys[$i] [' char '] = $this->words[rand (0,count ($this->words)-1)]; Use the font path to identify $this->keys[$i [' filename '] = $this->path. '    \ \ '. $this->fonts[rand (0,count ($this->fonts)-1)];    }} Public Function Createpng () {$this->createkeys (); Create canvas and color blocks $BG = Imagecreatetruecolor ($this->width + 10*2, $this->height + 3*2);//left 10px blank on both sides, up and down 3px $grey = ima    Gecolorallocate ($BG, 155,155,155);    $blue = Imagecolorallocate ($BG, 0x00,0x00,0xff);    Fill Background Imagefill ($BG, 0,0, $grey);    Add character $pwidth = $this->width/$this->count;    $x; $y;      for ($i = 0; $i < $this->count; $i + +) {$rotation = rand ( -40,40);//deflection angle ±40° $fontsize = 33;      $width _txt;      $height _txt;        do{$fontsize--;        $bbox = Imagettfbbox ($fontsize, $rotation, $this->keys[$i [' filename '], $this->keys[$i] [' char ']);      $width _txt = $bbox [2]-$bbox [0];//x 0 2 4 6,y1 3 5 7; lower left, lower right, upper right, upper left $height _txt = $bbox [7]-$bbox [1]; }while ($fOntsize > 8 && ($height _txt > $this->height | | $width _txt > $pwidth));      $fontcolor = Imagecolorallocate ($BG, Rand (0,255), Rand (0,255), Rand (0,255));      $x = 8 + $pwidth * $i + $pwidth/2-$width _txt/2;//x coordinates basic position $y = $this-&GT;HEIGHT/2-$height _TXT/2; Imagettftext ($BG, $fontsize, $rotation, $x, $y, $fontcolor, $this->keys[$i] [' filename '], $this->keys[$i] [' char '    ]);    }//Draw interference lines//Add interference line Imageline ($BG, 0,15,40,10, $blue) as appropriate;    Image Output header file header (' Content-type:image/png ');    Output PNG image imagepng ($BG);  Clear Cache Resource Imagedestroy ($BG);    The Public Function Checkkeys ($input) {if (count ($input)! = $this->count) {return ' ERROR: incorrect length. '; }else{for ($i =0; $i < $this->count; $i +) {//0 o i l 1 calibration, determine if manual calibration is required based on the selected font ($input [$i]! = $th        is->keys[$i] [' char ']) {return ' SUCCESS. ';        }else{return ' ERROR: Please enter the correct verification code. '; }}}}}?>

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.