yii\captcha\CaptchaAction::renderImage PHP Method

renderImage() protected method

Renders the CAPTCHA image.
protected renderImage ( string $code ) : string
$code string the verification code
return string image contents
    protected function renderImage($code)
    {
        if (isset($this->imageLibrary)) {
            $imageLibrary = $this->imageLibrary;
        } else {
            $imageLibrary = Captcha::checkRequirements();
        }
        if ($imageLibrary === 'gd') {
            return $this->renderImageByGD($code);
        } elseif ($imageLibrary === 'imagick') {
            return $this->renderImageByImagick($code);
        } else {
            throw new InvalidConfigException("Defined library '{$imageLibrary}' is not supported");
        }
    }

Usage Example

 /**
  * Renders the CAPTCHA image.
  * @param string $code the verification code
  * @return string image contents
  */
 protected function renderImage($code)
 {
     // alphabets ?
     if (!$this->useMbChars) {
         return parent::renderImage($code);
     }
     // font defaults to seto-mini.ttf
     if ($this->mbFontFile === null) {
         $this->mbFontFile = __DIR__ . DIRECTORY_SEPARATOR . 'seto-mini.ttf';
     }
     $encoding = 'UTF-8';
     if (Captcha::checkRequirements() === 'gd') {
         // check if conversion to Shift_JIS is needed
         $gd_info = gd_info();
         if ($gd_info['JIS-mapped Japanese Font Support']) {
             $code = mb_convert_encoding($code, 'SJIS', 'UTF-8');
             $encoding = 'SJIS';
         }
         return $this->mbRenderImageByGD($code, $encoding);
     } else {
         return $this->mbRenderImageByImagick($code, $encoding);
     }
 }