Customize magento native captcha appearance. Change amount of lines and dots

The way you did answer above is not a good approach.

The class Zend_Captcha_Image has provided functions to change the variables. You can find the functions in the same class which will be something like this:

public function setDotNoiseLevel ($dotNoiseLevel)
{
    $this->_dotNoiseLevel = $dotNoiseLevel;
    return $this;
}
/**
 * @param int $lineNoiseLevel
 */
public function setLineNoiseLevel ($lineNoiseLevel)
{
    $this->_lineNoiseLevel = $lineNoiseLevel;
    return $this;
}

And also Zend_Captcha_Image is extended to a Mage model class i.e Mage_Captcha_Model_Zend. So, you can easily override this Mage model class to set those variables.

In Mage_Captcha_Model_Zend:

public function __construct($params)
{
    if (!isset($params['formId'])) {
        throw new Exception('formId is mandatory');
    }
    $this->_formId = $params['formId'];
    $this->setExpiration($this->getTimeout());

    $this->setDotNoiseLevel(10);     // Added code
    $this->setLineNoiseLevel(0);     // Added code
}

I am setting those variables in constructor so that the changes will work even for page load and also for captcha refresh.

It will be better if you override the above function instead of modifying mage core files.


You can change the captcha noise using below code.

Go to : lib/Zend/Captcha/Image.php

Change below variable values as per your requirements

protected $_dotNoiseLevel = 10; // Increase the value if you want to increase amount of dots
protected $_lineNoiseLevel = 0; // Increase the value if you want to increase amount of lines

Reference : http://magentoforall.blogspot.com.au/2012/11/magento-change-captcha-background-lines.html


For Magento 2 : Go to vendor\magento\zendframework1\library\Zend\Captcha\Image.php

You will find below functions in this file which can be used to customize captcha image.

     /**
     * Set dot noise level
     *
     * @param int $dotNoiseLevel
     * @return Zend_Captcha_Image
     */
    public function setDotNoiseLevel ($dotNoiseLevel)
    {
        $this->_dotNoiseLevel = $dotNoiseLevel;
        return $this;
    }

    /**
     * Set line noise level
     *
     * @param int $lineNoiseLevel
     * @return Zend_Captcha_Image
     */
    public function setLineNoiseLevel ($lineNoiseLevel)
    {
        $this->_lineNoiseLevel = $lineNoiseLevel;
        return $this;
    }

you can change the values of this function from line number 122 and 129 .

/**
 * Number of noise dots on image
 * Used twice - before and after transform
 *
 * @var int
 */
protected $_dotNoiseLevel = 100;
/**
 * Number of noise lines on image
 * Used twice - before and after transform
 *
 * @var int
 */
protected $_lineNoiseLevel = 5;