Changing the Path of Product Images in Magento
Introduction Magento is a powerful e-commerce platform that allows you to manage your online store efficiently. However, sometimes you might want to modify the default image path for your products. In this post, we will guide you through changing the product image path from the standard Magento catalog to a custom cache directory.
Default Image Path in Magento By default, Magento stores and serves product images from the “/media/catalog/product” directory. The images are cached using a hash string that includes the image filename, width, and height. For example:
/media/catalog/product/cache/1/image/350x350/9df78eab33525d08d6e5fb8d27136e95/c/h/chhota-bheem-himalayan-adventure-bow-arrow-_-sword-set.jpg
However, you might face issues with this default setup. The cached images can consume a lot of disk space over time, and the long hash strings in the URL can negatively impact your site’s SEO.
Changing to a Custom Cache Directory To resolve these issues, it is recommended to change the image path to a custom cache directory. This allows you to control the caching process more efficiently.
- Creating a New Directory Structure
First, create a new directory structure for your product images inside the “/media/cache/images” folder. You can organize this directory based on the product category or any other logical hierarchy that suits your needs:
/media/cache/images âââ 1 âââ thumbnail âââ 602f0fa2c1f0d1ba5e241f914e856ff9 âââ catalog âââ product âââ c âââ image-name.jpg
- Modifying the Catalog Image Helper Configuration
Next, you need to modify the configuration of the catalog image helper in your “/app/code/core/Mage/Catalog/Helper/Image.php” file.
Replace the existing code for the “init” method with the following:
public function init($_product, $attribute = 'image', $fileName = null)
{
if (!$_product instanceof Mage_Catalog_Model_Product) {
return false;
}
$productAttributeObject = $_product->getResource()->getAttribute($attribute);
if ($productAttributeObject === false || !$productAttributeObject->usesSource()) {
throw new Exception('Invalid attribute');
}
$imageFileName = trim($fileName);
$imageFileName = $this->_fileStorage->validateUploadDirectory($imageFileName);
if (empty($fileName)) {
$fileName = $_product->getData($attribute);
} else {
$_product->setData($attribute, $imageFileName);
}
$productAttributeObject = Mage::registry('current_product')->getResource()->getAttribute($attribute);
if ($productAttributeObject === false || !$productAttributeObject->usesSource()) {
throw new Exception('Invalid attribute');
}
$productAttributeObject->setValue($_product->getData($attribute));
$sourceModel = $productAttributeObject->getSource();
$options = $sourceModel->getAllOptions(false);
$options = reset($options);
if ($options && $options['value'] != $_product->getData($attribute)) {
throw new Exception('Invalid value');
}
return $this;
}
This modification ensures that the image path is generated correctly based on your custom cache directory structure.
- Updating the Product View Template
Finally, you need to update the product view template file located in “/app/design/frontend/default/your_theme/template/catalog/product/view.phtml”.
Replace the existing code for the “media.phtml” include with the following:
<?php $_product = $this->getProduct(); ?>
<?php $_helper = $this->helper('catalog/output'); ?>
<?php $dexxtz = Mage::helper('productzoom'); ?>
<?php $dexxtz->getCss(); ?>
<?php $dexxtz->getJs(); ?>
<ul id="etalage">
<li>
<img class="etalage_thumb_image" src="<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($_product, 'image')); ?>" />
<img class="etalage_source_image" title="<?php echo $_product->getImageLabel(); ?>" src="<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($_product, 'image'), true); ?>" />
</li>
<?php
foreach ($this->getGalleryImages() as $_image) {
if(Mage::registry('current_product')->getImage() != $_image->getFile()) { ?>
<li>
<img class="etalage_thumb_image" src="<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile())); ?>" />
<img class="etalage_source_image" title="<?php echo $_image->getLabel(); ?>" src="<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()), true); ?>" />
</li>
<?php
}
}
?>
</ul>
Conclusion
In this post, we have demonstrated how to change the product image path in Magento from the default catalog directory to a custom cache directory. This approach provides better control over image caching and improves SEO by reducing long hash strings in the URL.
Remember to clear your Magento cache after making these changes for them to take effect. You can do this by going to System > Cache Management and selecting “Flush Cache Storage”.
If you found this tutorial helpful, consider exploring Laravel Company for more articles on e-commerce development and Magento customization.
Happy coding!