How to upload images in custom module like in add product in admin of magento?

How to upload images in custom module like in add product in admin of magento?

August 14, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to upload images in custom module like in add product in admin of magento?

Uploading Multiple Images in a Custom Magento Extension: A Developer’s Guide

As an experienced developer working on Magento 1.7, you’re likely looking to add image upload functionality to your custom extension that mirrors the default product image management system. In this guide, we’ll walk through the process of implementing multiple image uploads for your custom module.

Understanding the Default Product Image System in Magento

Before diving into creating your own solution, let’s briefly explore how Magento handles product images out-of-the-box:

  1. Product Image Upload: The default adminhtml interface provides a simple drag-and-drop interface to upload multiple product images.
  2. Image Thumbnails: Magento automatically generates thumbnails for the uploaded images (3 sizes: small, medium, and large).
  3. Database Storage: The image paths are stored in the database (catalog_product_entity_media_gallery table).

Creating a Custom Image Upload Form

To begin, you’ll need to create an upload form within your custom module’s admin interface.

  1. Create the Form Field In your module’s config.xml, define the new field:

    <adminhtml>
        <install>
            <resources>
                <product_image_setup>
                    <setup_module>
                        <module>Your_Module_Name</module>
                    </setup_module>
                </product_image_setup>
            </resources>
        </install>
    </adminhtml>
    
  2. Design the Form Create a new form in your module’s Block/Adminhtml/Your_Module.php file:

    class Your_Company_Adminhtml_Product extends Mage_Adminhtml_Block_Widget_Form
    {
        protected function _prepareForm()
        {
            $form = new Varien_Data_Form(array('id' => 'product_form', 'action' => $this->getData('action'), 'method' => 'post'));
    
            // Add your form fields here...
    
            return parent::_prepareForm();
        }
    }
    

Implementing the Image Upload Logic

Next, you’ll need to handle the actual image upload and storage within your module.

  1. Upload Handler Create a new controller action in Your_Company/Controller/Adminhtml/Product/Image:

    public function imagesAction()
    {
        if ($post = $this->getRequest()->isPost()) {
            try {
                $images = $this->_getImagesFromUpload();
                // Process and store the images here...
                $this->_saveImages($images);
                $this->_getSession()->addSuccess('Images have been saved.');
            } catch (Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
        }
    
        return $this->_redirect('adminhtml/your_module/product/edit', array('id' => $this->getRequest()->getParam('id')));
    }
    
  2. Image Processing You’ll need to process and generate thumbnails for the uploaded images:

    private function _saveImages($images)
    {
        foreach ($images as $image) {
            // Process and store each image...
            $uploadedFile = $this->_processUploadedFile($image);
            $imageName     = $this->_generateImageName();
            Mage::helper('your_module')->storeImage($uploadedFile, $imageName);
        }
    }
    
    private function _getImagesFromUpload()
    {
        return $this->getRequest()->getFiles('images');
    }
    

To make your custom images appear in a similar manner to default Magento product images, you’ll need to integrate your module’s image storage with the existing media gallery system.

  1. Database Table Create a new table similar to catalog_product_entity_media_gallery to store your custom images’ paths and other metadata.

  2. Image Association Update your main entity model (e.g., product) to include a reference to this new image table, allowing you to associate multiple images with each record.

Optimizing Performance

When working with large numbers of images in Magento, it’s crucial to optimize the system:

  • Implement pagination for image listings.
  • Use proper indexing and caching strategies.
  • Consider using third-party extensions like Amasty Image Gallery or Aitoc Advanced Images to enhance the core functionality.

Conclusion

In this guide, we’ve covered the key steps involved in creating a custom image upload system for your Magento extension that closely mirrors the default product image management features. By following these best practices and leveraging Magento’s built-in media gallery system, you can provide a seamless user experience while maintaining optimal performance.

Magento 1 Catalog Database Extensions

Our network