


PHP generates verification code, php generates verification code_PHP tutorial
PHP generates verification code, PHP generates verification code
You will know it after reading it, you won’t hit me, don’t say much, let’s get started ( People don’t talk much)
1.0 First, look at the code
<span><span> 1</span> <?<span>php </span><span> 2</span> <span>header</span>("Content-Type:text/html;Charset=UTF-8");<span>//</span><span> 设置页面的编码风格</span> <span> 3</span> <span>header</span>("Content-Type:image/jpeg");<span>//</span><span> 通知浏览器输出的是jpeg格式的图像</span> <span> 4</span> <span> 5</span> <span>$img</span> = imagecreatetruecolor(150,50);<span>//</span><span>创建画布并设置大小 x轴150 y轴50</span> <span> 6</span> <span> 7</span> <span>$bgcolor</span> = imagecolorallocate(<span>$img</span>, <span>mt_rand</span>(0,255), <span>mt_rand</span>(0,255), <span>mt_rand</span>(0,255));<span>//</span><span>分配背景颜色</span> <span> 8</span> imagefill(<span>$img</span>, 0, 0, <span>$bgcolor</span>); <span>//</span><span>//把背景填充到图像</span> <span> 9</span> imagejpeg(<span>$img</span>); <span>//</span><span> 输出图像</span> <span>10</span> imagedestroy(<span>$img</span>); <span>//</span><span> 销毁图像</span> <span>11</span> ?></span>
OK, now combine the above code to analyze the several functions used above:
① imagecreatetruecolor();
imagecreatetruecolor — Create a new true color image (It feels so long. In fact, it’s easy to remember image/create/true/color if you look carefully. What is True color image? Look below)
<span><span>1</span> <span>resource</span> imagecreatetruecolor ( int <span>$width</span> , int <span>$height</span> )</span>
Both functions imagecreatetruecolor() and imagecreate() can create canvases
<span><span>1</span> <span>resource</span> imagecreate ( int <span>$x_size</span> , int <span>$y_size</span> )</span>
imagecreatetruecolor() creates a black image of size x and y (the default is black [even if it is called a true color image] ), If you want to change the background color, you need to use the fill color function imagefill($img,0,0,$color);
imagecreate Create a new blank image resource and use imagecolorAllocate() to add a background color
The above two functions are just two methods of the same function
② imagecolorallocate();
imagecolorallocate — Assign a color to an image
<span><span>1</span> int imagecolorallocate ( <span>resource</span> <span>$image</span> , int <span>$red</span> , int <span>$green</span> , int <span>$blue</span> )</span>
The colors are red, green and blue respectively. These parameters are integers from 0 to 255 or hexadecimal 0x00 to 0xFF.
③ mt_rand();
mt_rand — Generate better random numbers
<span><span>1</span> int <span>mt_rand</span> ( int <span>$min</span> , int <span>$max</span> )</span>
$min
Optional, the minimum value returned (default: 0) $max
Optional, the maximum value returned (default: mt_getrandmax())
- Here it is used to randomly generate the background color, with any value from 0-255. Therefore, the canvas background color is different even if the page is refreshed.
- Rendering:
2.0 Start making interference lines and interference points inside. Prevent verification images from being recognized in seconds
<span><span> 1</span> <?<span>php </span><span> 2</span> <span>header</span>("Content-Type:text/html;Charset=UTF-8");<span>//</span><span> 设置页面的编码风格</span> <span> 3</span> <span>header</span>("Content-Type:image/jpeg");<span>//</span><span> 通知浏览器输出的是jpeg格式的图像</span> <span> 4</span> <span> 5</span> <span>$img</span> = imagecreatetruecolor(150,50);<span>//</span><span>创建画布并设置大小 x轴150 y轴50</span> <span> 6</span> <span> 7</span> <span>$bgcolor</span> = imagecolorallocate(<span>$img</span>, <span>mt_rand</span>(0,255), <span>mt_rand</span>(0,255), <span>mt_rand</span>(0,255));<span>//</span><span>分配背景颜色 </span><span> 8</span> <span> 9</span> <span>//添加干扰线,并循环3次,背景颜色随机</span> <span>10</span> <span>for</span>(<span>$i</span>=0;<span>$i</span><3;<span>$i</span>++<span>){ </span><span>11</span> <span>12</span> <span>$linecolor</span> = imagecolorallocate(<span>$img</span>,<span>mt_rand</span>(0,255),<span>mt_rand</span>(0,255),<span>mt_rand</span>(0,255<span>)); </span><span>13</span> imageline(<span>$img</span>, <span>mt_rand</span>(0,150), <span>mt_rand</span>(0,50), <span>mt_rand</span>(0,150), <span>mt_rand</span>(0,50), <span>$linecolor</span><span>); </span><span>14</span> <span>15</span> <span>} </span><span>16</span> <span>//</span><span>添加干扰点,并循环25次,背景颜色随机</span> <span>17</span> <span>for</span>(<span>$i</span>=0;<span>$i</span><25;<span>$i</span>++<span>){ </span><span>18</span> <span>19</span> <span>$dotcolor</span> = imagecolorallocate(<span>$img</span>, <span>mt_rand</span>(0,255), <span>mt_rand</span>(0,255), <span>mt_rand</span>(0,255<span>)); </span><span>20</span> imagesetpixel(<span>$img</span>, <span>mt_rand</span>(0,150), <span>mt_rand</span>(0,60), <span>$dotcolor</span><span>); </span><span>21</span> <span>22</span> <span>} </span><span>23</span> <span>24</span> imagefill(<span>$img</span>, 0, 0, <span>$bgcolor</span>); <span>//</span><span>//把背景填充到图像</span> <span>25</span> imagejpeg(<span>$img</span>); <span>//</span><span> 输出图像</span> <span>26</span> imagedestroy(<span>$img</span>); <span>//</span><span> 销毁图像</span> <span>27</span> ?></span>
Function analysis:
① imageline();
imageline — Draw a line segment
<span><span>1</span> bool imageline ( <span>resource</span> <span>$image</span> , int <span>$x1</span> , int <span>$y1</span> , int <span>$x2</span> , int <span>$y2</span> , int <span>$color</span> )</span>
imageline() uses the color
color to draw in the image image
from the coordinates x1
, y1
to x2
, y2
(the upper left corner of the image is 0, 0) A line segment.
<span><em>imageline($img, mt_rand(0,150), mt_rand(0,50), mt_rand(0,150), mt_rand(0,50), $linecolor);<br /></em><br />这里意思就是 画布$img 中从坐标 <code class="parameter">x1</code>,<code class="parameter">y1</code> 到 <code class="parameter">x2</code>,<code class="parameter">y2</code>随机<br /></span>
② imagesetpixel();
imagesetpixel— 画一个单一像素
<span><span>1</span> bool imagesetpixel ( <span>resource</span> <span>$image</span> , int <span>$x</span> , int <span>$y</span> , int <span>$color</span> )</span>
imagesetpixel() 在 image
图像中用 color
颜色在 x
,y
坐标(图像左上角为 0,0)上画一个点。
<span><em>imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);<br /></em>具体含义同上<br /><br /></span>
效果图:
3.0 添加验证字母数字
<span><span> 1</span> <?<span>php </span><span> 2</span> <span>header</span>("Content-Type:text/html;Charset=UTF-8");<span>//</span><span> 设置页面的编码风格</span> <span> 3</span> <span>header</span>("Content-Type:image/jpeg");<span>//</span><span> 通知浏览器输出的是jpeg格式的图像</span> <span> 4</span> <span> 5</span> <span>$img</span> = imagecreatetruecolor(150,50);<span>//</span><span>创建画布并设置大小 x轴150 y轴50</span> <span> 6</span> <span> 7</span> <span>$bgcolor</span> = imagecolorallocate(<span>$img</span>, <span>mt_rand</span>(0,255), <span>mt_rand</span>(0,255), <span>mt_rand</span>(0,255));<span>//</span><span>分配背景颜色 </span><span> 8</span> <span> 9</span> <span>//添加干扰线,并循环3次,背景颜色随机</span> <span>10</span> <span>for</span>(<span>$i</span>=0;<span>$i</span><3;<span>$i</span>++<span>){ </span><span>11</span> <span>12</span> <span>$linecolor</span> = imagecolorallocate(<span>$img</span>,<span>mt_rand</span>(0,255),<span>mt_rand</span>(0,255),<span>mt_rand</span>(0,255<span>)); </span><span>13</span> imageline(<span>$img</span>, <span>mt_rand</span>(0,150), <span>mt_rand</span>(0,50), <span>mt_rand</span>(0,150), <span>mt_rand</span>(0,50), <span>$linecolor</span><span>); </span><span>14</span> <span>15</span> <span>} </span><span>16</span> <span>//</span><span>添加干扰点,并循环25次,背景颜色随机</span> <span>17</span> <span>for</span>(<span>$i</span>=0;<span>$i</span><25;<span>$i</span>++<span>){ </span><span>18</span> <span>19</span> <span>$dotcolor</span> = imagecolorallocate(<span>$img</span>, <span>mt_rand</span>(0,255), <span>mt_rand</span>(0,255), <span>mt_rand</span>(0,255<span>)); </span><span>20</span> imagesetpixel(<span>$img</span>, <span>mt_rand</span>(0,150), <span>mt_rand</span>(0,60), <span>$dotcolor</span><span>); </span><span>21</span> <span>22</span> <span>} </span><span>23</span> <span>24</span> <span>//</span><span>添加需要验证的字母或者数字</span> <span>25</span> <span>$rand_str</span> = "qwertyuiopasdfghjklzxcvbnm1234567890";<span>//</span><span>需要使用到验证的一些字母和数字</span> <span>26</span> <span>$str_arr</span> = <span>array</span>(); <span>//</span><span>命名一个数组</span> <span>27</span> <span>for</span>(<span>$i</span> = 0;<span>$i</span><4;<span>$i</span>++){ <span>//</span><span>循环4次,就是有四个随机的字母或者数字 </span> <span>28</span> <span>$pos</span> = <span>mt_rand</span>(0,<span>strlen</span>(<span>$rand_str</span>)-1<span>); </span><span>29</span> <span>$str_arr</span>[] = <span>$rand_str</span>[<span>$pos</span>];<span>//</span><span>临时交换</span> <span>30</span> <span>} </span><span>31</span> <span>32</span> <span>$x_start</span>=150/4;<span>//</span><span>单个字符X轴位置</span> <span>33</span> <span>34</span> <span>foreach</span> (<span>$str_arr</span> <span>as</span> <span>$key</span><span>) { </span><span>35</span> <span>$fontcolor</span> = imagecolorallocate(<span>$img</span>, <span>mt_rand</span>(0,255), <span>mt_rand</span>(0,255), <span>mt_rand</span>(0,255<span>)); </span><span>36</span> imagettftext(<span>$img</span>, 25, <span>mt_rand</span>(-15,15), <span>$x_start</span>, 50/2, <span>$fontcolor</span>, "C:/Windows/Fonts/Verdana.TTF", <span>$key</span><span>); </span><span>37</span> <span>$x_start</span> +=20;<span>//</span><span>遍历后单个字符沿X轴 +20</span> <span>38</span> <span>} </span><span>39</span> <span>40</span> imagefill(<span>$img</span>, 0, 0, <span>$bgcolor</span>); <span>//</span><span>//把背景填充到图像</span> <span>41</span> imagejpeg(<span>$img</span>); <span>//</span><span> 输出图像</span> <span>42</span> imagedestroy(<span>$img</span>); <span>//</span><span> 销毁图像</span> <span>43</span> ?></span>
函数:
imagettftext();
imagettftext — 用 TrueType 字体向图像写入文本
<span><span>1</span> <span>array</span> imagettftext ( <span>resource</span> <span>$image</span> , <span>float</span> <span>$size</span> , <span>float</span> <span>$angle</span> , int <span>$x</span> , int <span>$y</span> , int <span>$color</span> , <span>string</span> <span>$fontfile</span> , <span>string</span> <span>$text</span> )</span>
分析下面的代码:
<span>imagettftext($img, 25, mt_rand(-15,15), $x_start, 50/2, $fontcolor, "C:/Windows/Fonts/Verdana.TTF", $key);</span>
$img-----------画布
25-----------字体的尺寸。
mt_rand(-15,15)----------角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转。例如 90 度表示从下向上读的文本。(就是字体角度的问题,)
$x_start----------通俗易懂的讲就是字符的X轴位置
50/2----------字符的高度
$fontcolor----------字符颜色
"C:/Windows/Fonts/Verdana.TTF"----------字符的字体样式路径
$key-----------遍历出后的字符
效果:
看起来还是挺可爱的。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use