Home >Backend Development >PHP Problem >How to draw graphical verification code through PHP?
In the previous article, I brought you "How to draw polygons, arcs and ellipses in PHP? (Detailed graphic explanation) ", which introduces in detail how to draw graphics in PHP. In this article, we will take a look at how we generate graphic verification codes in PHP. I hope everyone has to help!
Through our previous article on image drawing in PHP, we can use the knowledge to make an extension, which is the graphical verification we often use on daily login pages. code, how is the graphic verification code formed? If you want to learn how to make the graphic verification code, first let's take a look at how to draw text in PHP.
Drawing text
The functions we commonly use to draw text are:
imagechar()
: Draw a character horizontally
imagecharup()
: Draw a character vertically Characters
#imagestring() : Draw a line of string horizontally
imagestringup() : Draw a line of string vertically
imagettftext() : Use TrueType font to image Write text in
imagechar(resource $image, int $font, int $x, int $y, string $c, int $color)Among them, $image represents the created canvas. The next operations are performed on the canvas. $font represents the color of the drawn font. $x and $y represent the coordinates ($x, $y). Draw text at the position, $c represents the character (or string) to be drawn, and $color represents the color of the drawn font.
<?php $a = 'young and beautiful'; $img = imagecreate(300, 200); imagecolorallocate($img, 255, 255, 255); $green = imagecolorallocate($img, 0, 255, 0); imagestring($img, 5, 5, 50, $a, $green); imagestringup($img, 2, 200, 180, $a, $green); imagechar($img, 3, 80, 100, $a, $green); imagecharup($img, 4, 100, 100, $a, $green); header('Content-type:image/jpeg'); imagejpeg($img); imagedestroy($img); ?>Output result:
imagettftext(resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)Among them, $size represents the size of the character, $angle represents the angle, 0 degrees in this angle represents the text read from left to right, press The value increases in the counterclockwise direction; $fontfile represents the path to the TrueType font file to be used; $textb represents the string to be drawn.
<?php $str1 = 'young and beautiful'; $font = 'C:\Windows\Fonts\simhei.ttf'; $img = imagecreate(300, 300); imagecolorallocate($img, 255, 255, 255); $green = imagecolorallocate($img, 0, 255, 0); imagettftext($img, 16, 45, 40, 250, $green, $font, $str1); header('Content-type:image/jpeg'); imagejpeg($img); imagedestroy($img); ?>Output result:
##This is completed through the imagettftext() function Drawing of text.
Draw graphics verification codeAfter the previous learning of drawing text and images, we can then draw graphics using the knowledge we have learned before. Drawing of verification codes, we often encounter graphical verification codes in our login interface, so let's take a look at how to create it.
First, let’s take a look at the final effect:
To achieve such an effect, you need to go through some steps:
The first step is to prepare the canvas we need, which is to create the canvas. Then prepare the string we want to generate, draw the interference pattern on the canvas, then output the image, and finally release the resources.
The example is as follows:
<?php check_code(); function check_code($width = 100, $height = 50, $num = 4, $type = 'jpeg') { $img = imagecreate($width, $height); $string = ''; for ($i = 0; $i < $num; $i++) { $rand = mt_rand(0, 2); switch ($rand) { case 0: $ascii = mt_rand(48, 57); //0-9 break; case 1: $ascii = mt_rand(65, 90); //A-Z break; case 2: $ascii = mt_rand(97, 122); //a-z break; } //chr() $string .= sprintf('%c', $ascii); } //背景颜色 imagefilledrectangle($img, 0, 0, $width, $height, randBg($img)); //画干扰元素 for ($i = 0; $i < 50; $i++) { imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), randPix($img)); } //写字 for ($i = 0; $i < $num; $i++) { $x = floor($width / $num) * $i + 2; $y = mt_rand(0, $height - 15); imagechar($img, 5, $x, $y, $string[$i], randPix($img)); } //imagejpeg $func = 'image' . $type; $header = 'Content-type:image/' . $type; if (function_exists($func)) { header($header); $func($img); } else { echo '图片类型不支持'; } imagedestroy($img); return $string; } //浅色的背景 function randBg($img) { return imagecolorallocate($img, mt_rand(130, 255), mt_rand(130, 255), mt_rand(130, 255)); } //深色的字或者点这些干 扰元素 function randPix($img) { return imagecolorallocate($img, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); } ?>
Recommended study:《
PHP video tutorialThe above is the detailed content of How to draw graphical verification code through PHP?. For more information, please follow other related articles on the PHP Chinese website!