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

时间:2012-08-21 13:50:23

标签: magento

我有一些与Magento的免费延期相关的问题OnePica ImageCdn

当我上传“损坏图像”时,前端出现损坏的图像。 enter image description here

好的,让我们开始讲故事:

我注意到它是因为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();
}

我的问题是,有人知道该代码的目的是什么? 我不明白他们上面评论的含义是什么。 我认为这是一个错误,因为它总是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
)

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

  • 然后我用十六进制编辑器打开它,前两个字节是:BM这是BMP的标识符
  • 我尝试上传产品的.bmp图片 - &gt;失败,无法选择图像
  • 我要求我的同事上传它(在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');
?>
  • 从该代码中,我知道图片网址是使用:$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;
}
  • $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];
}
  
      
  • 上一个代码中包含了异常:
  •   
Mage_Catalog_Helper_Image
public function __toString()
{
    ...
    } catch( Exception $e ) {
        $url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
    }
    ...
}
   $ url成为:   http://local.m.com/skin/frontend/default/default/images/catalog/product/placeholder/image.jpg

  • 那么,它应该生成占位符图像吗? placeholder (没有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
}

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

1 个答案:

答案 0 :(得分:0)

不,这不是一个错误。它只是旧Magento系统的传统支持。我想知道,你有没有想过窥探早期版本的magento(如内联文档评论引用,&lt; 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;

__toString可能是protectedprivate,我不确定,但我确定通常的做法是始终编码此魔术方法以便使用它在一个类中,你想要重写一些预期使用这种数据转换的东西。