0

I've got pictures of some products in the folder:

media/catalog/product/cache/2/small_image/168x168/9df78eab33525d08d6e5fb8d27136e95

But actually not all of them. That size would exactly fit for a site I try to generate (where I'd like to include product pictures).

How can I create images in that size in this folder of all products that I've got in the shop?

How have those been created that are already there?

And how long do the images stay there? Is it safe to link there from a product catalogue?

3
  • Did you manage to resolve the issue Chris? Mar 31, 2014 at 18:23
  • Yes and no .. I used a workaround with a subtheme and a category. How the images in 168x168 folders are created, I don't know yet but the theme solution is just doing fine for me.
    – Chris
    Apr 1, 2014 at 0:20
  • To close the question please add your workaround as an answer and accept it Apr 1, 2014 at 7:02

2 Answers 2

1

Whenever you call the catalog/image helper on a product and use the resize method the images are written to a cache path.

echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(168, 168);

This roughly translates to the path you've discribed in your question.

media/catalog/product/cache/2/small_image/168x168/9df78eab33525d08d6e5fb8d27136e95

  • media directory
  • catalog directory
  • it's a product
  • it's in the cache
  • 2 is the store ID
  • small image is the type
  • 168x168 are the width and height
  • the hash is an md5 of a sting obtained from concatenating some image settings

Images are kept as long as you don't flush the image cache from the backend. So basically you can upload any size image you like in the backend, the helper will create resized and cached versions of them on the frontend. No need for any manual actions from your end.

1
  • To add on this...the number (2 in this case) is the store id, and the hash is an md5 of a sting obtained from concatenating some image settings (_keepAspectRatio, _keepFrame, _keepTransparency and others). Check the method Mage_Catalog_Model_Product_Image::setBaseFile almost at the end of it.
    – Marius
    Mar 27, 2014 at 8:06
0

you can resize images dynamically in magento using class

Varien_Image

I've attached a piece of code that I've used for one of my modules

public function resizeImage($width='100',$height='null',$imageUrl){


if($height=='null'):    /*for square images, setting equal height and width*/
    $height=$width;
else:
    $height=$height;
endif;

$folder_path=Mage::getBaseDir('media').DS.$this->_folder_name.DS.$this->_cat_id.'x'.$width.'x'.$height;
$imageName = substr(strrchr($imageUrl,"/"),1);

//$imageResized=Mage::getBaseUrl('media').$this->_folder_name.DS.$this->_cat_id.'x'.$width.'x'.$height.DS.$imageName;
$imageResized=$folder_path.DS.$imageName;

if(!is_dir($folder_path)||!file_exists($imageResized)):

    if(!is_dir($folder_path)):
    $this->createdirectory($folder_path);
    endif;
 $dirImg = Mage::getBaseDir().str_replace("/",DS,strstr($imageUrl,'/media'));

    if (!file_exists($imageResized)&&file_exists($dirImg)) :
        $imageObj = new Varien_Image($dirImg);
        $imageObj->constrainOnly(TRUE);
        $imageObj->keepAspectRatio(TRUE);
        $imageObj->keepFrame(FALSE);
        $imageObj->resize($width, $height);
            $imageObj->save($imageResized);
    endif;
$imageUrl=Mage::getBaseUrl('media').$this->_folder_name.DS.$this->_cat_id.'x'.$width.'x'.$height.DS.$imageName;
else:
$imageUrl=Mage::getBaseUrl('media').$this->_folder_name.DS.$this->_cat_id.'x'.$width.'x'.$height.DS.$imageName;
endif;
return $imageUrl;
}

public function createdirectory($folder_path){

$dir= new Varien_Io_File;
$dir->mkdir($folder_path);

}  

replace

  $this->_folder_name
  $this->_cat_id 

with your folder names and category ids

your question implies that the images are auto generated, so even if you delete them, they will be generated again.

1
  • add a comment if you're having issues Mar 27, 2014 at 6:48

Your Answer

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

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