Magento 2 -Display a saved image in a image uploader on page load

Magento 2 -Display a saved image in a image uploader on page load

August 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 -Display a saved image in a image uploader on page load

Magento 2 Image Uploader: Displaying Saved Images on Page Load

The Magento 2 image uploader is a powerful tool for managing media files within your e-commerce store. In this guide, we’ll explore how to display an uploaded image in the image uploader preview when you load the form page.

Problem Overview

You’ve successfully implemented an image uploader to handle customer login background and logo images. The upload works flawlessly, saving the images in the media folder. However, when you navigate to the admin uploader page, the saved images are not automatically displayed in the image uploader preview box. You want to mimic the functionality of changing the frontend header logo via the contents configuration.

Understanding the Current Setup

You’ve already configured your UI form and dataprovider as shown above. The form elements include an imageUploader field with settings for allowed extensions, max file size, and uploader configuration. However, the dataprovider currently returns an empty array in the getData method.

Solution Steps: Displaying Saved Images

To achieve this, we need to modify the DataProvider class to fetch the saved images from the media folder. Here’s a step-by-step solution:

  1. Fetch the saved image path Use Magento’s file system helper and the customer login model to retrieve the saved image path from the database. You can find the relevant field name in your customer login model.

  2. Check if the image exists Before trying to retrieve the image, check if it actually exists in the media folder using Magento\Framework\Filesystem. This prevents potential errors when the image is deleted or not found.

  3. Read and return the image data If the image exists, read its contents using a file read stream (fopen) or Magento’s file system helper methods. Return this image data along with other form fields in your getData method.

Here’s an example of how you can modify your DataProvider class:

class DataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
{
    // Existing code...

    public function getData()
    {
        $imagePath = $this->customerLoginModel->getImagePath($customerId);

        if ($imagePath && file_exists($mediaDir . $imagePath)) {
            $imageData = $this->fileSystemHelper->readFile($mediaDir . $imagePath);
        } else {
            $imageData = ''; // or default image data
        }

        return [
            'data' => [
                0 => [
                    'id' => 1,
                    'image_data' => base64_encode($imageData),
                    // other form fields...
                ],
            ],
        ];
    }
}

Remember to replace $customerLoginModel->getImagePath($customerId) with the appropriate method to fetch your image’s path. Also, ensure you have the necessary dependencies and services injected into your DataProvider.

Conclusion

Displaying saved images in Magento 2’s image uploader involves modifying the dataprovider to read and return the image data from the media folder. By following this guide, you can achieve a similar functionality as changing the frontend header logo via contents configuration.

This solution demonstrates how to handle file management and display uploaded files within the Magento admin interface. It’s an essential aspect of building a robust e-commerce platform with Magento 2.

For more advanced customizations and tips on Magento development, feel free to explore https://laravelcompany.com. We regularly publish in-depth guides on Magento 2 best practices, performance optimizations, and custom extension development.

Magento 2 Database Extensions Themes

Our network