2012-02-14 73 views
0

我嘗試設置一個新的magento產品圖像,例如che_3.png用:Magento - 如何以編程方式設置媒體庫圖片?

$visibility = array (
    'thumbnail', 
    'small_image', 
    'image' 
); 
$product->addImageToMediaGallery($file, $visibility, true, false); 

產物圖像從

臨時目錄移動:/media/tmp/catalog/product/upload/sessionid/original/che_3.png

目標目錄:/media/catalog/product/c/h/che_3.png

現在,如果我想調用圖像文件路徑 - 由magento保存 - 是錯誤的。該media_gallery對象看起來是這樣的:

[media_gallery] => Array 
     (
      [images] => Array 
       (
        [0] => Array 
         (
          [value_id] => 89 
          [file] => /c/h/che_3_2_1.png //che_3.svg is already in here 
          [label] => 
          [position] => 1 
          [disabled] => 0 
          [label_default] => 
          [position_default] => 1 
          [disabled_default] => 0 
         ) 

        [1] => Array // why this second item? 
         (
          [value_id] => 88 
          [file] => /c/h/che_3_2.png 
          [label] => 
          [position] => 1 
          [disabled] => 0 
          [label_default] => 
          [position_default] => 1 
          [disabled_default] => 0 
         ) 

       ) 

      [values] => Array 
       (
       ) 

     ) 

正如你所看到的文件名是由Magento的延長,也有兩個項目被添加到media_gallery。

我的問題是我如何重置產品分別內部計數器,所以magento使用原始文件,因爲它是?

在此先感謝。

+0

最好的選擇,第二個可能是縮略圖,你檢查過的圖像尺寸是否一樣? – 2012-02-14 19:24:02

回答

2

問題解決:我用$product->save()保存了兩次產品,看起來$product->addImageToMediaGallery()也執行了兩次。

相關問題