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 horizontallyimagecharup()
: 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)); } ?>
Output result:
Recommended study:《
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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!
