Yii2 验证码图像不显示

Yii2 Captcha Image not showing

<?php echo $form->field($model, 'captcha')->widget(Captcha::className(), [
                                              ]); ?> 

通过了 yii2 要求,如 GD 和 imagick liberies,但验证码图像未显示。

您必须在模型中设置 'captcha' 规则。

['captcha', 'captcha', 'captchaAction' => 'site/captcha'] 

第一个验证码是您的 属性,下一个是规则类型

一定要设置控制器

public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }
} else {
        ob_clean();  //Add this line, can solve this problem. Or add in other suitable places
        $this->setHttpHeaders();
        Yii::$app->response->format = Response::FORMAT_RAW;
        return $this->renderImage($this->getVerifyCode());
    }

这会有所帮助, 请更改 \vendor\yiisoft\yii2\captcha\CaptchaAction。php 将显示图片。

} else {
    ob_clean();  //Add this line, can solve this problem. Or add in other suitable places
    $this->setHttpHeaders();
    Yii::$app->response->format = Response::FORMAT_RAW;
    return $this->renderImage($this->getVerifyCode());
}