验证码偶尔会有乱码 小白一枚,求大神指点
public function code($length = 4, $fontSize = 30, $flag = false)
{
session_start();
$str = '23456789';
// $str = '23456789qwertyupasdfghjkzxcvbnmQWRETYUPASDFGHJKZXCVBNM';
$strZh = '李王张刘陈杨赵黄周吴徐孙胡朱高林何郭马罗梁宋郑谢韩唐冯于董萧程曹袁邓许傅沈曾彭吕苏卢蒋蔡贾丁魏薛叶阎余潘杜戴夏钟汪田任姜范方石姚谭廖邹熊金陆郝白崔康毛邱秦江史';
//根据字符的个数计算出画布的宽度
$width = $length * $fontSize * 1.2;
$height = $fontSize * 2.5;
//创建画布
$img = imagecreatetruecolor($width, $height);
//分配颜色
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
if ($flag) {
for ($i = 0; $i < $length; $i ++) {
$code[$i] = mb_substr($strZh, mt_rand(0, mb_strlen($strZh, 'utf-8')-1), 1, 'utf-8');
//依次将数组中的字符输出画布上
imagettftext($img, $fontSize, mt_rand(-40, 40), $i * $fontSize + 15, $height / 2 + 5, $white, public_path().'/fonts/STKAITI.TTF', $code[$i]);
}
} else {
//根据字符的个数,循环输出字符到画布上
for ($i = 0; $i < $length; $i ++) {
//随机取出单个字符,并且保存在数组里面
$code[$i] = $str{mt_rand(0, strlen($str)-1)};
//依次将数组中的字符输出画布上
imagettftext($img, $fontSize, mt_rand(-40, 40), $i * $fontSize + 15, $height / 2 + 5, $white, public_path().'/fonts/consola.ttf', $code[$i]);
}
}
//var_dump($code);
//将数组中元素转为字符串,保存在session中
$_SESSION['code'] = strtolower(implode($code)); //绘制干扰点
for ($i = 0; $i < 200; $i ++) {
$color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), $color);
} //干扰线
for ($i = 0; $i < 5; $i ++) {
$color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imageline($img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $color);
}
//输出验证码
header('Content-type:image/jpeg');
imagejpeg($img);
}
public function code($length = 4, $fontSize = 30, $flag = false)
{
session_start();
$str = '23456789';
// $str = '23456789qwertyupasdfghjkzxcvbnmQWRETYUPASDFGHJKZXCVBNM';
$strZh = '李王张刘陈杨赵黄周吴徐孙胡朱高林何郭马罗梁宋郑谢韩唐冯于董萧程曹袁邓许傅沈曾彭吕苏卢蒋蔡贾丁魏薛叶阎余潘杜戴夏钟汪田任姜范方石姚谭廖邹熊金陆郝白崔康毛邱秦江史';
//根据字符的个数计算出画布的宽度
$width = $length * $fontSize * 1.2;
$height = $fontSize * 2.5;
//创建画布
$img = imagecreatetruecolor($width, $height);
//分配颜色
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
if ($flag) {
for ($i = 0; $i < $length; $i ++) {
$code[$i] = mb_substr($strZh, mt_rand(0, mb_strlen($strZh, 'utf-8')-1), 1, 'utf-8');
//依次将数组中的字符输出画布上
imagettftext($img, $fontSize, mt_rand(-40, 40), $i * $fontSize + 15, $height / 2 + 5, $white, public_path().'/fonts/STKAITI.TTF', $code[$i]);
}
} else {
//根据字符的个数,循环输出字符到画布上
for ($i = 0; $i < $length; $i ++) {
//随机取出单个字符,并且保存在数组里面
$code[$i] = $str{mt_rand(0, strlen($str)-1)};
//依次将数组中的字符输出画布上
imagettftext($img, $fontSize, mt_rand(-40, 40), $i * $fontSize + 15, $height / 2 + 5, $white, public_path().'/fonts/consola.ttf', $code[$i]);
}
}
//var_dump($code);
//将数组中元素转为字符串,保存在session中
$_SESSION['code'] = strtolower(implode($code)); //绘制干扰点
for ($i = 0; $i < 200; $i ++) {
$color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), $color);
} //干扰线
for ($i = 0; $i < 5; $i ++) {
$color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imageline($img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $color);
}
//输出验证码
header('Content-type:image/jpeg');
imagejpeg($img);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货