1

I'm trying to build mobile image gallery for product using Magento resize function.

Everything works fine - code displays 4 pictures (there is 4 pictures for this product in backend), but all 4 displayed pictures are last image of a product.

Example:

In admin I have

Image 1 Image 2 Image 3 Image 4

Result I get is:

Image 4 Image 4 Image 4 Image 4

Result I want to achieve should be:

Image 1 Image 2 Image 3 Image 4

Any pointers would be more than appreciate.

Please see code below:

<?php
$product_id = $this->getProduct()->getId();
$product = Mage::getModel('catalog/product')->load($product_id);//product id here
$galeries = array();


foreach($product->getMediaGalleryImages() as $image){


    $image_path = $image->getPath();    
        if(!file_exists($image_path)) continue;
        $galeries[$image->getLabel()]['images'][]['url']=$image->url;
        $correctURL = $this->helper('catalog/image')->init($this->getProduct(), 'small_image', $image->getFile())->resize(750,750);
            if( $image->associated_attributes != ''){

                $idForSlider = explode('-',$image->associated_attributes);
                $idForSlider = $idForSlider[1];
                $idForSlider = 'swatches_option_value_'.$idForSlider;   
                $galeries[$image->getLabel()]['idForSlider']=$idForSlider;

            }

}

    foreach ($galeries as $galery){
        $idForOwl = $n;
        $n++;

        ?>
        <div class="v-slider<?php echo ($idForOwl+1)?>">
        <div id="owl-demo<?php echo ($idForOwl+1)?>" class="owl-carousel" style="">

        <?php

        foreach ($galery['images'] as $image) {
            ?>

            <div class="item">                                
            <img src="<?php echo $correctURL?>" width="750" height="750"> 
            </div>
            <?php 
         } 

         ?>
         </div>
         </div> 
    <?php
    }
    ?>  

1 Answer 1

1

You are looping through your product images and re-setting $correctURL on each iteration. You are then entering another loop and using the same variable, it will only ever use the last value you stored. You should store the value in your $galleries array during your first loop and use that instead.

2
  • Hi, thanks for your help. I stored $correctURL in $galleries array as $galeries[$image->getLabel()]['images'][]['url2']= Mage::helper('catalog/image')->init($this->getProduct(), 'small_image', $image->getFile())->resize(750,750); But unfortunately still getting previous results. Any help will be much appreciated, cheers. Commented Jul 14, 2015 at 15:18
  • Adding (string) before Mage::helper did the trick! Thanks Andrew! Commented Jul 14, 2015 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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