Download product images in a zip on click of download button in magento

Download product images in a zip on click of download button in magento

September 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Download product images in a zip on click of download button in magento

Download Product Images as ZIP on Button Click in Magento

In this post, we’ll guide you through creating a download button that zips and downloads all product images from a custom category page.

Current Setup Analysis

Your current setup displays product images using the getMediaGalleryImages() method, which is correct for retrieving all media gallery images associated with the product. The helper Mage::helper('catalog/image')->init($product, 'image', $image->getFile()) then initializes an image object for each image.

The button you’ve added:

<button type="button" style="display:block;margin:0 auto;">Download</button>

will serve as the trigger for the download action. However, it currently doesn’t have any functionality linked to it.

Adding Download Functionality

To achieve this, we’ll use a combination of jQuery for the front-end and Magento’s controller for the back-end processing. Here’s how:

Front-End: Prepare Images for Download

First, wrap your image container with an ID so you can easily select it later.

<div id="product-images" style="float:left; width:100%;">
  <!-- Your existing code -->
</div>

Next, add jQuery to generate a data URI representing the zipped images when the button is clicked. Add this script below your button:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
jQuery(function($) {
  $('#download-button').on('click', function() {
    var zip = new JSZip();
    var folder = zip.folder('images');

    // Add images to the zip
    $('#product-images img').each(function(index, element) {
      var image = new Image();
      image.src = $(element).attr('src');
      var dataURL = image.src;
      if (dataURL.indexOf(',') > -1 && dataURL.indexOf('base64') > -1) {
        folder.file($(this).attr('alt') + '.jpg', dataURL.substring(dataURL.indexOf(',')+1), {base64: true});
      }
    });

    // Generate a download link
    var zipUrl = URL.createObjectURL(zip.generate({type:'blob'}));
    var downloadLink = document.createElement('a');
    downloadLink.href = zipUrl;
    downloadLink.download = 'product-images.zip';
    document.body.appendChild(downloadLink);
    downloadLink.click();
  });
});
</script>

Back-End: Prepare Controller

You’ll need a controller to handle the download request. Create a new controller file in app/code/local/YourNamespace/YourModule/controllers/ProductController.php:

class YourCompany_YourModule_ProductController extends Mage_Core_Controller_Front_Action {
  public function downloadAction() {
    // Prepare your code here

    $this->getResponse()->setHttpResponseCode(200);
    $this->getResponse()->setHeader('Content-Type', 'application/zip');
    $this->getResponse()->setHeader('Content-Disposition', 'attachment; filename="product-images.zip"');

    // Send the zip data
    $this->getResponse()->sendHeaders();
    echo $zipData;
  }
}

Ensure you replace $zipData with your actual zip data, which will be generated by your front-end script.

Conclusion

In this post, we’ve demonstrated how to create a download button that zips and downloads all product images. By combining jQuery for the front-end and Magento’s controller for the back-end processing, you can now offer a seamless user experience on your custom category pages.

Remember to adapt the code snippets provided here to fit your specific project structure and requirements.

For more detailed guides on Magento development, be sure to check out our Magento Development tutorials at Laravel Company.

Catalog Database JavaScript CLI

Our network