1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| <?php
$img_w = 100; $img_h = 30; $img = imagecreatetruecolor($img_w, $img_h); $bg_color = imagecolorallocate($img,0xcc,0xcc,0xcc); imagefill($img,0,0,$bg_color);
$count = 4; $charset = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; $charset_len = strlen($charset)-1; $code = ''; for($i=0; $i<$count; ++$i) { $code .= $charset[mt_rand(0,$charset_len)]; }
$fontSize = 16; $fontStyle = realpath('./SourceCodePro-Bold.ttf');
for($i=0; $i<$count; ++$i){ $fontColor = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,50),mt_rand(0,255)); imagettftext ( $img, //画布资源 $fontSize, //文字大小 mt_rand(0,20) - mt_rand(0,25), //随机设置文字倾斜角度 $fontSize*$i+20,mt_rand($img_h/2,$img_h), //随机设置文字坐标,并自动计算间距 $fontColor, //文字颜色 $fontStyle, //文字字体 $code[$i] //文字内容 ); }
for($i=0; $i<300; ++$i){ $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); imagesetpixel($img,mt_rand(0,$img_w),mt_rand(0,$img_h),$color); } $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); imagerectangle($img, mt_rand(0,$img_h), mt_rand(0,$img_h), mt_rand(0,$img_h), mt_rand(0,$img_h), $color); imageellipse($img, mt_rand(0,$img_h), mt_rand(0,$img_h), 20, 20, $color);
for($i=0; $i<3; ++$i){ $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); imageline($img,mt_rand(0,$img_w),0,mt_rand(0,$img_h*5),$img_h,$color); }
session_start(); $_SESSION['captcha'] = $code; $fontfile =realpath('SourceCodePro-Bold.ttf');
header('Content-Type: image/gif'); imagegif($img);
|