Override Zend Library Files Function in Magento 2 – How to Guide

custommagento-2.1.3magento2overrideszend-framework

I would like to override below file

magento\vendor\magento\zendframework1\library\Zend\Captcha\Image.php

Can we do with

magento\app\code\zendframework1\library\Zend\Captcha\Image.php ?

What will be the class extends?

Following http://webkul.com/blog/overriding-rewriting-classes-magento2/

It's giving 90% answer.

magento\app\code\Zend\Captcha\Image.php

class Zend_Captcha_Image extends Zend_Captcha_Word
{
    protected function _generateImage($id, $word)
    {
    }
}

I want only this function. It gives abstract class error. If i only put this.

Can we override composer.json for this? Don't want to change in Magento Default

When u click on "Reload Captcha" it gives big DIV blank then it loads. It's not working as default works. Something still missing in override.

enter code here

Best Answer

The Magento_Captcha module use Zend_Captcha_Image for generate of captcha images.

Magento\Captcha\Model\DefaultModel:

class DefaultModel extends \Zend_Captcha_Image implements \Magento\Captcha\Model\CaptchaInterface

For extending image generation functionality, you can add you custom preference on Magento\Captcha\Model\DefaultModel.


di.xml

<preference for="Magento\Captcha\Model\DefaultModel" type="Vendor\Module\Model\Captcha\MyCaptchaModel"/>

MyCaptchaModel.php:

/**
 * Class MyCaptchaModel.
 */
class MyCaptchaModel extends \Magento\Captcha\Model\DefaultModel
{
    /**
     * {@inheritdoc}
     */
    protected function _generateImage($id, $word)
    {
        //@TODO
    }
}

Alternatively, you can add plugin on \Magento\Captcha\Model\CaptchaFactory::create() and define your custom logic for creating instances of captcha models.

Related Topic