[php]
<?php
header("content-type:text/html;charset=utf-8");
//变量初始化
$str = "";
$filename = "./images/captcha.png";
$font = 5;
//生成随机验证码字符串
//array_merge合并一个或多个数组
$arr_list = array_merge(range('A','Z'),range(0,9));
//array_rand从数组中随机取出一个或多个单元
$index_list = array_rand($arr_list,8); //随机取8个下标
shuffle($index_list); //打乱数组
foreach($index_list as $value)
{
$str .= $arr_list[$value];
}
//创建画布
$img = imagecreatefrompng($filename);
//分配颜色,随机字体颜色
$color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
//获取画布和字体尺寸
$imgWidth = imagesx($img);
$imgHeight = imagesy($img);
$fontWidth = imagefontwidth($font);
$fontHeight = imagefontheight($font);
//计算字符串的起始坐标
$x = ($imgWidth -$fontWidth*strlen($str))/2;
$y = ($imgHeight - $fontHeight)/2;
//将验证码字符串写入画布上
imagestring($img,$font,$x,$y,$str,$color);
//输出图像
header("content-type:image/png");
imagepng($img,'./images/captcha_new.png');
imagedestroy($img);
?>
[/php]

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注