<?php
/*
 *author:john
 *date:2019-02-27
 *描述:生成验证码
 **/

namespace app\modules\common;

 use Yii;
 use yii\web\Response;

class Captcha
{
    
    const REFRESH_GET_VAR = 'refresh';

    public $testLimit = 3;

    
    public $width = 120;

    
    public $height = 50;

    
    public $padding = 2;

    public $backColor = 0xFFFFFF;

    
    public $foreColor = 0x2040A0;

    
    public $transparent = false;

    
    public $minLength = 4;

    
    public $maxLength = 4;

    
    public $offset = -2;

    
    public $fontFile ='@app/captcha/SpicyRice.ttf';

    
    public $fixedVerifyCode;
        
    //生成验证码
    public function run($string)
    {

        $redis=Yii::$app->redis;
        //获取图像验证码
        $code=$this->generateVerifyCode();

        //
        $redis->set('String:'.md5($string.'_verify_jld_2233'), $code, "EX", 600);

        $this->setHttpHeaders();
        
        $response = Yii::$app->getResponse();
        
        Yii::$app->response->format = Response::FORMAT_RAW;

        return  $this->ImageByGD($code);
    }
    //绘制验证码
    public function ImageByGD($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
        );

        $length = strlen($code);
        
        $box = imagettfbbox(30, 0, Yii::getAlias($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);
            $angle = rand(-10, 10);
            $letter = $code[$i];
            $box = imagettftext($image, $fontSize, $angle, $x, $y, $foreColor, Yii::getAlias($this->fontFile), $letter);
            $x = $box[2] + $this->offset;
        }

        imagecolordeallocate($image, $foreColor);

        ob_start();
        imagepng($image);
        imagedestroy($image);

        return ob_get_clean();
    }
    
    
    public function generateVerifyCode()
    {
        if ($this->minLength > $this->maxLength) {
            $this->maxLength = $this->minLength;
        }
        if ($this->minLength < 3) {
            $this->minLength = 3;
        }
        if ($this->maxLength > 20) {
            $this->maxLength = 20;
        }
        $length = mt_rand($this->minLength, $this->maxLength);

        $letters = '0123456789';
        $vowels = '0123456789';
        $code = '';
        for ($i = 0; $i < 4; $i++) {
            $code .= $vowels[mt_rand(0, 9)];
        }

        return $code;
    }
    
    public function setHttpHeaders()
    {
        Yii::$app->getResponse()->getHeaders()
            ->set('Pragma', 'public')
            ->set('Expires', '0')
            ->set('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
            ->set('Content-Transfer-Encoding', 'binary')
            ->set('Content-type', 'image/png');
    }
}
