我最近和圖片一起工作,我相信我對它有很好的理解。
我不認爲Josua所說的是「真正的」正確答案。他的回答可以解決你的問題,但我不能忍受看到誤導性信息。
至於他的第一個選擇,它是「正確的」。
讓我打破代碼:
Mage_Catalog_Model_Product_Media_Config
public function getMediaUrl($file)
{
$file = $this->_prepareFileForUrl($file);
if(substr($file, 0, 1) == '/') {
return $this->getBaseMediaUrl() . $file;
}
return $this->getBaseMediaUrl() . '/' . $file;
}
public function getBaseMediaUrl()
{
return Mage::getBaseUrl('media') . 'catalog/product';
}
protected function _prepareFileForUrl($file)
{
return str_replace(DS, '/', $file);
}
正如你所看到的,它只是簡單地添加media/ + catalog/product + $file
。
$文件是從數據庫中獲取,值會像/e/x/example.jpeg
你上傳的產品圖片存儲這些文件夾內。
現在,爲什麼$product-> getImageUrl()
給你錯誤的URL的問題仍然未知。
的代碼Josua提出了第二個選項:
$this->helper('catalog/image')
->init($product, $type)
->resize(163, 100);
是 「幾乎」 與$product->getImageUrl()
一樣,它只是在resize
Mage_Catalog_Model_Product
public function getImageUrl()
{
return (string)$this->_getImageHelper()->init($this, 'image')->resize(265);
}
所以對於他的區別第二個選項,它會給你的舊代碼相同的結果。 我不知道他爲什麼要提出第二個選項,我覺得他從來沒有檢查什麼是那些功能(不是一個好主意,因爲它會導致錯誤的信息)的後面
當你致電$product->getImageUrl()
,它會嘗試從緩存中加載圖像(如果不存在),它將從數據庫加載圖像(對於路徑,然後將從媒體文件夾中查找正確的圖像)並創建緩存。如果無法找到圖像或發生錯誤,則會獲取佔位符圖像。
我的建議是檢查是否有拋出的異常。您需要使用舊代碼$product->getImageUrl()
。打開app/code/core/Mage/Catalog/Helper/Image.php
然後去:
Mage_Catalog_Helper_Image
public function __toString()
{
try {
if($this->getImageFile()) {
$this->_getModel()->setBaseFile($this->getImageFile());
} else {
$this->_getModel()->setBaseFile($this->getProduct()->getData($this->_getModel()->getDestinationSubdir()));
}
if($this->_getModel()->isCached()) {
return $this->_getModel()->getUrl();
} else {
if($this->_scheduleRotate) {
$this->_getModel()->rotate($this->getAngle());
}
if ($this->_scheduleResize) {
$this->_getModel()->resize();
}
if($this->getWatermark()) {
$this->_getModel()->setWatermark($this->getWatermark());
}
$url = $this->_getModel()->saveFile()->getUrl();
}
} catch(Exception $e) {
//put log to show error message
Mage::log($e->getMessage());
$url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
}
return $url;
}
把Mage::log($e->getMessage());
登錄是否有拋出的異常。調用佔位符圖像很可能是因爲拋出了異常而被調用。
這只是從我一個建議,以確保沒有什麼錯你的圖像/其他事情,其實你已經直接解決您的問題,從media/catalog/product/...
另一個校正Josua的代碼獲取圖像:
注意$ full_product = Mage :: getModel('catalog/product') - > load($ product_id);
這是因爲裏面的foreach($集合作爲$產品),產品對象將被加載,所以產品的另一個負荷不必要完全沒有必要(也PRODUCT_ID $是不確定的)
UPDATE,剛剛殺青的代碼:
public function items($filters = null, $store = null)
{
$collection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter($this->_getStoreId($store))
->addAttributeToSelect(array('name','image'));
//->addAttributeToSelect('name'); add another select, either image/small_image/thumbnail, modify it as you need
if (is_array($filters)) {
try {
foreach ($filters as $field => $value) {
if (isset($this->_filtersMap[$field])) {
$field = $this->_filtersMap[$field];
}
$collection->addFieldToFilter($field, $value);
}
} catch (Mage_Core_Exception $e) {
$this->_fault('filters_invalid', $e->getMessage());
}
}
$result = array();
foreach ($collection as $product) {
//$result[] = $product->getData();
$result[] = array(// Basic product data
'product_id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'set' => $product->getAttributeSetId(),
'type' => $product->getTypeId(),
'category_ids'=> $product->getCategoryIds(),
'url_path' => $product-> getImageUrl() // Added the Method here
);
}
return $result;
}
謝謝Bhai ..它的工作就像一個魅力..我得到每一個URL。 –