


PHP generates verification code, php generates verification code_PHP tutorial
Jul 12, 2016 am 08:50 AMPHP 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> 設(shè)置頁(yè)面的編碼風(fēng)格</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>創(chuàng)建畫(huà)布并設(shè)置大小 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> 設(shè)置頁(yè)面的編碼風(fēng)格</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>創(chuàng)建畫(huà)布并設(shè)置大小 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>//添加干擾線,并循環(huán)3次,背景顏色隨機(jī)</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>添加干擾點(diǎn),并循環(huán)25次,背景顏色隨機(jī)</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 />這里意思就是 畫(huà)布$img 中從坐標(biāo) <code class="parameter">x1</code>,<code class="parameter">y1</code> 到 <code class="parameter">x2</code>,<code class="parameter">y2</code>隨機(jī)<br /></span>
② imagesetpixel();
imagesetpixel— 畫(huà)一個(gè)單一像素
<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
坐標(biāo)(圖像左上角為 0,0)上畫(huà)一個(gè)點(diǎn)。
<span><em>imagesetpixel($img, mt_rand(0,150), mt_rand(0,60), $dotcolor);<br /></em>具體含義同上<br /><br /></span>
效果圖:
3.0 添加驗(yàn)證字母數(shù)字
<span><span> 1</span> <?<span>php </span><span> 2</span> <span>header</span>("Content-Type:text/html;Charset=UTF-8");<span>//</span><span> 設(shè)置頁(yè)面的編碼風(fēng)格</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>創(chuàng)建畫(huà)布并設(shè)置大小 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>//添加干擾線,并循環(huán)3次,背景顏色隨機(jī)</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>添加干擾點(diǎn),并循環(huán)25次,背景顏色隨機(jī)</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>添加需要驗(yàn)證的字母或者數(shù)字</span> <span>25</span> <span>$rand_str</span> = "qwertyuiopasdfghjklzxcvbnm1234567890";<span>//</span><span>需要使用到驗(yàn)證的一些字母和數(shù)字</span> <span>26</span> <span>$str_arr</span> = <span>array</span>(); <span>//</span><span>命名一個(gè)數(shù)組</span> <span>27</span> <span>for</span>(<span>$i</span> = 0;<span>$i</span><4;<span>$i</span>++){ <span>//</span><span>循環(huán)4次,就是有四個(gè)隨機(jī)的字母或者數(shù)字 </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>臨時(shí)交換</span> <span>30</span> <span>} </span><span>31</span> <span>32</span> <span>$x_start</span>=150/4;<span>//</span><span>單個(gè)字符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>遍歷后單個(gè)字符沿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>
函數(shù):
imagettftext();
imagettftext — 用 TrueType 字體向圖像寫(xiě)入文本
<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-----------畫(huà)布
25-----------字體的尺寸。
mt_rand(-15,15)----------角度制表示的角度,0 度為從左向右讀的文本。更高數(shù)值表示逆時(shí)針旋轉(zhuǎn)。例如 90 度表示從下向上讀的文本。(就是字體角度的問(wèn)題,)
$x_start----------通俗易懂的講就是字符的X軸位置
50/2----------字符的高度
$fontcolor----------字符顏色
"C:/Windows/Fonts/Verdana.TTF"----------字符的字體樣式路徑
$key-----------遍歷出后的字符
?
效果:
看起來(lái)還是挺可愛(ài)的。
?

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

Reasons and solutions for the header function jump failure: 1. There is output before the header, and all pre-outputs need to be checked and removed or ob_start() buffer is used; 2. The failure to add exit causes subsequent code interference, and exit or die should be added immediately after the jump; 3. The path error should be used to ensure correctness by using absolute paths or dynamic splicing; 4. Server configuration or cache interference can be tried to clear the cache or replace the environment test.

The method of using preprocessing statements to obtain database query results in PHP varies from extension. 1. When using mysqli, you can obtain the associative array through get_result() and fetch_assoc(), which is suitable for modern environments; 2. You can also use bind_result() to bind variables, which is suitable for situations where there are few fields and fixed structures, and it is good compatibility but there are many fields when there are many fields; 3. When using PDO, you can obtain the associative array through fetch (PDO::FETCH_ASSOC), or use fetchAll() to obtain all data at once, so the interface is unified and the error handling is clearer; in addition, you need to pay attention to parameter type matching, execution of execute(), timely release of resources and enable error reports.

In PHP, you can use a variety of methods to determine whether a string starts with a specific string: 1. Use strncmp() to compare the first n characters. If 0 is returned, the beginning matches and is not case sensitive; 2. Use strpos() to check whether the substring position is 0, which is case sensitive. Stripos() can be used instead to achieve case insensitive; 3. You can encapsulate the startsWith() or str_starts_with() function to improve reusability; in addition, it is necessary to note that empty strings return true by default, encoding compatibility and performance differences, strncmp() is usually more efficient.

There are three key ways to avoid the "undefinedindex" error: First, use isset() to check whether the array key exists and ensure that the value is not null, which is suitable for most common scenarios; second, use array_key_exists() to only determine whether the key exists, which is suitable for situations where the key does not exist and the value is null; finally, use the empty merge operator?? (PHP7) to concisely set the default value, which is recommended for modern PHP projects, and pay attention to the spelling of form field names, use extract() carefully, and check the array is not empty before traversing to further avoid risks.

When using PHP preprocessing statements to execute queries with IN clauses, 1. Dynamically generate placeholders according to the length of the array; 2. When using PDO, you can directly pass in the array, and use array_values to ensure continuous indexes; 3. When using mysqli, you need to construct type strings and bind parameters, pay attention to the way of expanding the array and version compatibility; 4. Avoid splicing SQL, processing empty arrays, and ensuring data types match. The specific method is: first use implode and array_fill to generate placeholders, and then bind parameters according to the extended characteristics to safely execute IN queries.
