实例化类,然后调用类run()方法,即可生成默认的验证码图片,验证码图片是根据.ttf的字体文件生成的,所以必须要有字体文件,字体文件和类同级目录,要更改默认设置可以实例化类是传入参数、值的数组。
调用getVerifyCode()方法会返回生成的验证码字符串。
修改字体在219行。
<?php
class MyCaptchaAction {
/**
* @var integer the width of the generated CAPTCHA image. Defaults to 120.
* 图片宽度,默认120
*/
public $width = 120;
/**
* @var integer the height of the generated CAPTCHA image. Defaults to 50.
* 图片高度,默认50
*/
public $height = 50;
/**
* @var integer padding around the text. Defaults to 2.
* 图片内边距,默认2
*/
public $padding = 2;
/**
* @var integer the background color. For example, 0x55FF00.
* Defaults to 0xFFFFFF, meaning white color.
* 背景色,默认白色
*/
public $backColor = 0xFFFFFF;
/**
* @var integer the font color. For example, 0x55FF00. Defaults to 0x2040A0 (blue color).
* 字体颜色,默认蓝色
*/
public $foreColor = 0x2040A0;
/**
* @var boolean whether to use transparent background. Defaults to false.
* 是否背景透明,默认否
*/
public $transparent = false;
/**
* @var integer the minimum length for randomly generated word. Defaults to 6.
* 验证码字符串最小长度,默认6
*/
public $minLength = 6;
/**
* @var integer the maximum length for randomly generated word. Defaults to 7.
* 验证码字符串最大长度,默认7
*/
public $maxLength = 7;
/**
* @var integer the offset between characters. Defaults to -2. You can adjust this property
* in order to decrease or increase the readability of the captcha.
* @since 1.1.7
* 字符间偏移量
**/
public $offset = -2;
/**
* @var string the TrueType font file. Defaults to Duality.ttf which is provided
* with the Yii release.
* 字体文件,默认是Duality.ttf,字体文件和类文件同级目录。
*/
public $fontFile;
/**
* 构造函数,类属性可以通过这里初始化,传入数组的方式,
*/
public function __construct($params){
foreach ($params as $key => $value){
$this->$key = $value;
}
}
/**
* 获取产生的验证码字符串
*/
public function getVerifyCode(){
return $this->generateVerifyCode();
}
/**
* 实例化类之后,调用此方法生存验证码图片
*/
public function run(){
$this->renderImageGD($this->getVerifyCode());
}
/**
* Generates a new verification code.
* @return string the generated verification code
*/
protected function generateVerifyCode()
{
if($this->minLength < 3)
$this->minLength = 3;
if($this->maxLength > 20)
$this->maxLength = 20;
if($this->minLength > $this->maxLength)
$this->maxLength = $this->minLength;
$length = mt_rand($this->minLength,$this->maxLength);
$letters = '0123456789abcdefghijklmnopqrstuvwxyz';
$code = '';
for($i = 0; $i < $length; ++$i)
{
$code.=$letters{mt_rand(0,34)};
}
return $code;
}
/**
* Renders the CAPTCHA image based on the code using GD library.
* @param string $code the verification code
* @since 1.1.13
*/
protected function renderImageGD($code)
{
$image = imagecreatetruecolor($this->width,$this->height);
//分配颜色
$backColor = imagecolorallocate($image,
(int)($this->backColor % 0x1000000 / 0x10000),
(int)($this->backColor % 0x10000 / 0x100),
$this->backColor % 0x100);
imagefilledrectangle($image,0,0,$this->width,$this->height,$backColor);
//取消之前分配的颜色
imagecolordeallocate($image,$backColor);
if($this->transparent)
imagecolortransparent($image,$backColor);
$foreColor = imagecolorallocate($image,
(int)($this->foreColor % 0x1000000 / 0x10000),
(int)($this->foreColor % 0x10000 / 0x100),
$this->foreColor % 0x100);
if($this->fontFile === null)
$this->fontFile = dirname(__FILE__) . '/Duality.ttf'; //默认的字体文件,Yii框架自带的
$length = strlen($code);
$box = imagettfbbox(30,0,$this->fontFile,$code);
$w = $box[4] - $box[0] + $this->offset * ($length - 1);
$h = $box[1] - $box[5];
$scale = min(($this->width - $this->padding * 2) / $w,($this->height - $this->padding * 2) / $h);
$x = 10;
$y = round($this->height * 27 / 40);
for($i = 0; $i < $length; ++$i)
{
$fontSize = (int)(rand(26,32) * $scale * 0.8);
$angle = rand(-10,10);
$letter = $code[$i];
$box = imagettftext($image,$fontSize,$angle,$x,$y,$foreColor,$this->fontFile,$letter);
$x = $box[2] + $this->offset;
}
imagecolordeallocate($image,$foreColor);
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
}
}本文链接:https://www.360us.net/article/6.html