3

How can I pull the product image absolute path in a product?

This returns the product along with the URL:

Mage::helper('catalog/image')->init($_item, 'small_image')->resize(160, 128)

But I needed to access this the _newFile from this object:

    object(Mage_Catalog_Helper_Image)#3877 (14) {
  ["_model":protected] => object(Mage_Catalog_Model_Product_Image)#3876 (34) {
    ["_width":protected] => int(160)
    ["_height":protected] => int(128)
    ["_quality":protected] => int(90)
    ["_keepAspectRatio":protected] => bool(true)
    ["_keepFrame":protected] => bool(true)
    ["_keepTransparency":protected] => bool(true)
    ["_constrainOnly":protected] => bool(false)
    ["_backgroundColor":protected] => array(3) {
      [0] => int(255)
      [1] => int(255)
      [2] => int(255)
    }
    ["_baseFile":protected] => string(90) "/file/path/media/catalog/product/V/V/123.jpg"
    ["_isBaseFilePlaceholder":protected] => bool(false)
    ["_newFile":protected] => string(151) "/file/path/media/catalog/product/cache/6/small_image/160x128/9df78eab33525d08d6e5fb8d27136e95/V/V/VV123.jpg"

Thank you!

2
  • try this <?php echo Mage::helper('catalog/image')->getModel()->getNewFile() ?> Aug 28, 2014 at 4:00
  • Unfortunately it didn't do the trick. Aug 28, 2014 at 5:16

2 Answers 2

5

_newFile is a member of the class Mage_Catalog_Model_Product_Image that is a protected member of the Mage_Catalog_Helper_Image ($_model).
Unfortunately there is no public method to access the _model member of the helper class.
Why? I have no idea. From my point of view you should be able to access it.

I see 2 options here.
Option 1
Rewrite the Mage_Catalog_Model_Product_Image with one of your classes and add this method in your class.

public function getModel() {
    return $this->_getModel()
}

Here is a tutorial on how to rewrite a class..

After that you would be able to access what you need like this:

$image = Mage::helper('catalog/image')->init($_item, 'small_image')->resize(160, 128);
$newFile = $image->getModel()->getNewFile();

Option 2
Get the url of the new image and replace the base url with the base path.

$image = Mage::helper('catalog/image')->init($_item, 'small_image')->resize(160, 128);
$imageUrl = $image->__toString();
$newFile = str_replace(
    array(Mage::getBaseUrl('media'), '/'),
    array(Mage::getBaseDir('media').DS, DS),
    $imageUrl
);

This seams to work, but it may not work if you use a CDN.

I would go with the first option.

2
  • Was asking the same thing why they made it protected. Aug 29, 2014 at 5:34
  • @user1240207. I don't have an answer for that. Maybe someone thought you don't need it outside the class. Maybe you need the helper only for resizing images.
    – Marius
    Aug 29, 2014 at 5:46
0

I have news for y'all. In Magento 1.9.1 there is a function called getNewFile that will return the value of _newFile. Its located at: line 356 of ./app/code/core/Mage/Catalog/Model/Product/Image.php in class Mage_Catalog_Model_Product_Image

Your Answer

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

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