>백엔드 개발 >PHP 튜토리얼 >php 实现验证码制造

php 实现验证码制造

WBOY
WBOY원래의
2016-06-13 12:21:231014검색

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

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.