Magento:ImageCdn bug?(很长的故事)

iva*_*dja 6 magento

我有一些与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')); ?>

结果:

Array
(
    [0] => 200
    [1] => 200
    [2] => 6
    [3] => width="200" height="200"
    [bits] => 24
    [mime] => image/x-ms-bmp
)
Run Code Online (Sandbox Code Playgroud)

当然我对结果感到惊讶,因为当我使用它Preview(在Mac OSX上) 打开它时看起来很好看起来不错

  • 然后我用十六进制编辑器打开它,前两个字节是:BM这是BMP的标识符
  • 我试图上传.bmp图片的产品 - >失败,无法选择图片
  • 我让我的同事上传它(在Ubuntu上),他能够将文件类型的选择更改为"任何文件".当他单击"上载文件"时,显示的错误消息表明不允许该类型的文件.
  • 我想到的是:管理员试图上传.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)

万一你已经忘记了这个问题:有人知道那段代码的目的是什么?我不明白他们上面评论的含义是什么.它真的是一个错误,还是我错误的解释?

Set*_*aki 0

不,这不是一个错误。这只是对旧版 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,我不确定,但我确定通常的做法是总是编写这个魔术方法,以便在您打算重写一些希望使用这种类型的类中使用它数据投射。