yii\captcha\Captcha::checkRequirements PHP Method

checkRequirements() public static method

This method will check the existence of ImageMagick and GD extensions.
public static checkRequirements ( ) : string
return string the name of the graphic extension, either "imagick" or "gd".
    public static function checkRequirements()
    {
        if (extension_loaded('imagick')) {
            $imagickFormats = (new \Imagick())->queryFormats('PNG');
            if (in_array('PNG', $imagickFormats, true)) {
                return 'imagick';
            }
        }
        if (extension_loaded('gd')) {
            $gdInfo = gd_info();
            if (!empty($gdInfo['FreeType Support'])) {
                return 'gd';
            }
        }
        throw new InvalidConfigException('Either GD PHP extension with FreeType support or ImageMagick PHP extension with PNG support is required.');
    }

Usage Example

Example #1
0
 /**
  * Renders the CAPTCHA image.
  * @param string $code the verification code
  * @return string image contents
  */
 protected function renderImage($code)
 {
     if (Captcha::checkRequirements() === 'gd') {
         return $this->renderImageByGD($code);
     } else {
         return $this->renderImageByImagick($code);
     }
 }
All Usage Examples Of yii\captcha\Captcha::checkRequirements