开发者

How to get a product's image in Magento?

I'm running on version 1.3.2.1, but on my client's server they had Magento 1.3.0 so my previous code to display images for my local copy,

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

, does not work on the client's installation.

Looking at the results returned by their Magento, version 1.3.0 actually returns a URL although it points to the skin's media folder.

Is there a way to get the absolute image p开发者_Python百科ath for the image?

Or should I make changes somewhere else that would tell Magento that the media directory should be on the root?


echo $_product->getImageUrl();

This method of the Product class should do the trick for you.


You can try to replace $this-> by Mage:: in some cases. You need to convert to string.

In my case i'm using DirectResize extension (direct link), so my code is like this:

(string)Mage::helper('catalog/image')->init($_product, 'image')->directResize(150,150,3)

The ratio options (3rd param) are :

  • none proportional. The image will be resized at the Width and Height values.
  • proportional, based on the Width value 2
  • proportional, based on the Height value 3
  • proportional for the new image can fit in the Width and the Height values. 4
  • proportional. The new image will cover an area with the Width and the Height values.

Update: other info and versions here


The common way, without plugin would be:

(string)Mage::helper('catalog/image')->init($_product, 'image')->resize(150)

You can replace 'image' with 'small_image' or 'thumbnail'.


I recently needed to do this as well... here's how I got to it:

$_product->getMediaGalleryImages()->getItemByColumnValue('label', 'LABEL_NAME')->getUrl();

Hope that helps you!


Here is the way I've found to load all image data for all products in a collection. I am not sure at the moment why its needed to switch from Mage::getModel to Mage::helper and reload the product, but it must be done. I've reverse engineered this code from the magento image soap api, so I'm pretty sure its correct.

I have it set to load products with a vendor code equal to '39' but you could change that to any attribute, or just load all the products, or load whatever collection you want (including the collections in the phtml files showing products currently on the screen!)

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addFieldToFilter(array(
    array('attribute'=>'vendor_code','eq'=>'39'),
));

$collection->addAttributeToSelect('*');

foreach ($collection as $product) {

    $prod = Mage::helper('catalog/product')->getProduct($product->getId(), null, null);

    $attributes = $prod->getTypeInstance(true)->getSetAttributes($prod);

    $galleryData = $prod->getData('media_gallery');

    foreach ($galleryData['images'] as &$image) {
        var_dump($image);
    }

}


First You need to verify the base, small and thumbnail image are selected in Magento admin.

admin->catalog->manage product->product->image

Then select your image roles(base,small,thumbnail)

How to get a product's image in Magento?

Then you call the image using

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

Hope this helps you.


<img src='.$this->helper('catalog/image')->init($product, 'small_image')->resize(225, 225).' width=\'225\' height=\'225\'/>


// Let's load the category Model and grab the product collection of that category

$product_collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();

// Now let's loop through the product collection and print the ID of every product 
foreach($product_collection as $product) {
  // Get the product ID

$product_id = $product->getId();

  // Load the full product model based on the product ID

$full_product = Mage::getModel('catalog/product')->load($product_id);

  // Now that we loaded the full product model, let's access all of it's data

  // Let's get the Product Name

  $product_name = $full_product->getName();

  // Let's get the Product URL path

  $product_url = $full_product->getProductUrl();

  // Let's get the Product Image URL

  $product_image_url = $full_product->getImageUrl();

  // Let's print the product information we gathered and continue onto the next one

 echo $product_name;

  echo $product_image_url;


}


You need set image type :small_image or image

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


If you have the product collection object like:

$collection = Mage::getModel('catalog/product')->getCollection(); 

Now you can get product sku using $_product->getSku() if you can't get image path with

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

Or

$_product->getImageUrl();

Then you can add a little code

$productModel = Mage::getModel('catalog/product');
$_prod = $productModel->loadByAttribute('sku', $_product->getSku()); 
$_prod->getImageUrl();


$model = Mage::getModel('catalog/product'); //getting product model
$_products = $model->getCollection(); //getting product object for particular product id
foreach($_products as $_product) { ?>
    <a href = '<?php echo $model->load($_product->getData("entity_id"))->getUrl_path(); ?>'> <img src= '<?php echo $model->load($_product->getData("entity_id"))->getImageUrl();  ?>' width="75px" height="75px"/></a>
     <?php echo "<br/>".$model->load($_product->getData("entity_id"))->getPrice()."<br/>". $model->load($_product->getData("entity_id"))->getSpecial_price()."<br/>".$model->load($_product->getData("entity_id"))->getName()?>
<?php 


get product images in magento using product id

    $product_id = $_POST['product_id'];
    $storeId    = Mage::app()->getStore()->getId();
    $loadpro    = Mage::getModel('catalog/product')->load($product_id);

    $mediaApi      =  Mage::getModel("catalog/product_attribute_media_api");
    $mediaApiItems = $mediaApi->items($loadpro->getId());
    foreach ($mediaApiItems as $item) {
            //for getting existing Images
            echo $item['file'];
            }


(string)Mage::helper('catalog/image')->init($product, 'image');

this will give you image url, even if image hosted on CDN.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜