验证码:
class SecurityCodeRepository
{
private $width;
private $height;
private $codeNum;
private $type;
private $session;
private $fontStyle;
private $dot;
private $line;
private $image;
private $chars;
private $bgcolor;
private $isUrl;
private $saveDir;
function __construct($config =array()){
$width = 100;
$height = 40;
$codeNum = 4;
$type = 3;
$session = 'securityCode';
$fontStyle = public_path().'/fz.TTF';
$dot = 50;
$line = 4;
$bgcolor = array(
'red' => 255,
'blue' => 255,
'green' => 255
);
$isUrl = false;
$saveDir = '.';
foreach ($config as $key => $value){
switch ($key){
case 'width':
$width = $value ? $value : $width;
break;
case 'height':
$height = $value ? $value : $height;
break;
case 'codeNum':
$codeNum = $value ? $value : $codeNum;
break;
case 'type':
$type = $value ? $value : $type;
break;
case 'session':
$session = $value ? $value : $session;
break;
case 'fontStyle':
$fontStyle = $value ? $value : $fontStyle;
break;
case 'dot':
$dot = $value ? $value : $dot;
break;
case 'line':
$line = $value ? $value : $line;
break;
case 'bgcolor':
$bgcolor = $value ? $value : $bgcolor;
break;
case 'isUrl':
$isUrl = $value ? $value : $isUrl;
break;
case 'saveDir':
$saveDir = $value ? $value : $saveDir;
break;
}
}
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
$this->type = $type;
$this->session = $session;
$this->fontStyle = $fontStyle;
$this->dot = $dot;
$this->line = $line;
$this->bgcolor = $bgcolor;
$this->isUrl = $isUrl;
$this->saveDir = $saveDir;
$this->image = $this->createImage();
}
private function createImage(){
$image = imagecreatetruecolor($this->width,$this->height);
return $image;
}
private function createChar($chr){
if ($this->type == 1){
$chars = implode('',range(0,9));
}else if($this->type == 2){
$chars = implode('',array_merge(range('A','Z'),range('a','z')));
}else if ($this->type == 3){
$chars = implode('',array_merge(range('A','Z'),range('a','z'),range(0,9)));
}
$chars = str_shuffle($chars);
if ($this->codeNum > strlen($chars)){
exit('数字过大');
}
$chars = substr($chars,0,$this->codeNum);
return $chars;
}
private function interferon(){
for ($i = 0; $i <$this->line;$i++){
$color = imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($this->image,mt_rand(0,$this->width -1),mt_rand(0,$this->height -1),mt_rand(0,$this->width -1),mt_rand(0,$this->height-1),$color);
}
for ($i = 0;$i < $this->dot;$i++){
$color = imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($this->image,mt_rand(0, $this->width - 1), mt_rand(0, $this->height - 1), $color);
}
}
public function getImage($char){
$color = imagecolorallocate($this->image,$this->bgcolor['red'],$this->bgcolor['green'],$this->bgcolor['blue']);
imagefilledrectangle($this->image,0,0,$this->width,$this->height,$color);
for ($i = 0;$i <$this->codeNum;$i++){
$size = floor($this->height / 2);
$angle = mt_rand(-15,15);
$x = 10 + $i * $size;
$y = $size + floor($size / 2);
$color = imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
$text = substr($char,$i,1);
imagettftext($this->image,$size,$angle,$x,$y,$color,$this->fontStyle,$text);
}
$this->interferon();
if ($this->isUrl){
$fileName = $this->saveDir.time().'.png';
imagepng($this->image,$fileName);
imagedestroy($this->image);
return $fileName;
}else{
header("content-type:image/png");
imagepng($this->image);
imagedestroy($this->image);
}
}