search
Homephp教程php手册PHP生成制作验证码,php生成验证码

PHP生成制作验证码,php生成验证码

看完就会,不会你打我,话不多说、开搞(人狠话不多

1.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> 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>

 

 好,现在结合以上代码,来分析分析以上用到的几个函数:

①  imagecreatetruecolor(); 

imagecreatetruecolor — 新建一个真彩色图像(感觉哇,那么长,其实仔细一看挺好记的 image/create/true/color,什么是真彩色图像?往下看

<span><span>1</span> <span>resource</span> imagecreatetruecolor ( int <span>$width</span> , int <span>$height</span> )</span>

imagecreatetruecolor() 和 imagecreate()两个函数都能创建画布

<span><span>1</span> <span>resource</span> imagecreate ( int <span>$x_size</span> , int <span>$y_size</span> )</span>

 

imagecreatetruecolor()建立的是一幅大小为 x和 y的黑色图像(默认为黑色[即便叫法就是真彩色图像]),如想改变背景颜色则需要用填充颜色函数 imagefill($img,0,0,$color);

imagecreate 新建一个空白图像资源,用imagecolorAllocate()添加背景色

上面两个函数只不过是一个功能的两种方法

 

②  imagecolorallocate();

imagecolorallocate — 为一幅图像分配颜色

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

颜色分别用 红 绿 蓝三色组合,这些参数是 0 到 255 的整数或者十六进制的 0x00 到 0xFF。

 

③  mt_rand();

mt_rand — 生成更好的随机数

<span><span>1</span> int <span>mt_rand</span> ( int <span>$min</span> , int <span>$max</span> )</span>

$min 可选的、返回的最小值(默认:0)  $max 可选的、返回的最大值(默认:mt_getrandmax()) 

这里就是用来让他随机生成背景颜色,0-255随便取值。所以页面没刷新一次画布背景颜色就不一样。
效果图:

 

2.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> 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>

 

 函数分析:

①  imageline();

 imageline — 画一条线段

<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() 用 color 颜色在图像 image 中从坐标 x1y1x2y2(图像左上角为 0, 0)画一条线段。 

<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 颜色在 xy 坐标(图像左上角为 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-----------遍历出后的字符

 

效果:

看起来还是挺可爱的。

 

Statement
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

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

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

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

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

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

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

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

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

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

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

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

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

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

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.