Home > Article > Backend Development > Briefly describe the process of designing verification code in PHP
Briefly describe the process of designing verification code in PHP
php uses GD library to generate verification code
1. Drawing steps:
1. Create a canvas, assign color, and use the following two functions (can be found in the GD library function of the PHP manual):
imagecreatetruecolor()
imagecolorallocate()
2. Painting process:
imagefill()
imagettftext()
imagesetpixel()
imageline ()
3. Output image:
header(“Content-Type:image/png”);
imagepng();
imagejpeg( );
4. Destroy the image (release memory)
imagedestroy();
2. Specific steps of drawing
Generate verification code string
private function getCode(){ $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $t=''; for($i=0;$i<$this->m;$i++){ $t.=$str[rand(0,$this->type['letter'])]; } return $t; }
Draw the verification code
function drawCode(){ $code=$this->getCode();//获取验证码字符串 imagefill($this->im, 0, 0, $this->bg); for ($i=0; $i <200 ; $i++) { imagesetpixel($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,255)); } imageline($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,$this->width), rand(0,$this->height), rand(0,255)); $c=imagecolorallocate($this->im, rand(0,200), rand(0,200), rand(0,200)); for($i=0;$i<$this->m;$i++){ imagettftext($this->im, 18, rand(-40,40), 8+(18*$i), 24, $c, "georgia.ttf",$code[$i] ); } }
Output image
function printImage(){ $this->drawCode(); header("Content-Type:image/jpeg"); imagepng($this->im); $this->destroy(); } function destroy(){ imagedestroy($this->im); }
3. Code demonstration
9,'letter' => 61 ); function __construct($width,$height,$m) { $this->width=$width; $this->height=$height; $this->m=$m; $this->im=imagecreatetruecolor($this->width, $this->height); $this->bg=imagecolorallocate($this->im, 220, 220, 220); } private function getCode(){ $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $t=''; for($i=0;$i<$this->m;$i++){ $t.=$str[rand(0,$this->type['letter'])]; } return $t; } function drawCode(){ $code=$this->getCode();//获取验证码字符串 imagefill($this->im, 0, 0, $this->bg); //加干扰点 for ($i=0; $i <200 ; $i++) { imagesetpixel($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,255)); } //加干扰线 imageline($this->im, rand(0,$this->width), rand(0,$this->height), rand(0,$this->width), rand(0,$this->height), rand(0,255)); $c=imagecolorallocate($this->im, rand(0,200), rand(0,200), rand(0,200)); for($i=0;$i<$this->m;$i++){ imagettftext($this->im, 18, rand(-40,40), 8+(18*$i), 24, $c, "georgia.ttf",$code[$i] ); } } function printImage(){ $this->drawCode(); header("Content-Type:image/jpeg"); imagepng($this->im); $this->destroy(); } function destroy(){ imagedestroy($this->im); } } function unit_test(){ $obj=new Image(20*4,30,4); $obj->printImage(); } unit_test(); ?>
For more PHP related knowledge, please visit PHP Chinese website !
The above is the detailed content of Briefly describe the process of designing verification code in PHP. For more information, please follow other related articles on the PHP Chinese website!