<?php
//éªè¯ç ç±»
class ValidateCode {
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//éæºå å
private $code;//éªè¯ç
private $codelen = 4;//éªè¯ç é¿åº¦
private $width = 90;//宽度
private $height = 40;//é«åº¦
private $img;//å¾å½¢èµæºå¥æ
private $font;//æå®çåä½
private $fontsize = 20;//æå®åä½å¤§å°
private $fontcolor;//æå®åä½é¢è²
//æé æ¹æ³åå§å
public function __construct() {
$this->font = dirname(__FILE__).'/font/elephant.ttf';//注æåä½è·¯å¾è¦å对ï¼å¦åæ¾ç¤ºä¸äºå¾ç
}
//çæéæºç
private function createCode() {
$_len = strlen($this->charset)-1;
for ($i=0;$i<$this->codelen;$i++) {
$this->code .= $this->charset[mt_rand(0,$_len)];
}
}
//çæèæ¯
private function createBg() {
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
//çææå
private function createFont() {
$_x = $this->width / $this->codelen;
for ($i=0;$i<$this->codelen;$i++) {
$this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);
}
}
//çæ线æ¡ãéªè±
private function createLine() {
//线æ¡
for ($i=0;$i<6;$i++) {
$color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
//éªè±
for ($i=0;$i<100;$i++) {
$color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}
//è¾åº
private function outPut() {
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
//对å¤çæ
public function doimg() {
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
//è·åéªè¯ç
public function getCode() {
return strtolower($this->code);
}
}
温馨提示:答案为网友推荐,仅供参考