Magento – How to get product image by position

imageimage-preview

At the moment I'm using the following code to get the image by position. Is there an easiest way to do that?

<?php 
$imgFile = $imgLabel = "";
$imagens = Mage::getModel('catalog/product')->load($_item->getProductId())->getMediaGalleryImages();
foreach ($imagens as $imagem) {
if($imagem->getPosition() === '9') :
    $imgFile = $imagem->getFile();
    $imgLabel = $imagem->getLabel();
    endif;
}
if(!$imgFile && !$imgLabel):
?>
<img src="<?php echo $this->helper('catalog/image')->init(Mage::getModel('catalog/product')->load($_item->getProductId()), 'small_image')->resize(70,105); ?>" width="70" height="105" alt="<?php echo $this->escapeHtml($this->getProductName()) ?>" />
<?php else : ?>
<img src="<?php echo $this->helper('catalog/image')->init(Mage::getModel('catalog/product')->load($_item->getProductId()), 'small_image', $imgFile)->resize(70, 105); ?>" width="70" height="105" alt="<?php echo $this->htmlEscape($imgLabel);?>" />
<?php endif; ?>

Best Answer

I recently needed to do this as well... here's how I got to it:

$_product->getMediaGalleryImages()->getItemByColumnValue('position_default', 'Your Position')->getUrl();

Hope that helps you!