PHP基于GD库验证码的制作

来源:互联网 发布:电脑软件管家 编辑:程序博客网 时间:2024/06/10 06:55

 

<?php

$count = 4;
$type = 2;
$code = getCode($count, $type);

$width = 20 * $count;
$height = 30;

$img = imagecreatetruecolor($width, $height);

$bg = imagecolorallocate($img, 200, 200, 200);
imagefill($img, 0, 0, $bg);

$color[] = imagecolorallocate($img, 54, 37, 52);
$color[] = imagecolorallocate($img, 27, 22, 69);
$color[] = imagecolorallocate($img, 124, 119, 22);
$color[] = imagecolorallocate($img, 23, 79, 16);
$color[] = imagecolorallocate($img, 10, 14, 91);

imagerectangle($img, 0, 0, $width-1, $height-1, $color[rand(0,count($color)-1)]);

for($i = 0; $i < $count; $i++){
imagettftext($img, 18, rand(-40,40), 8+(18*$i), 24, $color[rand(0,count($color)-1)], 'msyh.ttc', $code[$i]);
}

for($i = 0; $i < 200; $i++){
$randColor = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($img, rand(0,$width), rand(0,$height), $randColor);
}

for($i = 0; $i < 5; $i++){
$randColor = imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255));
imageline($img, rand(0,$width), rand(0,$height), rand(0,$width), rand(0,$height), $randColor);
}

header("Content-Type:image/png");
imagepng($img);

imagedestroy($img);

function getCode($count=4, $type=0){
$str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = null;

$end = array(9,35,61);

for($i = 0; $i < $count; $i++){
$code .= $str[rand(0,$end[$type])];
}

return $code;
}

// echo getCode();
0 0