Hello Everyone,
In this blog, we will learn about how to Add Image Upload Field in system.xml Configuration in Magento 2.
Without wasting your time, let us guide you straight away. Follow the easy step given below to Add Image Upload Field in Configuration file in Magento 2.
STEPS FOR ADD IMAGE UPLOAD FIELD IN SYSTEM CONFIGURATION IN MAGENTO 2
Step 1: Create system.xml file
app/code/Vendor/Extension/etc/adminhtml/system.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<tab id="magecurious" class="magecurious" translate="label" sortOrder="10">
<label>Magecurious</label>
</tab>
<section id="extension" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<class>separator-top</class>
<label>Magecurious</label>
<tab>magecurious</tab>
<resource>Vendor_Extension::config_extension</resource>
<group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>General Configuration</label>
<field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable Extension</label>
<source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
</field>
<field id="vendor_image" translate="label" type="image" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Upload Image</label>
<backend_model>Vendor\Extension\Model\Config\Backend\Image</backend_model>
<base_url type="media">foldername</base_url>
</field>
</group>
</section>
</system>
</config>
Step 2: Create Image.php file
app/code/Vendor/Extension/Model/Config/Backend/Image.php
<?php
namespace Vendor\Extension\Model\Config\Backend;
class Image extends \Magento\Config\Model\Config\Backend\Image
{
const UPLOAD_DIR = 'yourfolder';
/**
* 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));
}
protected function _addWhetherScopeInfo()
{
return true;
}
/**
* Getter for allowed extensions of uploaded files.
*
* @return string[]
*/
protected function _getAllowedExtensions()
{
return ['jpg', 'jpeg', 'gif', 'png', 'svg'];
}
protected function getTmpFileName()
{
$tmpName = null;
if (isset($_FILES['groups']))
{
$tmpName = $_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value'];
}
else
{
$tmpName = is_array($this->getValue()) ? $this->getValue()['tmp_name'] : null;
}
return $tmpName;
}
public function beforeSave()
{
$value = $this->getValue();
$deleteFlag = is_array($value) && !empty($value['delete']);
if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value))
{
$fileTmpName = $this->getTmpFileName();
if ($this->getOldValue() && ($fileTmpName || $deleteFlag))
{
$this->_mediaDirectory->delete(self::UPLOAD_DIR . '/' . $this->getOldValue());
}
}
return parent::beforeSave();
}
private function isTmpFileAvailable($value)
{
return is_array($value) && isset($value[0]['tmp_name']);
}
private function getUploadedImageName($value)
{
if (is_array($value) && isset($value[0]['name']))
{
return $value[0]['name'];
}
return '';
}
}
Step 3: Finally run the below commands
$ php bin/magento cache:clean
$ php bin/magento cache:flush
Step 4: Output:

Final Thoughts:
So this was the easiest way which we have told you in this blog. This is how you can Add Image Upload Field in system.xml configuration file in Magento 2. Hope you liked the blog.
So quickly go to the comment box and tell me how you like this blog?
Stay tuned with us on our site to get new updates of Magento.
Thanks for reading and visiting our site.

Pretty! Thiss was an incredibly wonderful article.
Thanks for supplying this information.
Also visit my webpage; Telegra.Ph