Jacklovely

导航

 

php代码,主要把RGB改成随机生成,不是之前固定的七种颜色了:

 1 <?php 
 2    // ob_clean();
 3    header("content-type:image/png");
 4    $width = 110;
 5    $height = 40;
 6    $img = imagecreatetruecolor($width, $height);
 7    $string = "hello";
 8    //7种颜色,存入数组
 9    $red = imagecolorallocate($img, 255, 0, 0);
10    $white = imagecolorallocate($img, 255, 255, 255);
11    $green = imagecolorallocate($img, 0, 255, 0);
12    $blue = imagecolorallocate($img, 0, 0, 255);
13    $aaa = imagecolorallocate($img, 255, 255, 0);
14    $bbb = imagecolorallocate($img, 0, 255, 255);
15    $ccc = imagecolorallocate($img, 255, 0, 255);
16    $colors = array($white,$red,$green,$blue,$aaa,$bbb,$ccc);
17    //颜色换成随机组成的RGB,每次循环都生成一次
18    $color = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
19    //画点
20    for ($i=0; $i < 10; $i++) { 
21       $color1 = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
22       imagesetpixel($img, mt_rand(0,$width), mt_rand(0,$height), $color1);
23    }
24    //划线
25    for ($i=0; $i < 4; $i++) { 
26       $color2 = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
27       imageline($img, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $color2);
28    }
29    //生成4位验证码
30    $a1 = range(0, 9);
31    $a2 = range(a, z);
32    $a3 = range(A, Z);
33    $a4 = array_merge($a1,$a2,$a3);
34    //改用shuffle打断顺序,array_slice取出前4个字母数字。不然如果用mt_rand在循环中每次取一个,还要生成字符串,不好比对
35    shuffle($a4);
36    $a5 = array_slice($a4,0,4);
37    $num = 4;
38    $fontsize = 20;
39    for ($i=0; $i < 4; $i++) { 
40       $color3 = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
41       imagettftext($img, $fontsize, mt_rand(-30,30), $width/$num*$i+5, 30, $color3, "Fonts/msyh.ttf", $a5[$i]);
42    }
43    imagepng($img);
44  ?>

HTML代码(后缀也是php):

 1 <!doctype html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>注册</title>
 6 </head>
 7 <body>
 8     <form action="test.php" method = "get">
 9         <table>
10             <tr>
11                 <td>用户名:</td>
12                 <td><input type="text"><br/></td>
13             </tr>
14             <tr>
15                 <td>密码:</td>
16                 <td><input type="text"><br/></td>
17             </tr>
18             <tr>
19                 <td>验证码:</td>
20                 <td><input type="text" name = "txtYanZheng"><br/></td>
21             </tr>
22             <tr>
23                 <td></td>
24                 <td><img src="yanzhengma.php" id = "yanzhengma" ></form></td>
25             </tr>
26             <tr>
27                 <td><input type="submit" value="提交" ></td>
28             </tr>
29         </table>
30     <?php 
31     $var = '
32     <script type="text/javascript">
33     onload = function(){
34         var yanzhengma = document.getElementById("yanzhengma");
35         yanzhengma.onclick = function(){
36             this.src = "yanzhengma.php?"+Math.random();
37         };
38     }
39 </script>
40 ';
41     echo $var ?>
42 </body>
43 </html>

 

posted on 2016-11-09 21:03  Jacklovely  阅读(426)  评论(0编辑  收藏  举报