Jadi saya memang meningkatkan bug dengan Magento termasuk "solusi" yang harus berurusan dengan masalah penggunaan memori dalam proses impor gambar.
Solusinya dapat ditemukan di github di bawah https://github.com/sitewards/import_image_memory_leak_fix tetapi ide dasarnya adalah.
Memperbaiki Mage_Catalog_Helper_Image::validateUploadFile
untuk benar-benar memanggil destruct
metode pada prosesor gambar. Sayangnya sepertinya default Varien_Image
tidak berurusan dengan destruct
jadi kami harus menambahkan kelas kami sendiri yang tidak.
<?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.');
}
}
}
Dan kemudian menulis ulang pembantu.
<?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>
Dan fungsi baru memanggil kelas gambar baru yang dapat dirusak.
<?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;
}
}