Get the height of product images within Magento

Get the height of product images within Magento

August 23, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get the height of product images within Magento

Getting Image Heights for Magento Product Galleries

In Magento’s catalog/product/view/media.phtml file, you can easily retrieve the original width of your product images using the core catalog image helper. However, when dealing with multiple gallery images within a foreach loop, things get a bit more complex.

Retrieving Original Width for Primary Image

To get the original width of the first product image (the primary image), use the following code:

<?php $imageWidth = $this->helper('catalog/image')->init($_product, 'image')->getOriginalWidth(); ?>

This initializes the catalog image helper with your product and ‘image’ as the attribute, then calls getOriginalWidth() to retrieve the width.

Unfortunately, this method doesn’t work seamlessly for additional gallery images in a foreach loop. You might be tempted to use something like:

<?php if (count($this->getGalleryImages()) > 1): ?>
    <?php foreach ($this->getGalleryImages() as $_image): ?>
        <?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile())->getOriginalWidth(); ?>
    <?php endforeach; ?>
<?php endif; ?>

However, this will not provide the expected results due to Magento’s handling of gallery images.

The Solution: Using Image Model

To accurately retrieve image heights for all your product gallery images, you need to use the Mage_Catalog_Model_Product_Gallery model. Here’s how:

<?php if ($this->hasGalleries()): ?>
    <?php $galleries = $this->getGalleryImages() ?>
    <?php foreach ($galleries as $_image): ?>
        <?php $image = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGallery()->getImages();
            $imageWidth = $image[$_image['file']]['image']; // This is the width
            $imageHeight = $image[$_image['file']]['height']; // And this is the height
        ?>
    <?php endforeach; ?>
<?php endif; ?>

In this example, hasGalleries() checks if there are any gallery images attached to the product. We then retrieve all gallery images using $this->getGalleryImages().

Next, we load the product and get its media gallery collection. This gives us access to the array of image data.

Finally, we access the width ($_image['file']) and height ('height') from this image data array for each image in our foreach loop.

Conclusion

Retrieving original heights (and widths) for all product images, including those in gallery collections, requires a bit more effort than simply using Magento’s core catalog image helper. By leveraging the Mage_Catalog_Model_Product_Gallery model and the getMediaGallery()->getImages() method, you can accurately retrieve these dimensions.

Remember to always check if there are any galleries attached before attempting to access them to avoid potential errors. This ensures your code remains robust and maintainable.

For more in-depth Magento development guides and best practices, be sure to check out our Magento Development category at Laravel Company.

Catalog Themes

Our network