Create Image Upload in Configuration Magento 2

Create system.xml

<field id="countdown_product_page" translate="label comment" type="Magento\Config\Block\System\Config\Form\Field\Image" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
      <label>Select Countdown for Homepage</label>
      <source_model>PME\FlashSales\Model\Config\Source\Timer</source_model>
      <frontend_model>PME\FlashSales\Block\Adminhtml\Form\Renderer\Config\Timers</frontend_model>
  </field>

Create Uploader Class.

Vendor\Module\Model\Config\Backend;

 

<?php

namespace PME\FLashSales\Model\Config\Backend;

class Image extends \Magento\Config\Model\Config\Backend\Image
{
    /**
     * The tail part of directory path for uploading
     *
     */
    const UPLOAD_DIR = 'image'; // Folder save image

    /**
     * Return path to directory for upload file
     *
     * @return string
     * @throw \Magento\Framework\Exception\LocalizedException
     */
    protected function _getUploadDir()
    {
        return $this->_mediaDirectory->getAbsolutePath($this->_appendScopeInfo(self::UPLOAD_DIR));
    }

    /**
     * Makes a decision about whether to add info about the scope.
     *
     * @return boolean
     */
    protected function _addWhetherScopeInfo()
    {
        return true;
    }

    /**
     * Getter for allowed extensions of uploaded files.
     *
     * @return string[]
     */
    protected function _getAllowedExtensions()
    {
        return ['jpg', 'jpeg', 'gif', 'png'];
    }
}

 

About Ejaz Alam

A Software Engineer who Believes in a simple lifestyle and follow a simple logic to make himself better than yesterday. :)

View all posts by Ejaz Alam →

Leave a Reply

Your email address will not be published. Required fields are marked *