Imagickpixel Exception: Unrecognized Color When Using Captcha?

EDIT: For some obscure reason, just changing to another color, worked. I tried that before, but it seems to happen with some specific colors. In this case 0x000000. I later tried 0x111111 and 0x222222, and both worked. I have no clue why 000000 didn’t work, since that color worked perfectly in my machine. More than a fix, it’s a workaround, since I don’t know if this happens with other colors.

END EDIT

I had captcha working just fine in my laptop localhost, but once I moved the application to the server, the captcha stopped working. I directly opened the captcha action, and it’s throwing an exception which I don’t exactly know how to fix. Google isn’t being particularly helpful right now (or I suck at searching)

Happens in line 297 of CCaptchaAction:


$backColor=new ImagickPixel('#'.dechex($this->backColor));

The above is the first line of the renderImageImagick function (github.com/yiisoft/yii/blob/1.1.13/framework/web/widgets/captcha/CCaptchaAction.php)

The server seems to have GD support, and I didn’t change anything when uploading the application to the server.

This is the phpinfo information:

And this is the action in my controller


/**

     * Declares class-based actions.

     */

    public function actions()

    {

        return array(

            // captcha action renders the CAPTCHA image displayed on the contact page

            'captcha'=>array(

                'class'=>'CCaptchaAction',

                'backColor'=>0x000000,

                'foreColor'=>0xFFFFFF,

                'testLimit'=>1

            ),

        );

    }



The captcha being used:


<span class="add-on"><?php echo $form->labelEx($model,'verifyCode'); ?></span>

<?php echo $form->textField($model,'verifyCode'); ?>

<?php echo $form->error($model,'verifyCode'); ?>

And my model has the verifyCode property:


public $verifyCode;//To use in the public registration form

Any clues? EVERYTHING was working perfectly fine in localhost, at least in regards to the captcha.

Maybe you can pad out the generated hexadecimal string?




$backColor=new ImagickPixel('#' . str_pad(dechex($this->backColor), 6, '0', STR_PAD_LEFT));



Or shorter:


$backColor=new ImagickPixel(sprintf('#%06x', $this->backColor));

:P

Show off ::)

I would really rather not change the Yii CCapcthaAction class, unless absolutely necessary. In this case, I’ll have to be happy with the workaround of just using another hex and have users suffer a not 100% black color :lol:

This should be a bug specific to imagick in CCaptchaAction of Yii 1.1.13 … it is already fixed in 1.1.14 RC.

Probably imagick is not installed in your local host, so the captcha is working fine with GD.

If you don’t want to tweak the core code, you can try the backColor of #111111.

[EDIT]

Sorry, #101010 should be better. :D

[EDIT 2]

Or, you can force the captcha action to use GD instead of imagick.

http://www.yiiframework.com/doc/api/1.1/CCaptchaAction#backend-detail

I see. Indeed, I’m using 1.1.13. Thank you for your help, I’ll just keep 0x111111 since it worked just fine, and doesn’t look much different from the black background.

Edit: Just saw your edit. I’ll try changing the backend to GD. Is there any particular reason to preferring the use of Imagick over GD?

Another Edit: Changed backend to ‘gd’ and backColor to my original 0x000000, and everything is working. Thank you very much!

I don’t know why imagick is selected as the default back end. Probably it’s faster or draws more beautiful than GD.