1 <?php
2 // 1 造画布
3 $im = imagecreatetruecolor(50,25);
4 // 2 造颜料准备写字
5 $red = imagecolorallocate($im,255,0,0);
6 // 浅色背景
7 $gray = imagecolorallocate($im,220,220,220);
8 // 随机颜色
9 $randcolor = imagecolorallocate($im,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
10 $linecolor1 = imagecolorallocate($im,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
11 $linecolor2 = imagecolorallocate($im,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
12 $linecolor3 = imagecolorallocate($im,mt_rand(100,150),mt_rand(100,150),mt_rand(100,150));
13 // 填充背景
14 imagefill($im,0,0,$gray);
15 // 画干扰线
16 imageline($im,0,mt_rand(0,25),50,mt_rand(0,25),$linecolor1);
17 imageline($im,0,mt_rand(0,25),50,mt_rand(0,25),$linecolor2);
18 imageline($im,0,mt_rand(0,25),50,mt_rand(0,25),$linecolor3);
19 /*
20 3 写字
21 imagestring — 水平地画一行字符串
22 说明
23 bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col )
24 参数分别代表: 画布资源,字体大小(1-5中选择), 字符最左上角的x坐标,y坐标 ,要写的字符串,颜色
25 */
26 $str = substr(str_shuffle('ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'),0,4);
27 imagestring($im,5,8,5,$str,$randcolor);
28 header('content-type: image/png');
29 imagepng($im);
30 ?>
1 <html>
2 <script type="text/javascript">
3 function code(obj){
4 obj.src = 'yan.php?s=' + Math.random();
5 }
6 </script>
7 <img src="yan.php" onclick="code(this)"/>
8 </html>