如果有什么好的提议,可以留言,一起改善代码:
<?php //$i为要写在画板的位置,$num为要写的文字 function rand4string($i,$num){ global $image_width,$image_height,$img; $font=mt_rand(3,5); $x=mt_rand(1,8)+$image_width*$i/6; $y=mt_rand(1,$image_height/4); $color=imagecolorallocate($img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)); imagestring($img,$font,$x,$y,$num,$color); } session_start(); header("content-type:image/png"); $image_width=80; $image_height=18; $img = imagecreate($image_width,$image_height); imagecolorallocate($img,255,255,255); $random4symbol = array("+","-","x"); //三个符号进行循环 $result = 0; $symbol = $random4symbol[mt_rand(0,count($random4symbol)-1)]; $num1 = mt_rand(1,9); rand4string(0,$num1); $num2 = mt_rand(1,9); switch ($symbol){ case "+": $result = $num1 + $num2; rand4string(1,"+"); break; case "-": $result = $num1 - $num2; rand4string(1,"-"); break; case "x": $result = $num1 * $num2; rand4string(1,"x"); break; } rand4string(2,$num2); rand4string(3,"="); rand4string(4,"?"); $_SESSION['code'] = $result; imagepng($img); imagedestroy($img); ?>
打完收工。。呵呵。