PHP学习笔记(5)GD库画验证码

PHP学习笔记(5)GD库画验证码

 <?php
header("content-type:image/png");
$width = 500;
$height = 500;
$img = imagecreatetruecolor($width, $height);
$string = "hello";
//7种颜色,存入数组
$red = imagecolorallocate($img, 255, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
$aaa = imagecolorallocate($img, 255, 255, 0);
$bbb = imagecolorallocate($img, 0, 255, 255);
$ccc = imagecolorallocate($img, 255, 0, 255);
$colors = array($white,$red,$green,$blue,$aaa,$bbb,$ccc);
//画点
for ($i=0; $i < 1000; $i++) {
imagesetpixel($img, mt_rand(0,$width), mt_rand(0,$height), $colors[mt_rand(0,6)]);
}
//划线
for ($i=0; $i < 200; $i++) {
imageline($img, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $colors[mt_rand(0,6)]);
}
//生成4位验证码
$a1 = range(0, 9);
$a2 = range(a, z);
$a3 = range(A, Z);
$a4 = array_merge($a1,$a2,$a3);
$num = 4;
$fontsize = 60;
for ($i=0; $i < 4; $i++) {
imagettftext($img, $fontsize, mt_rand(-45,45), $width/$num*$i+$fontsize, $height/2, $colors[mt_rand(0,6)], "Fonts/msyh.ttf", $a4[mt_rand(0,61)]);
} imagepng($img);
?>

------------------------------------------------------------------------------- -----------------------------------------------------------------------------

下面的小一点:

PHP学习笔记(5)GD库画验证码

 <?php
header("content-type:image/png");
$width = 110;
$height = 40;
$img = imagecreatetruecolor($width, $height);
$string = "hello";
//7种颜色,存入数组
$red = imagecolorallocate($img, 255, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
$aaa = imagecolorallocate($img, 255, 255, 0);
$bbb = imagecolorallocate($img, 0, 255, 255);
$ccc = imagecolorallocate($img, 255, 0, 255);
$colors = array($white,$red,$green,$blue,$aaa,$bbb,$ccc);
//画点
for ($i=0; $i < 10; $i++) {
imagesetpixel($img, mt_rand(0,$width), mt_rand(0,$height), $colors[mt_rand(0,6)]);
}
//划线
for ($i=0; $i < 4; $i++) {
imageline($img, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $colors[mt_rand(0,6)]);
}
//生成4位验证码
$a1 = range(0, 9);
$a2 = range(a, z);
$a3 = range(A, Z);
$a4 = array_merge($a1,$a2,$a3);
$num = 4;
$fontsize = 20;
for ($i=0; $i < 4; $i++) {
imagettftext($img, $fontsize, mt_rand(-45,45), $width/$num*$i+5, 30, $colors[mt_rand(0,6)], "Fonts/msyh.ttf", $a4[mt_rand(0,61)]);
}
imagepng($img);
?>
上一篇:swift创建对象use of undeclared type 自己的类


下一篇:final-review