Reputation: 1943
I would like to know a reliable way to loop through a product's gallery images and take out the base image by checking if they're the same.
I'm customizing the media template of the product page (catalog/product/view/media.phtml
), and I want to put all the gallery images together with the base image in an image-selector, without the base image being displayed twice (which would happen if it wasn't "excluded" in the backend).
I've learned from the original template that the gallery images are displayed as follows:
<?php foreach ($this->getMediaGallery() as $_image): ?>
<img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile()) ?>" />
<?php endforeach; ?>
and the base image of the product is displayed as follows:
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'image') ?>" />
I researched the origins of the media gallery and found that it is an array of Varien_Object
s, which all have a so-called "value ID" that identifies the gallery images. I don't know what the scope of this ID is, and with that, its usefulness. The base image of the product is returned as an Mage_Product_Helper_Image
, through which I can't seem to get any ID or ID-like property (not even through the product image model, which I exposed by rewriting the helper class).
I also tried to match them by file name. It worked for a few days, because the gallery images' are stored in an attribute of the Varien_Object (file
) which produced something like this:
/8/2/8280.jpg
The __toString()
method of the helper class at first returned the file name in the format above, but after a while it started returning a url of a cached image:
http://domain.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/8/2/8280.jpg
Not reliable.
If anyone know a much better way retrieve a product's images, while knowing which one is the base image, being able to prevent the same image to be displayed twice, then please tell me.
EDIT:
Clarification:
Let's say Product X has 5 associated images, conveniently called:
The gallery images collection would include all the images that aren't explicitly "excluded" from the gallery. In our example the gallery images collection would include: B, T, N and P. These are the images which I want to display in the image-selector, but I always want the base image included, regardless of the fact that it may be excluded from the gallery.
If I can't compare the gallery images to the base image I would get something like this: B, T, N, P, B. The base image is displayed twice.
There are two ways to prevent this from happening:
My guess is, the solution lies in comparing the images to each other, by means of an ID or some ID-like property.
Upvotes: 1
Views: 2181
Reputation: 3846
I needed to do something similar, and ended up doing it like this.
First, just before the initial image (which isn't within the gallery foreach loop) add the following:
$base = basename($_product->getData('image'))
then, immediately within the foreach loop add this:
$image = basename($this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile()))
and then place the new images within another foreach loop:
if ($base != $image):
This compares the image names, without the rest of the url included.
Upvotes: 1
Reputation: 2850
My solution was to search for the image name. I had the same issue because I wanted to draw a border around the thumb image and show that was selected at the time. So, for the product image name:
<?php $product_image_name = substr($image_url, strrpos($image_url, '/') + 1); ?>
Then, for each image on the gallery:
foreach ($this->getGalleryImages() as $_image){
$image_name = substr($_image->getUrl(), strrpos($_image->getUrl(), '/') + 1);
if ($product_image_name == $image_name) // Do stuff here
}
Hope this helps.
Upvotes: 0