Magento: ImageCdn error? (long story)

I have a question related to the free Magento OnePica ImageCdn extension .

A broken image appears in the frontend when I upload a “damaged image”. enter image description here

Ok, let's start the long story:

I noticed that this was due to the ImageCdn extension and the "damaged image".

In some part of the ImageCdn code:

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(); } 

My question is: does anyone know what the purpose of this code is? I do not understand the meaning of their comments above. I think this is a mistake, because it always return $this->_getModel()->getUrl();

Is this really a mistake or is it just my misinterpretation?

This is what I have done so far:

  • I have a dummy.jpeg image
  • After some investigation, I just realized that it was a “damaged image”.
  • I tested using: <?php print_r(getimagesize('dummy.jpeg')); ?> <?php print_r(getimagesize('dummy.jpeg')); ?>

Result:

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

Of course, I was surprised by the result, because it looks good when I open it using Preview (on Mac OSX) looks good

  • Then I open it with a hex editor, the first two bytes: BM , which is the BMP identifier
  • I tried to load the .bmp image for the product -> failed, cannot select image
  • I asked my colleague to download it (on Ubuntu), he was able to change the choice of file type to "any files". When he clicks "Download Files", an error message is displayed indicating that this type of file is not allowed.
  • Which is for my part: the administrator tried to load the .bmp image and failed. He will then rename it to .jpeg and succeed. Although I do not understand which images can be renamed without showing the logo of the broken image (off topic).
  • These scripts throw an exception, I will break what I have traced.

Code Tracking:

  • application / design / interface / base / default / catalog / product / view / 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'); ?> 
  • From this code, I know that the image URL is generated using: $this->helper('catalog/image')->init($_product, 'image')
  • I did Mage::log((string)$this->helper('catalog/image')->init($_product, 'image'));

Result: 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; } 
  • The error caused by $this->_getModel()->saveFile()->getUrl() . In some part of the code, it will eventually achieve:

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]; } 
  • The exception was fixed in the previous code:
 Mage_Catalog_Helper_Image public function __toString() { ... } catch( Exception $e ) { $url = Mage::getDesign()->getSkinUrl($this->getPlaceholder()); } ... } 

$ url became: http://local.m.com/skin/frontend/default/default/images/catalog/product/placeholder/image.jpg

  • So, should it have generated the placeholder image correctly? placeholder (without extension ImageCdn)
  • No, because

Mage_Catalog_Helper_Image been rewritten by 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 } 

If you all have already forgotten the question: Does anyone know what the purpose of this code is? I do not understand the meaning of their comments above. Is this really a mistake, or is it just my misinterpretation?

+6
source share
1 answer

No, this is not a mistake. This is simply outdated support for older Magento systems. I am wondering, have you ever walked around earlier versions of magento (as links to inline documentation comments, <1.1.3)?

The essence of the question is that before the appearance of Magician 1.1.3, Mage_Catalog_Helper_Image instances arise to create URLs from string broadcasts, for example.

 $image = (some instance of Mage_Catalog_Helper_Image).. ; $imageUrl = (string) $image; 

__toString is probably either protected or private , I’m not sure, but I’m sure that the usual practice is to always code this magic method in order to use it in a class, which makes sense to rewrite something with what expects the use of this kind of data.

0
source

Source: https://habr.com/ru/post/923393/


All Articles