<?php
ini_set('session.cookie_httponly', 1);
session_start();
/*设置输出格式*/
header('content-type:image/png');

/*设置宽高*/
$width = 70;
$height = 33;

/*绘制画布*/
$image = imagecreate($width, $height);

/*设置颜色*/
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);

/*生成随机字符*/
$rand_str = '';
for ($i = 0; $i < 4; $i++){
    $rand_str .= dechex(mt_rand(0,15));
}

/*设置状态*/
$_SESSION['code'] = $rand_str;

/*显示内容*/
imagefill($image, 0, 0, $white);

/*生成随机线条*/
for ($i=0; $i <mt_rand(1,5) ; $i++) { 
    $sjys = imagecolorallocate($image, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
    imageline($image, mt_rand(1,$width), mt_rand(1,$height), mt_rand(1,$width), mt_rand(1,$height), $sjys);
}

/*常规验证码
imagestring($image, 7, 7, 5, $rand_str, $white);//字体大小、宽度、高度常规
 */

/*复杂化验证码*/
for ($i=0; $i <strlen($_SESSION['code']) ; $i++) { 
    $sjys = imagecolorallocate($image, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100));
    imagestring($image, mt_rand(3,8),$i * $width/4 + mt_rand(0,10) , mt_rand(5,10), $_SESSION['code'][$i], $sjys);/*字体大小、宽度、高度*/
}

/*绘制边框线*/
imagerectangle($image, 0, 0, $width-1, $height-1, $black);

/*输出图像*/
imagepng($image);

/*清除上方占用资源*/
imagedestroy($image);

?>
Last modification:September 19th, 2019 at 05:10 pm
如果觉得我的文章对你有用,请随意赞赏