Então, eu levantei um bug com o Magento, incluindo uma "solução" que deveria lidar com os problemas de uso de memória no processo de importação de imagens.
A solução pode ser encontrada no github em https://github.com/sitewards/import_image_memory_leak_fix, mas a idéia básica é.
Corrigindo o Mage_Catalog_Helper_Image::validateUploadFile
para realmente chamar o destruct
método no processador de imagem. Infelizmente, parece que o padrão Varien_Image
não lida com um, destruct
por isso tivemos que adicionar nossa própria classe.
<?php
/**
* @category Sitewards
* @package Sitewards_ImportImageMemoryLeakFix
* @copyright Copyright (c) Sitewards GmbH (http://www.sitewards.com/)
*/
class Sitewards_ImportImageMemoryLeakFix_Model_Destructable_Image extends Varien_Image
{
/**
* Constructor,
* difference from original constructor - we register a destructor here.
*
* @param string $sFileName
* @param Varien_Image_Adapter $oAdapter Default value is GD2
*/
public function __construct($sFileName = null, $oAdapter = Varien_Image_Adapter::ADAPTER_GD2)
{
parent::__construct($sFileName, $oAdapter);
// Initialize shutdown function
register_shutdown_function(array($this, 'destruct'));
}
/**
* Destroy object image on shutdown
*/
public function destruct()
{
$oAdapter = $this->_getAdapter();
if (method_exists($oAdapter, 'destruct')) {
$oAdapter->destruct();
} else {
Mage::log('Image can not be destructed properly, adapter doesn\'t support the method.');
}
}
}
E então uma reescrita do ajudante.
<?xml version="1.0"?>
<config>
<modules>
<Sitewards_ImportImageMemoryLeakFix>
<version>0.1.0</version>
</Sitewards_ImportImageMemoryLeakFix>
</modules>
<global>
<models>
<sitewards_importimagememoryleakfix>
<class>Sitewards_ImportImageMemoryLeakFix_Model</class>
</sitewards_importimagememoryleakfix>
</models>
<helpers>
<catalog>
<rewrite>
<image>Sitewards_ImportImageMemoryLeakFix_Helper_Catalog_Helper_Image</image>
</rewrite>
</catalog>
</helpers>
</global>
</config>
E a nova função chama a nova classe de imagem destrutível.
<?php
/**
* @category Sitewards
* @package Sitewards_ImportImageMemoryLeakFix
* @copyright Copyright (c) Sitewards GmbH (http://www.sitewards.com/)
*/
class Sitewards_ImportImageMemoryLeakFix_Helper_Catalog_Helper_Image extends Mage_Catalog_Helper_Image
{
/**
* Check - is this file an image
*
* Difference from original method - we destroy the image object here,
* i.e. we are not wasting memory, without that fix product import with images
* easily goes over 4Gb on memory with just couple hundreds of products.
*
* @param string $sFilePath
*
* @return bool
* @throws Mage_Core_Exception
*/
public function validateUploadFile($sFilePath) {
if (!getimagesize($sFilePath)) {
Mage::throwException($this->__('Disallowed file type.'));
}
/** @var Sitewards_ImportImageMemoryLeakFix_Model_Destructable_Image $oImageProcessor */
$oImageProcessor = Mage::getModel('sitewards_importimagememoryleakfix/destructable_image', $sFilePath);
$sMimeType = $oImageProcessor->getMimeType();
$oImageProcessor->destruct();
return $sMimeType !== null;
}
}