2

I need to get images by its position and I need each image resized to a concrete heigh and width.

If I want a image by its position I can do it by this code:

$_product
    ->getMediaGalleryImages()
    ->getItemByColumnValue('position', 1)
    ->getUrl();

//get image at position 1

And I know how to resize an image with this code:

echo $product
        ->helper('catalog/image')
        ->init($_product, 'image')
        ->constrainOnly(true)
        ->keepAspectRatio(true)
        ->resize(900, 400);

But this code resizes the image set as image (or thumbnail, or small_image) but I don't know how to resize the image set at certain position (for example 2).

How can I merge both codes to resize the image at a certain position?

1

I found a solution, this works, at least in the catalog/product/view templates:

<?php
    $_imgSize = 630; 
    $_imgSizeHeight = 945;
?>
<?php $i=0; foreach ($this->getGalleryImages() as $_image): ?>
    <?php if ($this->isGalleryImageVisible($_image)): ?>
        // at 'position default' set the number of the position you need
        <?php if($_image['position_default'] == 1): ?>  
            <a class="product-view-gallery" data-fancybox="gallery" data-caption="<?php echo $_product->getName(); ?>" href="<?php echo $this->getGalleryImageUrl($_image); ?>"><img id="image-<?php echo $i; ?>" class="gallery-image" src="<?php 

                echo $this
                        ->helper('catalog/image')
                        ->init(
                            $this->getProduct(), 
                            'image', 
                            $_image->getFile()
                        )
                        ->constrainOnly(true)
                        ->keepAspectRatio(true)
                        ->keepFrame(false)
                        ->resize($_imgSize, $_imgSizeHeight);

             ?>" data-zoom-image="<?php echo $this->getGalleryImageUrl($_image); ?>" /></a>
        <?php endif; ?>
    <?php endif; ?>
<?php $i++; endforeach; ?>

Maybe it helps somebody in the future

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.