php 实现验证码制作
先给看下 大致的效果
那么接下来的就直接贴代码吧
<span style="color: #000000;">php </span><span style="color: #800080;">$image</span> = imagecreatetruecolor(100, 30); <span style="color: #008000;">//</span><span style="color: #008000;">创建画布</span> <span style="color: #800080;">$imagecolor</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, 255, 255, 255); <span style="color: #008000;">//</span><span style="color: #008000;">背景色</span><span style="color: #000000;"> imagefill(</span><span style="color: #800080;">$image</span>, 0, 0, <span style="color: #800080;">$imagecolor</span>); <span style="color: #008000;">//</span><span style="color: #008000;">填充背景色</span> <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$i++ ){ <span style="color: #008000;">//</span><span style="color: #008000;">循环4位数</span> <span style="color: #800080;">$fontsize</span> = 6<span style="color: #000000;">; </span><span style="color: #800080;">$fontcolor</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, <span style="color: #008080;">rand</span>(0, 200), <span style="color: #008080;">rand</span>(0, 200), <span style="color: #008080;">rand</span>(0, 200<span style="color: #000000;">)); </span><span style="color: #800080;">$fontcontent</span> = <span style="color: #008080;">rand</span>(0, 9<span style="color: #000000;">); </span><span style="color: #800080;">$x</span> = <span style="color: #800080;">$i</span>*100/4 + <span style="color: #008080;">rand</span>(5, 15<span style="color: #000000;">); </span><span style="color: #800080;">$y</span> = <span style="color: #008080;">rand</span>(5, 10<span style="color: #000000;">); imagestring(</span><span style="color: #800080;">$image</span>, <span style="color: #800080;">$fontsize</span>, <span style="color: #800080;">$x</span>, <span style="color: #800080;">$y</span>, <span style="color: #800080;">$fontcontent</span>, <span style="color: #800080;">$fontcolor</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$i++ ){ <span style="color: #008000;">//</span><span style="color: #008000;">循环 添加干扰点</span> <span style="color: #800080;">$pointcolor</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, <span style="color: #008080;">rand</span>(50, 200), <span style="color: #008080;">rand</span>(50, 200), <span style="color: #008080;">rand</span>(50, 200<span style="color: #000000;">)); </span><span style="color: #800080;">$x</span> = <span style="color: #008080;">rand</span>(1, 99<span style="color: #000000;">); </span><span style="color: #800080;">$y</span> = <span style="color: #008080;">rand</span>(1, 29<span style="color: #000000;">); imagesetpixel(</span><span style="color: #800080;">$image</span>, <span style="color: #800080;">$x</span>, <span style="color: #800080;">$y</span>, <span style="color: #800080;">$pointcolor</span><span style="color: #000000;">); } </span><span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$i++){ <span style="color: #008000;">//</span><span style="color: #008000;">循环 添加干扰线 </span> <span style="color: #800080;">$linecolor</span> = imagecolorallocate(<span style="color: #800080;">$image</span>, <span style="color: #008080;">rand</span>(100, 250), <span style="color: #008080;">rand</span>(100, 250), <span style="color: #008080;">rand</span>(100, 250<span style="color: #000000;">)); </span><span style="color: #800080;">$x1</span> = <span style="color: #008080;">rand</span>(1, 25<span style="color: #000000;">); </span><span style="color: #800080;">$x2</span> = <span style="color: #008080;">rand</span>(50, 75<span style="color: #000000;">); </span><span style="color: #800080;">$y1</span> = <span style="color: #008080;">rand</span>(1, 15<span style="color: #000000;">); </span><span style="color: #800080;">$y2</span> = <span style="color: #008080;">rand</span>(15, 25<span style="color: #000000;">); imageline(</span><span style="color: #800080;">$image</span>, <span style="color: #800080;">$x1</span>, <span style="color: #800080;">$y1</span>, <span style="color: #800080;">$x2</span>, <span style="color: #800080;">$y2</span>, <span style="color: #800080;">$linecolor</span><span style="color: #000000;">); } </span><span style="color: #008080;">header</span>("content-type:image/png"<span style="color: #000000;">); imagepng(</span><span style="color: #800080;">$image</span><span style="color: #000000;">); imagedestroy(</span><span style="color: #800080;">$image</span><span style="color: #000000;">);</span>?>