PHP 生成验证码
<?php/**
* php 生成验证码
* yzm.php
* @author flyer0126
*/
//定义图像的宽、高
$width = 75;
$height = 25;
//创建一个真彩色图像
$img = imagecreatetruecolor($width, $height);
//为一幅图像分配颜色
$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $white);
//画一个黑色矩形边框
$black = imagecolorallocate($img, 0, 0, 0);
imagerectangle($img, 0, 0, $width-1, $height-1, $black);
//随机画直线、打点、创建验证码
$msg = '';
for ($i = 0; $i < 4; $i++){
$randcolor = 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), $randcolor);
imagestring($img, 1, mt_rand(1, $width), mt_rand(1, $height), ".", $randcolor);
$msg .= dechex(mt_rand(0,15));
}
//将验证码放到图片上
for ($j = 0; $j < strlen($msg); $j++){
$randcolor = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagestring($img, mt_rand(3, 5), $j*$width/strlen($msg) + mt_rand(1, 10), mt_rand(1, $height/2), $msg[$j], $randcolor);
}
//以JPEG格式输出图像
header("Content-Type:image/jpeg");
imagejpeg($img);
//销毁图像【释放关联内存】
imagedestroy($img);
?>
<!-- 实现页面点击切换效果 -->
<img src="yzm.php" title="click change" >
页:
[1]