How to draw graphical verification code through PHP?

WBOY
Release: 2023-03-13 12:42:01
Original
2057 people have browsed it

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!

How to draw graphical verification code through PHP?

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

Among them, the syntax format of imagechar(), imagecharup(), imagestring() and imagestringup() functions is the same. Their syntax format is as follows:

imagechar(resource $image, int $font, int $x, int $y, string $c, int $color)
Copy after login

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.


The example is as follows:

<?php
    $a = &#39;young and beautiful&#39;;
    $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(&#39;Content-type:image/jpeg&#39;);
    imagejpeg($img);
    imagedestroy($img);
?>
Copy after login

Output result:


How to draw graphical verification code through PHP?

There is also a function called imagettftext() function, It can also draw text, but the difference is that it can use TrueType fonts, which are extensions in Windows systems. Ttf format font, its syntax format is as follows:


imagettftext(resource $image, float $size, float $angle, int $x, int $y, int $color, string $fontfile, string $text)
Copy after login

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.


The example is as follows:

<?php
    $str1 = &#39;young and beautiful&#39;;
    $font = &#39;C:\Windows\Fonts\simhei.ttf&#39;;
    $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(&#39;Content-type:image/jpeg&#39;);
    imagejpeg($img);
    imagedestroy($img);
?>
Copy after login

Output result:


How to draw graphical verification code through PHP?##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:


How to draw graphical verification code through PHP?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 = &#39;jpeg&#39;) {
 $img = imagecreate($width, $height);
 $string = &#39;&#39;;
 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(&#39;%c&#39;, $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 = &#39;image&#39; . $type;
 $header = &#39;Content-type:image/&#39; . $type;
 if (function_exists($func)) {
 header($header);
 $func($img);
 } else {
 echo &#39;图片类型不支持&#39;;
 }
 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));
}
?>
Copy after login

Output result:


How to draw graphical verification code through PHP?How to draw graphical verification code through PHP?How to draw graphical verification code through PHP?Recommended study:《

PHP video tutorial

The 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!