8

Hello All
I want to change the layout of my captcha on contact form. I need to decrease the amount of lines and dots it contains so, a user can see the text more clearly.
Does anyone have any idea how to do it?
Thanks in advance.

1

6 Answers 6

11

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.

8

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

3

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;
1
  • Thanks Dinesh for sharing this. It will be better if you add a new question and post this answer as the answer. It will be helpful for those who are looking for it. Mar 7, 2017 at 5:20
2

The above will not work for Magento 2.3.4. There are minor changes in the code and file path. You can read below to reduce the noise level for captcha images.

The functions to reduce the noise setDotNoiseLevel and setLineNoiseLevel are same but the code is moved in following file.

vendor/zendframework/zend-captcha/src/Image.php

You can either change the following code in core file.

protected $dotNoiseLevel = 10; 
protected $lineNoiseLevel = 1;

OR you can rewrite the file in local, that is the most accurate and suggested way.

  1. Add the code in di.xml

<preference for="Magento\Captcha\Model\DefaultModel" type="CodeMasterz\Utility\Model\Captcha\DefaultModel"/>

  1. Create a file at app/code/CodeMasterz/Utility/Model/Captcha/DefaultModel.php and add below code in it.
<?php

namespace CodeMasterz\Utility\Model\Captcha;

class DefaultModel extends \Magento\Captcha\Model\DefaultModel 
{


    public function __construct(

    \Magento\Framework\Session\SessionManagerInterface $session,
    \Magento\Captcha\Helper\Data $captchaData,
    \Magento\Captcha\Model\ResourceModel\LogFactory $resLogFactory,
    $formId
    ) 
    {
        parent::__construct($session,$captchaData,$resLogFactory,$formId);

        
        $this->setDotNoiseLevel(10);    //  S:VA
        $this->setLineNoiseLevel(0); 
    } 
}
  1. Run the following commands.
bin/magento cache:clean layout
bin/magento c:c                       
bin/magento c:f
rm -rf generated/*
0

In latest version of magento2 it seems moved to vendor/laminas/laminas-captcha/src\Image.php file. You need to edit the protected $dotNoiseLevel = 100; change it to your required number

/**
 * Number of noise lines on image
 * Used twice - before and after transform
 *
 * @var int
 */
protected $lineNoiseLevel = 10; change it to your required number

Or you can use below code to extension core functionality remove lines and dots. in you custom extension di.xml file add below code

and Create file in Rackron\AdvancedContact\Model\ with name defaultModel.php.

setDotNoiseLevel(0); // S:VA $this->setLineNoiseLevel(0); } } Of course you have to set path of your custom extension rather than Rackron\AdvancedContact\Model can be Vendor\YourExtension\Model . It works for me, hope can help other as well facing same issue.
0

In 2.4.x you will find this file under /vendor/laminas/laminas-captcha/src/

Filename : Image.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.