20,359
社区成员
发帖
与我相关
我的任务
分享
<?php
/*
* Created on 2010-12-14 下午01:35:27
* Author:hst001
* Version:1.0
*
*/
class cheakcode
{
private $alltext;
private $imgw; //图片宽度
private $imgh; //图片高度
private $fs; //字体大小(px)
private $fso; //字体大小变化量,一般在1到3个像素
private $fa; //字体旋转角度
private $lro; //左右偏移
private $udo; //上下偏移
private $ffldx; //第一个字符左下角的x坐标
private $ffldy; //第一个字符左下角的y坐标
private $bgcolor; //背景色
private $fc; //文本色
private $ff; //字体文件
private $textnum; //验证码个数
function __construct()
{
$this->alltext='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
}
public function setsize( $imw=100, $imh=30, $fontsize=17, $fontsizeoffset=2, $fontangle=15, $LRO=1, $UDO=1, $firstx=10, $firsty=20)
{
$this->imgw=$imw;
$this->imgh=$imh;
$this->fs=$fontsize;
$this->fso=$fontsizeoffset;
$this->fa=$fontangle;
$this->lro=$LRO;
$this->udo=$UDO;
$this->ffldx=$firstx;
$this->ffldy=$firsty;
}
public function setcolor( $backgroudcolor='255,255,255', $textcolor='0,0,255')
{
$this->bgcolor=$backgroudcolor;
$this->fc=$textcolor; //在 addtext里面转为颜色
}
public function setother( $fontfile='arialbi.ttf', $intnum=4)
{
$this->ff=$fontfile;
$this->textnum=$intnum;
}
private function createimg()
{
$bg_arr=explode( ",", $this->bgcolor);
$im=imagecreate( $this->imgw, $this->imgh);
$bgcolor=imagecolorallocate( $im, $bg_arr[0], $bg_arr[1], $bg_arr[2]); //设置背景色
return $im;
}
private function addtext($im)
{
$fontcolor_arr=explode( ",", $this->fc);
$this->fc=imagecolorallocate( $im, $fontcolor_arr[0], $fontcolor_arr[1], $fontcolor_arr[2]);
for( $i=1; $i<=$this->textnum; $i++)
{
$text=$this->alltext{rand(0,61)};
$cheaktext=$cheaktext.$text;
imagettftext( $im, rand($this->fs - $this->fso,$this->fs + $this->fso), rand(- $this->fa,$this->fa), $this->ffldx + $i * 13+rand(- $this->lro,$this->lro), $this->ffldy + rand(- $this->udo,$this->udo), $this->fc, $this->ff, $text );
}
return $cheaktext;
}
public function showimg()
{
$im=$this->createimg();
$cheaktext=$this->addtext($im);
session_start();
$_SESSION['cheaktext']=$cheaktext;
header("Content-type: image/gif");
imagegif($im);
imagedestroy($im);
}
}
/*
*前天看了一篇关于如何用程序去识别破解验证码的文章,才发现现在很多网站的验证码都是摆设,于是决定自己写一个,
* 由于干扰点不仅对防破解没什么帮助,有时还影响阅读,所以不加上去
* 干扰线过再研究一下那篇文章,过两天有空补上来
*
*
* ============ DEMO===========
* 为能适应更多情况,可设置参数较多
*
* setsize参数说明:【$imw:图像宽度,像素】【$imh:图像高度,像素】【$fontsize:字体大小,像素】
* 【$fontsizeoffset:字体大小变化量,1-3适宜】【$fontangle:字体偏移角度,12-20适宜】
* 【$LRO:字体粘连度,1-2适宜】【$UDO:字体上下随机调整,1-3适宜】
* 【$firstx:第一个字体左下角在图片中的X坐标,自己看着办】【$firsty:第一个字体左下角在图片中的Y坐标,自己看着办】
*
* setsize参数说明:【$backgroudcolor:背景色,string,如'255,255,255'】
* 【$textcolor:字体颜色,设置同上】
*
* setother参数说明:【$fontfile:True Type字体文件所在位置,打开你电脑 C:\WINDOWS\Fonts 自己选,至于版权问题就不知道了】
* 【$intnum:字体个数,就是验证码个数】
*
* 所有参数一般都综合来设置
*
*/
$code=new cheakcode;
$code->setsize($imw=100, $imh=30, $fontsize=17, $fontsizeoffset=2, $fontangle=15, $LRO=1, $UDO=1, $firstx=10, $firsty=20);
$code->setcolor($backgroudcolor='255,255,255', $textcolor='0,0,255');
$code->setother($fontfile='arialbi.ttf', $intnum=4);
$code->showimg();
?>