php生成验证码并显示在浏览器

来源:互联网 发布:wps网络受限 编辑:程序博客网 时间:2024/06/04 19:36
php文件名是image.func.php
<?php        session_start();// //创建画布$width=80;$height=28;$image=imagecreatetruecolor($width, $height);//返回以画像,默认黑色$white=imagecolorallocate($image,255,255,255);//为画布上色$black=imagecolorallocate($image,0,0,0);//为画布上色// //用填充矩形填充画布//imagefilledrectangle()意思是在$image画布上画了一个$white颜色的矩形,其左上角坐标为 x1,y1,右下角坐标为 x2,y2。0, 0 是图像的最左上角。并无返回数据imagefilledrectangle($image, 0, 0, $width, $height, $white);$chars=buildRandomString($type,$length);$_SESSION[$sess_name]=$chars;$fontfiles=array("msyh.ttc","msyhbd.ttc","msyhl.ttc","simsun.ttc");for ($i=0; $i <$length ; $i++) { //mt_rand()生成x到y范围内随机数$size=mt_rand(14,18);$angle=mt_rand(-15,15);$x=5+$i*$size;$y=mt_rand(20,26);$color=imagecolorallocate($image,mt_rand(50,90),mt_rand(80,200),mt_rand(90,180));$fontfile="../fonts/".$fontfiles[mt_rand(0,count($fontfiles)-1)];$text=substr($chars,$i,1);//向图像写入文本imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text);}if ($pixel) {for ($i=0; $i < 50; $i++) { //imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。imagesetpixel($image,mt_rand(0,$width-1),mt_rand(0,$height-1),$black);}}if ($line) {for ($i=1; $i < $line; $i++) { $color=imagecolorallocate($image,mt_rand(50,90),mt_rand(80,200),mt_rand(90,180));//imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。imageline($image,mt_rand(0,$width-1),mt_rand(0,$height-1),mt_rand(0,$width-1),mt_rand(0,$height-1),$color);}}header("content-type:image/png");//imagegif — 输出图象到浏览器或文件。// imagegif($image);imagepng($image);imagedestroy($image);?>


html代码

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title></head><body><img src="http://localhost/PHPDemo/lib/image.func.php" onclick="getVerify()" id="verify"><script src="https://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script><script type="text/javascript">function getVerify() {$("#verify").attr("src","http://localhost/PHPDemo/lib/image.func.php");}</script></body></html>



0 0