Friday, September 21, 2012

Magento : How to encode product path image


Override following file /app/Local/Mage/Catalog/Helper/Image.php
and add following code
public function base64_encode_image ($filename=string) {
if ($filename) {

$forFiletype = explode('.',$ filename);
$forFiletype = array_reverse($forFiletype);
$filetype = $forFiletype[0];
if(strtoupper($filetype) == 'JPG')
{
$filetype = 'jpeg';
}
$base64image = '';
$changeFilePath = explode(‘cache’,$filename);
$changeFilePath = Mage::getBaseDir(). DS. ‘media’. DS. ‘catalog’.DS.’products’.DS.’cache’.$changeFilePath[1];
$imgbinary = fread(fopen($changeFilePath, “r”), filesize($changeFilePath));
return ‘data:image/’ . $filetype . ‘;base64,’ . base64_encode($imgbinary);
}
}

Now use in /app/design/catalog/product/list.phtml as
$this->helper('catalog/image')->init($_product, 'small_image')->resize(193,250);
$base64image = $this->helper('catalog/image')->base64_encode_image($product_image,$filetype);
<img src="<?php echo $this->getSkinUrl('images/spacer.png'); ?>" width="193" height="250" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" style="background:url(<?php echo $base64image;?>) no-repeat;"/>

No comments:

Post a Comment