With reference to my own question, I have found that we can change density and number of lines and dots in native magento 2 captcha in /vendor/magento/zendframework1/library/Zend/Captcha/Image.php.

Now my question is that how can we override above file class using custom module, because we don't want to make changes directly in core magento files.

Any help would be appreciated.


2 Answers 2


It is not so complicated. First in composer.json file add this line in psr-0 node

"Zend_Captcha": "app/code/"

so it will look like that

"psr-0": {
   "": "app/code/",
   "Zend_Captcha": "app/code/"

Then create directory app/code/Zend/Captcha/ and place there your file Image.php that you want to override.

After that just run composer update.

  • Ok, let me try. Commented Aug 11, 2016 at 8:26
  • If my module does not have composer.json, then is it possible? Commented Aug 11, 2016 at 8:59
  • 1
    After using composer update, it worked. If I want to use it as personal extension, how can I include my composer.json in it? Commented Aug 11, 2016 at 11:37
  • 1
    How to override vendor/magento/zendframework1/library/Zend/Locale/Data/pa.xml, Could you please let me know? Commented Nov 30, 2016 at 11:32
  • 1
    Instead of changing in Magento default composer.json can we also overwrite that?
    – Jackson
    Commented Jan 20, 2017 at 6:21

If you are using magento 2.3.3 or above then there is no directory for captcha Image generation in this path : /vendor/magento/zendframework1/library/Zend/Captcha

For 2.3.3 or above Magento moved captcha generated code in /vendor/zendframework/zend-captcha/src/, so you need to override \Magento\Captcha\Model\DefaultModel that extends \Zend\Captcha\Image like below :

First, you need to add a preference in your custom module's di.xml file.

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

with below code:

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,


you can set setDotNoiseLevel & setLineNoiseLevel as per your requirement. Let me know if you still face the issue.

Your Answer

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

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