When we want to use , first add a line of reference in php.ini: extension=php_gd2.dll
Restart apache. Make a test page var_dump(gd_info()); the output data shows that the PHP GD library is successfully referenced.
form auth.html
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></span></font></strong><span> </span></span></li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>meta</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>http-equiv</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>'Content-Type'</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>content</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>'text/html; charset=utf-8'</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span>验证码</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>title</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>head</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>h1</SPAN><SPAN class=tag>></span></font></strong><span>请输入验证码</span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>h1</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>form</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>action</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"check_auth.php"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>method</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"post"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"auth"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"text"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span> </span> </li> <li class=""> <span> </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>img</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>src</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"auth.php"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>border</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"0"</FONT></SPAN><SPAN> </SPAN><SPAN class=tag><STRONG><FONT color=#006699>/></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"submit"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>value</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"提交"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>form</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class="alt"> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>body</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></</SPAN><SPAN class=tag-name>html</SPAN><SPAN class=tag>></span></font></strong><span> </span> </li> </ol>
PHP GD library generates verification code auth.php
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN> session_start(); </SPAN><LI class=alt><SPAN> header("Content-type:image/png"); </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> $</SPAN><SPAN class=attribute><FONT color=#ff0000>img_width</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>100</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN> $</SPAN><SPAN class=attribute><FONT color=#ff0000>img_height</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>20</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN> srand(microtime()*100000); </SPAN><LI class=alt><SPAN> for($</SPAN><SPAN class=attribute><FONT color=#ff0000>i</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>0</FONT></SPAN><SPAN>;$i</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>4</SPAN></FONT></STRONG><SPAN>;$i++) </SPAN></SPAN><LI class=""><SPAN> { </SPAN><LI class=alt><SPAN> $</SPAN><SPAN class=attribute><FONT color=#ff0000>new_number.</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>dechex</FONT></SPAN><SPAN>(rand(0,15)); </SPAN></SPAN><LI class=""><SPAN> } </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN> $_SESSION[check_auth]=$new_number; </SPAN><LI class=alt><SPAN> $</SPAN><SPAN class=attribute><FONT color=#ff0000>new_number</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>imageCreate</FONT></SPAN><SPAN>($img_width,$img_height);//创建图象 </SPAN></SPAN><LI class=""><SPAN> ImageColorAllocate($new_number,255,255,255); //设置背景色为白色 </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN> for($</SPAN><SPAN class=attribute><FONT color=#ff0000>i</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>0</FONT></SPAN><SPAN>;$i</SPAN><STRONG><FONT color=#006699><SPAN class=tag><</SPAN><SPAN class=tag-name>strlen</SPAN></FONT></STRONG><SPAN>($_SESSION[check_auth]);$i++) </SPAN></SPAN><LI class=alt><SPAN> { </SPAN><LI class=""><SPAN> $</SPAN><SPAN class=attribute><FONT color=#ff0000>font</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>mt_rand</FONT></SPAN><SPAN>(3,5); </SPAN></SPAN><LI class=alt><SPAN> $</SPAN><SPAN class=attribute><FONT color=#ff0000>x</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>mt_rand</FONT></SPAN><SPAN>(1,8) + $img_width*$i/4; </SPAN></SPAN><LI class=""><SPAN> $</SPAN><SPAN class=attribute><FONT color=#ff0000>y</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>mt_rand</FONT></SPAN><SPAN>(1,$img_height/4); </SPAN></SPAN><LI class=alt><SPAN> $</SPAN><SPAN class=attribute><FONT color=#ff0000>color</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>imageColorAllocate</FONT></SPAN><SPAN>($new_number,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));//设置字符颜色 </SPAN></SPAN><LI class=""><SPAN> imageString($new_number,$font,$x,$y,$_SESSION[check_auth][$i],$color);//输出字符 </SPAN><LI class=alt><SPAN> } </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> ImagePng($new_number); </SPAN><LI class=""><SPAN> ImageDestroy($new_number); </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
PHP GD library submission page check_auth.php
<ol class="dp-xml"><li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN> session_start(); </SPAN><LI class=alt><SPAN> $</SPAN><SPAN class=attribute><FONT color=#ff0000>auth</FONT></SPAN><SPAN>=$_POST['auth']; </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> if(empty($auth)) </SPAN><LI class=""><SPAN> { </SPAN><LI class=alt><SPAN> echo '错误:验证码不能为空'; </SPAN><LI class=""><SPAN> die; </SPAN><LI class=alt><SPAN> } </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> if($</SPAN><SPAN class=attribute><FONT color=#ff0000>auth</FONT></SPAN><SPAN>==$_SESSION['check_auth']) </SPAN></SPAN><LI class=""><SPAN> { </SPAN><LI class=alt><SPAN> echo '正确'; </SPAN><LI class=""><SPAN> } </SPAN><LI class=alt><SPAN> else </SPAN><LI class=""><SPAN> { </SPAN><LI class=alt><SPAN> echo '错误:验证码输入错误'; </SPAN><LI class=""><SPAN> } </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span> </li></ol>
The above is the relevant knowledge about generating verification codes by the PHP GD library introduced in this article. I hope it will be helpful to everyone.