私は最近画像を扱っていましたが、それについてかなりよく理解していると思います。
ヨスアの言ったことが「本当の」正解だとは思いません。彼の答えがあなたの問題を解決できるのは良いことですが、誤解を招く情報を見るのは我慢できませんでした.
彼の最初の選択肢は「正解」です。
コードを分解してみましょう。
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
。
$file はデータベースから取得され、値は次のようになります/e/x/example.jpeg
アップロードした製品画像は、これらのフォルダー内に保存されます。
さて、なぜ$product-> getImageUrl()
間違ったURLを提供するのかという問題については、まだ不明です。
Josua が 2 番目のオプションとして提案するコード:
$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);
}
したがって、彼の 2 番目のオプションでは、古いコードで同じ結果が得られます。なぜ彼が 2 番目のオプションを提案したのかはわかりません。彼はそれらの機能の背後にあるものを決してチェックしていないと思います (間違った情報につながる可能性があるため、良い考えではありません)。
を呼び出すと$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($collection as $product) 内で製品オブジェクトがロードされるため、これは絶対に不要です。そのため、製品の別のロードは不要です ($product_id も未定義です)。
更新、コードを修正するだけです:
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;
}