生成数字计算题验证码

@param string $type
@param int $width
@param int $height
@param string $font
@param string $verifyName

@return void
548755
public static function calocVerify($type = 'png', $width = 170, $height = 45, $font = 'tahoma.ttf', $verifyName = 'verifyCode')
    {
        $la = $ba = 0;
        $calcType = mt_rand(1, 3);
        $createNumber = function () use (&$la, &$ba, $calcType) {
            $la = mt_rand(1, 9);
            $ba = mt_rand(1, 9);
        };
        $createNumber();
        if ($calcType == 3) {
            while ($la == $ba) {
                $createNumber();
            }
            if ($la < $ba) {
                $tmp = $la;
                $la = $ba;
                $ba = $tmp;
            }
        }
        $calcTypeArr = [
            1 => $la + $ba,
            2 => $la * $ba,
            3 => $la - $ba
            // 4 => $la / $ba,
        ];
        $randStr = $calcTypeArr[$calcType];
        $randResult = [
            1 => $la . '+' . $ba . '=?',
            2 => $la . '*' . $ba . '=?',
            3 => $la . '-' . $ba . '=?'
            // 4 => $la .'/'. $ba.'='. $randarr[4],
        ];
        $calcResult = $randResult[$calcType];
        $authKey = md5(mt_rand() . microtime());
        Cookie::set($verifyName, $authKey);

        Model::getInstance()->cache()->set($authKey, $randStr, 1800);
        //$width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
        if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
            $im = imagecreatetruecolor($width, $height);
        } else {
            $im = imagecreate($width, $height);
        }
        $r = Array(225, 255, 255, 223);
        $g = Array(225, 236, 237, 255);
        $b = Array(225, 236, 166, 125);
        $key = mt_rand(0, 3);

        $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);    //背景色（随机）
        $borderColor = imagecolorallocate($im, 100, 100, 100);                    //边框色
        imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
        imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
        $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
        // 干扰
        for ($i = 0; $i < 10; $i++) {
            imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);
        }
        for ($i = 0; $i < 25; $i++) {
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);
        }
        for ($i = 0; $i < 5; $i++) {
            //  imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $calcResult{$i}, $stringColor);
            $x = $i === 0 ? 20 : $i * 50;
            $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
            if ($i == 1 || $i == 3 || $i == 4) {
                $fontSize = $calcType == 3 ? 50 : 28;
                if ($i == 1) {
                    imagettftext($im, $fontSize, 0, $x, 35, $stringColor, CML_EXTEND_PATH . DIRECTORY_SEPARATOR . $font, $calcResult[$i]);
                } else {
                    $decNum = $i == 3 ? 30 : 55;
                    imagettftext($im, 25, 0, $x - $decNum, 35, $stringColor, CML_EXTEND_PATH . DIRECTORY_SEPARATOR . $font, $calcResult[$i]);
                }
            } else {
                imagettftext($im, 28, mt_rand(0, 60), $x, 35, $stringColor, CML_EXTEND_PATH . DIRECTORY_SEPARATOR . $font, $calcResult[$i]);
            }
        }
        self::output($im, $type);
    }
1257826
private function realExec($url, $data, $type, $header = [])
    {
        if (!$url || !$type) {
            return false;
        }
        if (!$data) {
            $data = [];
        }
        $header = $this->getHeader($header);
        $type = strtolower($type);

        //提供全局日志id支持.
        if ($this->globalLogId) {
            $url = URLUtil::addParameter($url, [Log::LOG_REQUEST_ID => Log::getGlobalRequestId()]);
        }
        //提供基础验证签名支持
        if ($this->basicAuthFlag) {
            if ($type == 'get') {
                $url = AuthUtil::get($url);
            } else {
                $url = URLUtil::addParameter($url, ['sign' => AuthUtil::get($data)]);
            }
        }
        $this->finalUrl = $url;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_URL, $url);

        switch ($this->postType) {
            case self::POST_FORM_DATA:
                if (!$data || !is_array($data)) {
                    $data = [];
                }
                break;
            case self::POST_FORM_URLENCODED:
                if (!$data || !is_array($data)) {
                    $data = [];
                }
                $data = http_build_query($data);
                $header[] = 'Content-Type: application/x-www-form-urlencoded';
                break;
            case self::POST_JSON:
                if (!$data) {
                    $data = '';
                } else if (is_array($data)) {
                    $data = json_encode($data);
                }
                $header[] = 'Content-Type: application/json';
                break;
            case self::POST_XML:
                $header[] = 'Content-Type: text/xml;charset=utf-8';
                break;
            case self::POST_RAW:
                if (is_array($data)) $data = json_encode($data);
                break;
            case self::POST_AJAX:
                if (is_array($data)) $data = json_encode($data);
                $header[] = 'X-Requested-With: XMLHttpRequest';
                $header[] = 'Accept:application/json, text/javascript';
                $header[] = 'Accept-Language:zh-CN,zh;q=0.8,en;q=0.6';
                $data = urlencode($data);
                break;
        }


        if ($type == 'get') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        } else if ($type == 'post') {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        } else if ($type == 'put') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        } else if ($type == 'delete') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        } else if ($type == 'head') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
        } else {
            return false;
        }

        if ($this->userAgent) {
            curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
        }
        if ($this->cookieFile) {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile);
        }
        if ($header) {
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);


        if ($this->httpFormat) {
            //CURLINFO_HEADER_OUT选项可以拿到请求头信息
            curl_setopt($ch, CURLINFO_HEADER_OUT, true);
            //设置返回输出响应头
            curl_setopt($ch, CURLOPT_HEADER, true);
            $this->responseOriginalResult = curl_exec($ch);
            //获取请求头
            $this->requestHeader = curl_getinfo($ch, CURLINFO_HEADER_OUT);
            $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            //获取响应头(根据头大小去获取响应头信息内容)
            $this->responseHeader = substr($this->responseOriginalResult, 0, $headerSize);
            if ($type == 'get') {
                $this->requestOriginalResult = $this->requestHeader;
            } else {
                $this->requestOriginalResult = $this->requestHeader . http_build_query($data);
            }
            //设置响应原始内容
            $this->result = substr($this->responseOriginalResult, $headerSize);
        } else {
            $this->result = curl_exec($ch);
        }

        $this->curlInfo = curl_getinfo($ch);
        $this->httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $this->errno = curl_errno($ch);
        $this->error = curl_error($ch);
        $this->isSuccess = $this->httpCode == 200;

        return $this->jsonResultFlag ? json_decode($this->result, true) : $this->result;
    }
593155
public static function makeThumb($image, $thumbName, $type = null, $width = 100, $height = 50, $isAutoFix = true)
    {
        is_dir(dirname($thumbName)) || mkdir(dirname($thumbName), 0700, true);
        $imageInfo = self::getImageInfo($image);
        if (!$imageInfo) return false;

        $type = is_null($type) ? strtolower($imageInfo['ext']) : strtolower(($type == 'jpg' ? 'jpeg' : $type));

        if ($isAutoFix) { //根据比例缩放
            $fixScale = min($width  / $imageInfo['width'], $height / $imageInfo['height']);//缩放的比例
            if ($fixScale > 1) {
                //缩略图超过原图大小
                $width = $imageInfo['width'];
                $height = $imageInfo['height'];
            } else {
                $width = $imageInfo['width'] * $fixScale;
                $height = $imageInfo['height'] * $fixScale;
            }
        } else {
            ($width > $imageInfo['width']) && $width = $imageInfo['width'];
            ($height > $imageInfo['height']) && $height = $imageInfo['height'];
        }

        if (!in_array($type, ['jpeg', 'gif', 'png'])) {
            return false;
        }
        $createImageFunc = "imagecreatefrom{$type}";
        $sourceCreateImage = $createImageFunc($image);//载入原图

        //生成缩略图
        if ($type == 'gif' || !function_exists('imagecreatetruecolor')) {
            $thumbImg = imagecreate($width, $height);
        } else {
            $thumbImg = imagecreatetruecolor($width, $height);
        }

        if ($type == 'png') {
            imagealphablending($thumbImg, false);//取消默认的混色模式（为解决阴影为绿色的问题）
            imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息（为解决阴影为绿色的问题）
        } elseif ($type == 'gif') {
            $transparentIndex = imagecolortransparent($sourceCreateImage);
            if ($transparentIndex >= 0) {
                $transparentColor = imagecolorsforindex($sourceCreateImage , $transparentIndex);
                $transparentIndex = imagecolorallocate($thumbImg, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']);
                imagefill($thumbImg, 0, 0, $transparentIndex);
                imagecolortransparent($thumbImg, $transparentIndex);
            }
        }
        //复制
        function_exists('imagecopyresampled') ? imagecopyresampled($thumbImg, $sourceCreateImage, 0, 0, 0, 0, $width, $height, $imageInfo['width'], $imageInfo['height']) : imagecopyresized($thumbImg, $sourceCreateImage, 0, 0, 0, 0, $width, $height, $imageInfo['width'], $imageInfo['height']);
        self::output($thumbImg, $type, $thumbName);
        imagedestroy($sourceCreateImage);
        return $thumbName;
    }
594517
public static function header(string $content, string $type = null)
    {
        if ($type) {
            header($content.': '.$type.'; charset=UTF-8');
        } else {
            header($content);
        }
    }
630560
protected function init($request, $builder, $type = 'builder')
    {
        $this->request    = $request;
        $this->query_type = $type;
        $this->columns    = $builder->columns;
        $this->connection = $builder->getConnection();
        $this->prefix     = $this->connection->getTablePrefix();
        $this->database   = $this->connection->getDriverName();
        if ($this->isDebugging()) {
            $this->connection->enableQueryLog();
        }
    }
951073
public function addCredential($type)
    {
        $this->data_type = $type;

        $this->setTitleEditCredential();
        $this->setBreadcrumbEditCredential();

        $this->credential->callHandler($type, 'edit');
    }
453865
public function setUp(Server $server, $type)
    {
        $server->registerHandler($this->backend->getUserLibraryInstance()->generateTypeHandler());
        if(($user = $this->backend->getUserLibraryInstance()->getUserLoggedIn()) != null){
            $server->registerHandler($user->generateTypeHandler());
        }
        $server->registerHandler($this->backend->getPageOrderInstance()->generateTypeHandler());
        if(($page = $this->backend->getPageOrderInstance()->getCurrentPage()) != null){
            $server->registerHandler($page->generateTypeHandler());
        }
        $server->registerHandler($this->backend->getLoggerInstance()->generateTypeHandler());
        $server->registerHandler($this->backend->getUpdaterInstance()->generateTypeHandler());
        $server->registerHandler($this->backend->getSiteInstance()->generateTypeHandler());
        $server->registerHandler($this->backend->getFileLibraryInstance()->generateTypeHandler());
        $server->registerHandler(new PostGetFilesArrayAccessTypeHandlerImpl());
        $server->registerHandler(new ParserTypeHandlerImpl());




    }
312461
public function thumb($width, $height, $type = self::THUMB_SCALING)
    {
        //原图宽度和高度
        $w = $this->info['width'];
        $h = $this->info['height'];
        /* 计算缩略图生成的必要参数 */
        switch ($type) {
            /* 等比例缩放 */
            case self::THUMB_SCALING:
                //原图尺寸小于缩略图尺寸则不进行缩略
                if ($w < $width && $h < $height) {
                    return $this;
                }
                //计算缩放比例
                $scale = min($width / $w, $height / $h);
                //设置缩略图的坐标及宽度和高度
                $x      = $y      = 0;
                $width  = $w * $scale;
                $height = $h * $scale;
                break;
            /* 居中裁剪 */
            case self::THUMB_CENTER:
                //计算缩放比例
                $scale = max($width / $w, $height / $h);
                //设置缩略图的坐标及宽度和高度
                $w = $width / $scale;
                $h = $height / $scale;
                $x = ($this->info['width'] - $w) / 2;
                $y = ($this->info['height'] - $h) / 2;
                break;
            /* 左上角裁剪 */
            case self::THUMB_NORTHWEST:
                //计算缩放比例
                $scale = max($width / $w, $height / $h);
                //设置缩略图的坐标及宽度和高度
                $x = $y = 0;
                $w = $width / $scale;
                $h = $height / $scale;
                break;
            /* 右下角裁剪 */
            case self::THUMB_SOUTHEAST:
                //计算缩放比例
                $scale = max($width / $w, $height / $h);
                //设置缩略图的坐标及宽度和高度
                $w = $width / $scale;
                $h = $height / $scale;
                $x = $this->info['width'] - $w;
                $y = $this->info['height'] - $h;
                break;
            /* 填充 */
            case self::THUMB_FILLED:
                //计算缩放比例
                if ($w < $width && $h < $height) {
                    $scale = 1;
                } else {
                    $scale = min($width / $w, $height / $h);
                }
                //设置缩略图的坐标及宽度和高度
                $neww = $w * $scale;
                $newh = $h * $scale;
                $x    = $this->info['width'] - $w;
                $y    = $this->info['height'] - $h;
                $posx = ($width - $w * $scale) / 2;
                $posy = ($height - $h * $scale) / 2;
                do {
                    //创建新图像
                    $img = imagecreatetruecolor($width, $height);
                    // 调整默认颜色
                    $color = imagecolorallocate($img, 255, 255, 255);
                    imagefill($img, 0, 0, $color);
                    //裁剪
                    imagecopyresampled($img, $this->im, $posx, $posy, $x, $y, $neww, $newh, $w, $h);
                    imagedestroy($this->im); //销毁原图
                    $this->im = $img;
                } while (!empty($this->gif) && $this->gifNext());
                $this->info['width']  = (int) $width;
                $this->info['height'] = (int) $height;
                return $this;
            /* 固定 */
            case self::THUMB_FIXED:
                $x = $y = 0;
                break;
            default:
                throw new ImageException('不支持的缩略图裁剪类型');
        }
        /* 裁剪图像 */
        return $this->crop($w, $h, $x, $y, $width, $height);
    }
1266151
private function askForCreateSP(string $spType, string $tableName): void
  {
    $question = sprintf('Create SP for <dbo>%s</dbo> ? (default Yes): ', $spType);
    $question = new ConfirmationQuestion($question, true);
    if ($this->helper->ask($this->input, $this->output, $question))
    {
      $defaultSpName = strtolower(sprintf('%s_%s', $tableName, $spType));
      $fileName      = sprintf('%s/%s.psql', $this->sourceDirectory, $defaultSpName);

      $question = new Question(sprintf('Please enter filename (%s): ', $fileName), $fileName);
      $spName   = $this->helper->ask($this->input, $this->output, $question);

      if ($spName!==$fileName)
      {
        $spName   = strtolower($spName);
        $fileName = sprintf('%s/%s.psql', $this->sourceDirectory, $spName);
      }
      else
      {
        $spName = $defaultSpName;
      }

      if (file_exists($fileName))
      {
        $this->io->writeln(sprintf('File <fso>%s</fso> already exists', $fileName));
        $question = 'Overwrite it ? (default No): ';
        $question = new ConfirmationQuestion($question, false);
        if ($this->helper->ask($this->input, $this->output, $question))
        {
          $code = $this->generateSP($tableName, $spType, $spName);
          $this->writeTwoPhases($fileName, $code);
        }
      }
      else
      {
        $code = $this->generateSP($tableName, $spType, $spName);
        $this->writeTwoPhases($fileName, $code);
      }
    }
  }
1256512
public function bindColumn($column, &$param, $type = null)
    {
        if ($type === null) :
            $this->_statement->bindColumn($column, $param);
        else :
            $this->_statement->bindColumn($column, $param, $type);
        endif;
    }
555829
public function addColumn($name, $type, $size = false) {
        return $this->table ? $this->table->column($name, $type, $size) : false;
    }
945835
public static function captcha($length = 4, $mode = 3, $type = 'png', $hasborder = true)
    {
        $randval                   = \ePHP\Misc\Func::randString($length, $mode);
        $_SESSION['imgVerifyCode'] = md5(strtolower($randval));

        $width  = 50;
        $height = 25;

        $width = ($length * 9 + 10) > $width ? $length * 9 + 10 : $width;
        if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
            $im = @imagecreatetruecolor($width, $height);
        } else {
            $im = @imagecreate($width, $height);
        }

        // 背景色
        $backColor = imagecolorallocate($im, 252, 252, 252);

        if ($hasborder) {
            $border_color = 238;
        } else {
            $border_color = 255;
        }

        $borderColor = imagecolorallocate($im, $border_color, $border_color, $border_color); //边框色
        $pointColor  = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //点颜色

        @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
        @imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
        $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));

        // 干扰
        for ($i = 0; $i < 10; $i++) {
            $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
        }

        for ($i = 0; $i < 25; $i++) {
            $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
        }

        @imagestring($im, 5, 5, 3, $randval, $stringColor);

        header("Content-type: image/" . $type);
        $imageFun = 'Image' . $type;
        $imageFun($im);
        imagedestroy($im);
    }
236502
public function add($name, $type = TextType::class, $args = [])
    {
        if ($this->getTypeClass($type) === RepeatedType::class || $type instanceof RepeatedType) {
            return $this->addRepeatedType($name, $type, $args);
        }

        $formControl = $this->getTypeObject($type, $args);

        if (!isset($args['name'])) {
            $formControl->setName($name);
        }

        if ($formControl->getLabel() === null) {
            $formControl->setLabel($formControl->getHumanName());
        }

        $this->getForm()->add($formControl);

        return $this;
    }
1160312
protected function register(
        string $type,
        $name,
        string $source,
        $dependencies,
        $attributes,
        $replaces
    ): void {
        $dependencies = (array) $dependencies;
        $attributes = (array) $attributes;
        $replaces = (array) $replaces;

        if (\is_array($name)) {
            $replaces = \array_merge($name, $replaces);
            $name = '*';
        }

        $this->assets[$type][$name] = [
            'source' => $source,
            'dependencies' => $dependencies,
            'attributes' => $attributes,
            'replaces' => $replaces,
        ];
    }
528757
public function setToken($type = null, int $size = null, bool $specialCharacter = null, $column = null)
    {
        if (! isset($this->DT_settings)) {
            $this->DT_settings = $this->defaultSettings;
        }
        if (! isset($this->DT_Column)) {
            $this->DT_Column = $this->defaultColumn;
        }
        if ($type != null) {
            $this->attributes['DT_settings']['type'] = $type;
        }
        if ($column != null) {
            $this->DT_Column = $column;
        }
        if ($size != null) {
            $this->attributes['DT_settings']['size'] = $size;
        }
        if ($specialCharacter != null) {
            $this->attributes['DT_settings']['special_chr'] = $specialCharacter;
        }
        $table = $this->getTable();
        $token = new Token();
        $generated_token = null;
        switch ($this->DT_settings['type']) {
            case Token::UNIQUE_NUMBER:
                $generated_token = $token->uniqueNumber($table, $this->DT_Column, $this->DT_settings['size'], $this->DT_settings['special_chr']);

                break;
            case Token::UNIQUE_STRING:
                $generated_token = $token->uniqueString($table, $this->DT_Column, $this->DT_settings['size'], $this->DT_settings['special_chr']);

                break;
            case Token::RANDOM:
                $generated_token = $token->random($this->DT_settings['size'], $this->DT_settings['special_chr']);

                break;
            case Token::RANDOM_NUMBER:
                $generated_token = $token->randomNumber($this->DT_settings['size'], $this->DT_settings['special_chr']);

                break;
            case Token::RANDOM_STRING:
                $generated_token = $token->randomString($this->DT_settings['size'], $this->DT_settings['special_chr']);

                break;
            case Token::UNIQUE:
            default:
                $generated_token = $token->unique($table, $this->DT_Column, $this->DT_settings['size'], $this->DT_settings['special_chr']);

                break;
        }

        $this->attributes[$this->DT_Column] = $generated_token;
        unset($this->DT_settings);
    }
510826
public function createAndSubmitForm($type, $data = null, callable $validCallback = null)
    {
        $form = $this->createForm($type, $data);

        if ($this->isPost()) {
            $form->handleRequest($this->getRequest());

            if ($form->isValid()) {
                if ($validCallback) {
                    $response = $validCallback($form->getData(), $form);

                    if($response instanceof Response)

                        return $response;
                }
            }
        }

        return $form->createView();
    }
1048703
public static function create(
        $originalWidth,
        $originalHeight,
        $newWidth,
        $newHeight,
        $type
    ) {
        return new self(
            $originalWidth,
            $originalHeight,
            $newWidth,
            $newHeight,
            $type
        );
    }