Lex
Lex

Reputation: 13

Get the height of product images within Magento

Within Magento's media.phtml file you can get the image height for the first product image with:

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

However this does not work for further product images (within the foreach loop):

<?php if (count($this->getGalleryImages()) > 1): ?>
<?php foreach ($this->getGalleryImages() as $_image): ?>

neither does..

<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile())->getOriginalWidth(); ?>

Anyone have the answer?

Upvotes: 1

Views: 4729

Answers (2)

Lex
Lex

Reputation: 13

An alternative to Jernej's answer is:

$imagelink = $this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile());                        

list($width, $height, $type, $attr) = getimagesize($imagelink);

echo $width; 

Upvotes: 0

Jernej Golja
Jernej Golja

Reputation: 520

Catalog image helper is lacking support for those kind of operations. You will have to initialize your image model and get sizes from there. So:

<?php foreach ($this->getGalleryImages() as $_image): ?>
  <?php $image = new Varien_Image($_image->getPath()); ?>
  <?php echo $image->getOriginalWidth(); ?>
  <?php echo $image->getOriginalHeight(); ?>
<?php endforeach; ?>

Upvotes: 3

Related Questions