示例#1
0
 * Date: 2015/10/4
 * Time: 16:14
 * file: ToolsVerifyCode.class.php 生成验证码
 */
session_start();
$num = '';
for ($i = 0; $i < 4; $i++) {
    $num .= dechex(mt_rand(0, 15));
}
$_SESSION['verify'] = $num;
$imagewidth = 60;
$imageheight = 18;
//创建画布
$image = imagecreate($imagewidth, $imageheight);
// 设置背景颜色
imagecolorallocate($image, 240, 240, 240);
// 添加文字
for ($i = 0; $i < strlen($num); $i++) {
    $randColor = imagecolorallocate($image, mt_rand(0, 50), mt_rand(0, 50), mt_rand(0, 50));
    $x = $imagewidth * $i / 4 + mt_rand(2, 5);
    $y = mt_rand(1, $imageheight / 4);
    imagestring($image, 5, $x, $y, $num[$i], $randColor);
}
//添加干扰点
for ($i = 0; $i < 200; $i++) {
    $randColor = imageColorallocate($image, mt_rand(100, 250), mt_rand(100, 250), mt_rand(100, 250));
    imagesetpixel($image, mt_rand(0, 60), mt_rand(0, 18), $randColor);
}
header('content-type: image/png');
imagepng($image);
imagedestroy($image);
示例#2
0
$charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';
$font = '/asset/msyh.ttf';
$im = imagecreatetruecolor($imgwidth, $imgheight);
$while = imageColorAllocate($im, 255, 255, 255);
imagefill($im, 0, 0, $while);
//填充图像
//取得字符串
$authstr = '';
$_len = strlen($charset) - 1;
for ($i = 0; $i < $codelen; $i++) {
    $authstr .= $charset[mt_rand(0, $_len)];
}
session_start();
$_SESSION[$t . "Captcha"] = strtolower($authstr);
for ($i = 0; $i < $imgwidth; $i++) {
    $randcolor = imageColorallocate($im, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
    imagestring($im, mt_rand(1, 5), mt_rand(0, $imgwidth), mt_rand(0, $imgheight), '*', $randcolor);
}
//随机画线,线条数量=字符数量(随便)
for ($i = 0; $i < $codelen; $i++) {
    $randcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
    imageline($im, 0, mt_rand(0, $imgheight), $imgwidth, mt_rand(0, $imgheight), $randcolor);
}
$_x = intval($imgwidth / $codelen);
//计算字符距离
$_y = intval($imgheight * 0.7);
//字符显示在图片70%的位置
for ($i = 0; $i < strlen($authstr); $i++) {
    $randcolor = imagecolorallocate($im, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
    imagettftext($im, $fontsize, mt_rand(-30, 30), $i * $_x + 3, $_y, $randcolor, $font, $authstr[$i]);
}