简体   繁体   中英

Custom Image Attribute in Magento generates error when not set any image and called from product view page

I have created a custom image attribute and added it to default attribute set in Magento. My new attributes are img_support and img_upgrade . I've displayed both images on product view page using this piece of code:

<?php if ($_product->getimg_upgrade()):
 $_img = '<img src="'.$this->helper('catalog/image')->init($_product, 'img_upgrade')->resize(85).'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'"  "/>';
 echo $_helper->productAttribute($_product, $_img, 'img_upgrade');
 endif; 
?>

Same code with the second custom image attribute name will display img_support image. When I set images and select them for these two attributes they'll display fine but when there is no image uploaded and selected for these attributes and I try to show it on product view page then it generates an error. Something like this:

a:5:{i:0;s:25:"Image file was not found.";i:1;s:3911:"#0 C:\Program Files\EasyPHP-    5.3.8.0\www\mymagentoroot\app\code\core\Mage\Catalog\Helper\Image.php(163):
Mage_Catalog_Model_Product_Image->setBaseFile('no_selection').....

And a long trace after that. So basically it fetches the image even though there is no image selected. Can anyone tell me how can I display my custom images in product view page of selected products? I have, say 10 products with default attribute set. So all of these 10 products CAN be set with img_support and img_upgrade but I want to show the custom images only on 5 selected products. If the images are set display. Otherwise don't. How can i do this? If there is any other proper or better way to do this then help me out here. Thanks!

When a product has no image for a given attribute, Magento uses a placeholder image instead.
Basically, each image attribute must have a placeholder image in the skin directory, named with the corresponding attribute code.
In your case, you should create two new placeholder images for your two new attributes img_support and img_upgrade , in this skin directory : images/catalog/product/placeholder/your_attribute_code.jpg (either with a new image, or with a copy of one of the base placeholders)

This problem also happens if you upload image for that attribute then suddenly remove image file via FTP. It is not good to show the site down. To summarize, there are 3 ways to fix this: 1) Answer of @blmage: use place holder 2) Use try catch:

<?php 
try {
if ($_product->getimg_upgrade()):
 $_img = '<img src="'.$this->helper('catalog/image')->init($_product, 'img_upgrade')->resize(85).'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'"  "/>';
 echo $_helper->productAttribute($_product, $_img, 'img_upgrade');
 endif; 
}
catch (Exception $e) {

}
?> 
  1. Check if the file is existed

    getBaseMediaPath(); $baseFile = $baseDir . $_product->getImgUpgrade(); if (file_exists($baseFile)) { } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM