首页 > 开发 > PHP > 正文

PHP验证码图片生成程序

2024-05-04 22:58:50
字体:
来源:转载
供稿:网友
<?php
//小猪会气功
session_start();
session_register('code');
$width = "70";//图片宽
$height = "25";//图片高
$len = "4";//生成几位验证码
$bgcolor = "#ffffff";//背景色
$noise = true;//生成杂点
$noisenum = 200;//杂点数量
$border = false;//边框
$bordercolor = "#000000";
$image = imagecreate($width, $height);
$back = getcolor($bgcolor);
imagefilledrectangle($image, 0, 0, $width, $height, $back);
$size = $width/$len;
if($size>$height) $size=$height;
$left = ($width-$len*($size+$size/10))/$size;
for ($i=0; $i<$len; $i++)
{
        $randtext = rand(0, 9);
        $code .= $randtext;
 $textcolor = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
 $font = rand(1,4).".ttf";
 $randsize = rand($size-$size/10, $size+$size/10);
 $location = $left+($i*$size+$size/10);
 imagettftext($image, $randsize, rand(-18,18), $location, rand($size-$size/10, $size+$size/10), $textcolor, $font, $randtext);
}
if($noise == true) setnoise();
$_session['code'] = $code;
$bordercolor = getcolor($bordercolor);
if($border==true) imagerectangle($image, 0, 0, $width-1, $height-1, $bordercolor);
header("content-type: image/png");
imagepng($image);
imagedestroy($image);
function getcolor($color)
{
     global $image;
     $color = eregi_replace ("^#","",$color);
     $r = $color[0].$color[1];
     $r = hexdec ($r);
     $b = $color[2].$color[3];
     $b = hexdec ($b);
     $g = $color[4].$color[5];
     $g = hexdec ($g);
     $color = imagecolorallocate ($image, $r, $b, $g);
     return $color;
}
function setnoise()
{
 global $image, $width, $height, $back, $noisenum;
 for ($i=0; $i<$noisenum; $i++){
  $randcolor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255)); 
  imagesetpixel($image, rand(0, $width), rand(0, $height), $randcolor);
 }
}
?>

最大的网站源码资源下载站,

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表