Magento 2 get product gallery images – resized (gallery.phtml)

Gepostet am Februar 21, 2017

Der folgende Beispielcode zeigt, wie man die Galeriebilder von einem Produkt direkt auslesen und anzeigen kann. Die entsprechende Größe der Bilder wird über die Datei „MeinTemplate/etc/view.xml“ definiert.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php $_imagehelper = $this->helper('Magento\Catalog\Helper\Image'); ?>
<?php $product = $block->getProduct() ?>
<?php $images = $product->getMediaGalleryImages(); ?>
<?php
foreach ($images as $image) {
    $image->setData(
        'small_image_url',
        $_imagehelper->init($product, 'product_page_image_small')
            ->setImageFile($image->getFile())
            ->getUrl()
    );
    $image->setData(
        'medium_image_url',
        $_imagehelper->init($product, 'product_page_image_medium')
            ->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)
            ->setImageFile($image->getFile())
            ->getUrl()
    );
    $image->setData(
        'large_image_url',
        $_imagehelper->init($product, 'product_page_image_large')
            ->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)
            ->setImageFile($image->getFile())
            ->getUrl()
    );
    $image->setData(
        'is_default_image',
        ($image->getFile()==$product->getImage()) ? 1 : 0
    );
}
?>
 
<?php foreach ($images as $image): ?>
    <?php $item = $image->getData(); ?>
    <? print_r($item); ?>
    <img src="<?php echo $item['medium_image_url'] ?>" alt="<?php echo ($image->getLabel()!='') ? $image->getLabel() : $product->getName() ?>" />
<?php endforeach ?>

Tags: ,

Leave a Reply

 

Posts of Current Category



Facebook: