* @since 2.0 */ class Captcha extends Widget { /** * @var string the route of the action that generates the CAPTCHA images. * The action represented by this route must be an action of [[CaptchaAction]]. */ public $captchaAction = 'site/captcha'; /** * @var array HTML attributes to be applied to the rendered image element. */ public $options = array(); /** * Renders the widget. */ public function run() { $this->checkRequirements(); if (!isset($this->options['id'])) { $this->options['id'] = $this->getId(); } $id = $this->options['id']; $options = Json::encode($this->getClientOptions()); $this->view->registerAssetBundle('yii/captcha'); $this->view->registerJs("jQuery('#$id').yiiCaptcha($options);"); $url = Yii::$app->getUrlManager()->createUrl($this->captchaAction, array('v' => uniqid())); echo Html::img($url, $this->options); } /** * Returns the options for the captcha JS widget. * @return array the options */ protected function getClientOptions() { $options = array( 'refreshUrl' => Html::url(array($this->captchaAction, CaptchaAction::REFRESH_GET_VAR => 1)), 'hashKey' => "yiiCaptcha/{$this->captchaAction}", ); return $options; } /** * Checks if there is graphic extension available to generate CAPTCHA images. * This method will check the existence of ImageMagick and GD extensions. * @return string the name of the graphic extension, either "imagick" or "gd". * @throws InvalidConfigException if neither ImageMagick nor GD is installed. */ public static function checkRequirements() { if (extension_loaded('imagick')) { $imagick = new \Imagick(); $imagickFormats = $imagick->queryFormats('PNG'); if (in_array('PNG', $imagickFormats)) { return 'imagick'; } } if (extension_loaded('gd')) { $gdInfo = gd_info(); if (!empty($gdInfo['FreeType Support'])) { return 'gd'; } } throw new InvalidConfigException('GD with FreeType or ImageMagick PHP extensions are required.'); } }