<?php header("content-type:image/png"); $num = $_GET['num']; $imagewidth=60; $imageheight=18; srand(microtime() * 100000); $numimage = imagecreate($imagewidth,$imageheight); imagecolorallocate($numimage,183,180,83); for($i=0;$i<strlen($num);$i++){ $font = mt_rand(3,5); $x = mt_rand(1,8)+$imagewidth*$i/4; $y = mt_rand(1,$imageheight/3); $color=imagecolorallocate($numimage,mt_rand(0,50),mt_rand(0,150),mt_rand(0,255)); imagestring($numimage,$font,$x,$y,$num[$i],$color); } imagepng($numimage); imagedestroy($numimage); ?> 调用方法: <?php session_start(); include_once 'conn/conn.php'; $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ'; $num = null; for($i = 0; $i < 4; $i++) { $num .= $pattern{mt_rand(0, 61)}; } ?> <div> 验证码: <input id="lgchk" type="text" /> <img src="valcode.php?num=<?php echo $num; ?>" width="55" height="18" /> </div>