【问题标题】:How to get a product's image in Magento?如何在 Magento 中获取产品的图像?
【发布时间】:2011-01-29 06:26:45
【问题描述】:

我在 1.3.2.1 版本上运行, 但在我客户的服务器上,他们有 Magento 1.3.0 所以我以前的代码为我的本地副本显示图像,

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

,不适用于客户端安装。

查看 Magento 返回的结果,1.3.0 版实际上返回了一个 URL,尽管它指向皮肤的媒体文件夹。

有没有办法获取图像的绝对图像路径?
或者我应该在其他地方进行更改,告诉 Magento 媒体目录应该在根目录上?

【问题讨论】:

  • 我的错,它不会从皮肤目录加载图像,只是碰巧它会加载默认的 no-image-found(在皮肤的文件夹上),以防找不到产品的图像
  • (string)Mage::helper('catalog/image')->init($product, 'image');

标签: magento


【解决方案1】:
(string)Mage::helper('catalog/image')->init($product, 'image');

这将为您提供图片网址,即使图片托管在 CDN 上。

【讨论】:

  • 以上在 Magento 1.9.3.8 中不再有效,您需要以下之一: (string)Mage::helper('catalog/image')->init($product, small_'image') OR (string)Mage::helper('catalog/image')->init($product, 'detail_image');
【解决方案2】:

使用产品 ID 在 magento 中获取产品图片

    $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'];
            }

【讨论】:

    【解决方案3】:

    在某些情况下,您可以尝试将$this-> 替换为Mage::。你需要转换成字符串。

    就我而言,我使用的是DirectResize extension(direct link),所以我的代码是这样的:

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

    比率选项(第三个参数)是:

    • 不成比例。图像将根据宽度和高度值调整大小。
    • 成比例,基于宽度值 2
    • 成比例,基于高度值 3
    • 新图像的比例可以适合宽度和高度值。 4
    • 成比例。新图像将覆盖一个具有 Width 和 Height 值的区域。

    更新:其他信息和版本here


    没有插件的常用方法是

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

    您可以将“image”替换为“small_image”或“thumbnail”。

    【讨论】:

    • 在您看来,在我的设置中,为什么 'image' 不起作用而 'small_image' 会起作用? 'image' 总是显示标准的 Magento 图像(但在我的产品中,我指定了所有三个图像,基本图像、小图像和拇指图像)。直到几天前,“图像”还在工作,然后突然停止。未进行任何更新或其他后端更改。
    【解决方案4】:
    <img src='.$this->helper('catalog/image')->init($product, 'small_image')->resize(225, 225).' width=\'225\' height=\'225\'/>
    

    【讨论】:

      【解决方案5】:

      我最近也需要这样做...这是我的方法:

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

      希望对你有所帮助!

      【讨论】:

      • 这太完美了。方便在产品视图之外获取特定图像(例如产品横幅)。
      • 酷。奇迹般有效。谢谢。
      【解决方案6】:

      如果你有这样的产品集合对象:

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

      如果您无法使用

      获取图像路径,现在您可以使用 $_product-&gt;getSku() 获取产品 sku
      echo $this->helper('catalog/image')->init($_product,'small_image')->resize(135);
      

      或者

      $_product->getImageUrl();
      

      然后你可以添加一点代码

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

      【讨论】:

        【解决方案7】:

        这是我发现加载集合中所有产品的所有图像数据的方式。我目前不确定为什么需要从 Mage::getModel 切换到 Mage::helper 并重新加载产品,但必须这样做。我已经从 magento image soap api 对这段代码进行了逆向工程,所以我很确定它是正确的。

        我已将其设置为加载供应商代码等于“39”的产品,但您可以将其更改为任何属性,或仅加载所有产品,或加载您想要的任何集合(包括 phtml 文件中显示的集合产品目前在屏幕上!)

        $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);
            }
        
        }
        

        【讨论】:

          【解决方案8】:
          $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 
          

          【讨论】:

            【解决方案9】:
            // 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;
            
            
            }
            

            【讨论】:

              【解决方案10】:

              首先您需要验证在 Magento admin 中选择了基本图像、小图像和缩略图图像。

              管理->目录->管理产品->产品->图片

              然后选择你的图片角色(base,small,thumbnail)

              然后你调用图像使用

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

              希望对你有所帮助。

              【讨论】:

              • resize 不起作用,仍然显示 210 x 210 图像。尝试清除缓存和索引
              【解决方案11】:
              echo $_product->getImageUrl();
              

              Product 类的这个方法应该可以解决问题。

              【讨论】:

              • 别忘了添加这个:->addAttributeToSelect('image')
              • @HaydenThring 你是救世主!
              • @deprecated since 1.1.5
              【解决方案12】:

              你需要设置图片类型:small_image 或 image

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

              【讨论】:

              • 我用那个替换了我的行,但问题仍然存在:(
              • 我在 IE 和 Chrome 中也有同样的问题。但适用于 FF、Safari 和 Opera
              猜你喜欢
              • 2012-08-13
              • 1970-01-01
              • 1970-01-01
              • 2011-12-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-11-11
              • 1970-01-01
              相关资源
              最近更新 更多