我有一些与Magento的免费扩展OnePica ImageCdn有关的问题.
当我上传"损坏的图像"时,前端会出现损坏的图像.

好吧,让我们开始这个长篇故事:
我注意到它是因为ImageCdn扩展和"腐败图像"而发生的.
在ImageCdn的代码的某些部分:
OnePica_ImageCdn_Helper_Image
/**
* In older versions of Magento (<1.1.3) this method was used to get an image URL.
* However, 1.1.3 now uses the getUrl() method in the product > image model. This code
* was added for backwards compatibility.
*
* @return string
*/
public function __toString()
{
parent::__toString();
return $this->_getModel()->getUrl();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,有谁知道该代码的目的是什么?我不明白他们上面评论的含义是什么.我认为它总是一个错误return $this->_getModel()->getUrl();
是真的是一个错误还是我错误的解释?
这是我到目前为止所做的:
dummy.jpeg<?php print_r(getimagesize('dummy.jpeg')); ?>结果:
Run Code Online (Sandbox Code Playgroud)Array ( [0] => 200 [1] => 200 [2] => 6 [3] => width="200" height="200" [bits] => 24 [mime] => image/x-ms-bmp )
当然我对结果感到惊讶,因为当我使用它Preview(在Mac OSX上)
打开它时看起来很好
BM这是BMP的标识符.bmp图像并失败.然后他将其重命名.jpeg并成功.虽然我没有得到它可以重命名什么样的图像而不显示破损的图像标识(超出主题).跟踪代码:
- 应用程序/设计/前端/碱/默认/目录/产品/视图/ media.phtml
<?php
$_img = '<img id="image" src="'.$this->helper('catalog/image')->init($_product, 'image').'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" />';
echo $_helper->productAttribute($_product, $_img, 'image');
?>
Run Code Online (Sandbox Code Playgroud)
$this->helper('catalog/image')->init($_product, 'image')Mage::log((string)$this->helper('catalog/image')->init($_product, 'image'));结果:
http://local.m.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/d/u/dummy.jpeg
.
- 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());
}
Mage::log('pass');
$url = $this->_getModel()->saveFile()->getUrl();
Mage::log('not pass');
}
} catch( Exception $e ) {
$url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
}
return $url;
}
Run Code Online (Sandbox Code Playgroud)
$this->_getModel()->saveFile()->getUrl().在代码的某些部分,它最终会达到:Varien_Image_Adapter_Gd2
private function _getCallback($callbackType, $fileType = null, $unsupportedText = 'Unsupported image format.')
{
if (null === $fileType) {
$fileType = $this->_fileType;
}
if (empty(self::$_callbacks[$fileType])) {
//reach this line -> exception thrown
throw new Exception($unsupportedText);
}
if (empty(self::$_callbacks[$fileType][$callbackType])) {
throw new Exception('Callback not found.');
}
return self::$_callbacks[$fileType][$callbackType];
}
Run Code Online (Sandbox Code Playgroud)
- 在前面的代码中捕获了异常:
Mage_Catalog_Helper_Image
public function __toString()
{
...
} catch( Exception $e ) {
$url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
}
...
}
Run Code Online (Sandbox Code Playgroud)
$ url成为:
http://local.m.com/skin/frontend/default/default/images/catalog/product/placeholder/image.jpg
(没有ImageCdn扩展名)
Mage_Catalog_Helper_Image改写了OnePica_ImageCdn_Helper_Image
public function __toString()
{
parent::__toString(); //the result is http://local.m.com/skin/frontend/default/default/images/catalog/product/placeholder/image.jpg but no variable store/process its value
return $this->_getModel()->getUrl(); //in the end it will return http://local.m.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/d/u/dummy.jpeg
}
Run Code Online (Sandbox Code Playgroud)
万一你已经忘记了这个问题:有人知道那段代码的目的是什么?我不明白他们上面评论的含义是什么.它真的是一个错误,还是我错误的解释?
不,这不是一个错误。这只是对旧版 Magento 系统的遗留支持。我想知道,您是否曾经抽出时间来窥探 magento 的早期版本(如内嵌文档注释所引用的,< 1.1.3)?
问题的要点是在 Mage 1.1.3 之前,Mage_Catalog_Helper_Image实例碰巧从 to-string 转换产生 URL,例如
$image = (some instance of Mage_Catalog_Helper_Image).. ;
$imageUrl = (string) $image;
Run Code Online (Sandbox Code Playgroud)
__toString可能是protected或者private,我不确定,但我确定通常的做法是总是编写这个魔术方法,以便在您打算重写一些希望使用这种类型的类中使用它数据投射。
| 归档时间: |
|
| 查看次数: |
920 次 |
| 最近记录: |