网络编程 
首页 > 网络编程 > 浏览文章

一漂亮的PHP图片验证码实例

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )

一、显示效果
一漂亮的PHP图片验证码实例
二、代码如下
复制代码 代码如下:/*
 *  @Author fy
 */

$imgwidth =100; //图片宽度
$imgheight =40; //图片高度
$codelen =4; //验证码长度
$fontsize =20; //字体大小
$charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';
$font = 'Fonts/segoesc.ttf';

$im=imagecreatetruecolor($imgwidth,$imgheight);

$while=imageColorAllocate($im,255,255,255);
imagefill($im,0,0,$while); //填充图像

//取得字符串
$authstr='';
$_len = strlen($charset)-1;
for ($i=0;$i<$codelen;$i++) {
 $authstr .= $charset[mt_rand(0,$_len)];
}

session_start();
$_SESSION['scode']=strtolower($authstr);//全部转为小写,主要是为了不区分大小写

//随机画点,已经改为划星星了
for ($i=0;$i<$imgwidth;$i++){
    $randcolor=imageColorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
 imagestring($im,mt_rand(1,5), mt_rand(0,$imgwidth),mt_rand(0,$imgheight), '*',$randcolor);
    //imagesetpixel($im,mt_rand(0,$imgwidth),mt_rand(0,$imgheight),$randcolor);
}
//随机画线,线条数量=字符数量(随便)
for($i=0;$i<$codelen;$i++)

 $randcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
 imageline($im,0,mt_rand(0,$imgheight),$imgwidth,mt_rand(0,$imgheight),$randcolor);
}

$_x=intval($imgwidth/$codelen); //计算字符距离
$_y=intval($imgheight*0.7); //字符显示在图片70%的位置
for($i=0;$i<strlen($authstr);$i++){

 $randcolor=imagecolorallocate($im,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
 //imagestring($im,5,$j,5,$imgstr[$i],$color3);
 // imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text )
 imagettftext($im,$fontsize,mt_rand(-30,30),$i*$_x+3,$_y,$randcolor,$font,$authstr[$i]);

}

//生成图像
header("content-type:image/PNG");
imagePNG($im);
imageDestroy($im);


 

上一篇:一个显示效果非常不错的PHP错误、异常处理类
下一篇:PHP中nowdoc和heredoc使用需要注意的一点
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网