Also habe ich einen Fehler mit Magento gemeldet, der eine "Lösung" enthielt, die die Probleme mit der Speichernutzung beim Import von Bildern beheben sollte.
Die Lösung finden Sie auf github unter https://github.com/sitewards/import_image_memory_leak_fix, aber die Grundidee ist.
Behebung des Problems Mage_Catalog_Helper_Image::validateUploadFile
, die destruct
Methode tatsächlich auf dem Bildprozessor aufzurufen . Leider scheint es, dass die Standardeinstellung Varien_Image
nicht mit einer übereinstimmt, so dass destruct
wir unsere eigene Klasse hinzufügen mussten, die dies tut.
<?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.');
}
}
}
Und dann eine Umschreibung des Helfers.
<?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>
Und die neue Funktion ruft die neue zerstörbare Bildklasse auf.
<?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;
}
}