diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 9dfa64c..b79fb0a 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -67,13 +67,6 @@ class GDToolkit extends ImageToolkitBase { protected $streamWrapperManager; /** - * An array storing last memory check results. - * - * @var array[] - */ - protected $memoryInfo; - - /** * Constructs a GDToolkit object. * * @param array $configuration @@ -193,11 +186,11 @@ protected function load() { } // Check if enough memory is available. - if (!$this->checkAvailableMemory($this->getWidth(), $this->getHeight())) { + $memory = $this->memoryCheck($this->getWidth(), $this->getHeight()); + if (!empty($memory)) { // If not, it is not possible to load the GD resource. Mark the image as // invalid, to avoid useless attempts to manipulate it. $this->preLoadInfo = NULL; - $memory = $this->getMemoryInfo(); $this->logger->error("Not enough memory (required: %req_size %req_unit, free: %free_size %free_unit, total: %tot_size %tot_unit) to load image '%file'", [ '%req_size' => $memory['required']['size'], '%req_unit' => $memory['required']['unit'], @@ -488,42 +481,31 @@ protected static function supportedTypes() { * http://php.net/manual/en/function.imagecreatetruecolor.php#99623. * Defaults to 1.7. * - * @return bool - * TRUE if there is sufficient memory to allow the operation, FALSE - * otherwise. + * @return array[] + * An empty array if the memory check is successful. If not enough memory + * is available, an associative array with the following keys: + * - 'required': information about required memory. + * - 'free': information about unused memory, free for PHP to allocate. + * - 'total': information about total memory available for PHP. * * @see http://php.net/manual/en/function.imagecreatetruecolor.php#99623 */ - public function checkAvailableMemory($width, $height, $bits_per_pixel = 31, $tweak_factor = 1.7) { + public function memoryCheck($width, $height, $bits_per_pixel = 31, $tweak_factor = 1.7) { // Bytes per pixel need to accommodate enough bytes to store all the bits // needed. $bytes_per_pixel = ceil($bits_per_pixel / 8); $required = (int) ($width * $height * $bytes_per_pixel * $tweak_factor); $memory = new EnvironmentMemory(); if ($memory->checkAvailable($required)) { - $this->memoryInfo = []; - return TRUE; + return []; } else { - $this->memoryInfo = [ + return [ 'required' => Bytes::toUnitAndSize($required), 'free' => Bytes::toUnitAndSize($memory->getFree()), 'total' => Bytes::toUnitAndSize($memory->getTotal()), ]; - return FALSE; } } - /** - * Get last memory check information. - * - * @return array[] - * An associative array with the following keys: - * - 'required': information about required memory. - * - 'free': information about unused memory, free for PHP to allocate. - * - 'total': information about total memory available for PHP. - */ - public function getMemoryInfo() { - return $this->memoryInfo; - } } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php index da651e5..7bcabca 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php @@ -85,8 +85,8 @@ protected function validateArguments(array $arguments) { */ protected function execute(array $arguments) { // Check if enough memory is available. - if (!$this->getToolkit()->checkAvailableMemory($arguments['width'], $arguments['height'])) { - $memory = $this->getToolkit()->getMemoryInfo(); + $memory = $this->getToolkit()->memoryCheck($arguments['width'], $arguments['height']); + if (!empty($memory)) { throw new \RuntimeException("Not enough memory (required: {$memory['required']['size']} {$memory['required']['unit']}, free: {$memory['free']['size']} {$memory['free']['unit']}, total: {$memory['total']['size']} {$memory['total']['unit']}) to perform the image '{$this->getPluginId()}' operation"); } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php index 4f33332..a9ffc9f 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php @@ -102,8 +102,8 @@ protected function execute(array $arguments) { // Check that enough memory is available. $rect = new Rectangle($this->getToolkit()->getWidth(), $this->getToolkit()->getHeight()); $rect = $rect->rotate($arguments['degrees']); - if (!$this->getToolkit()->checkAvailableMemory($rect->getBoundingWidth(), $rect->getBoundingHeight())) { - $memory = $this->getToolkit()->getMemoryInfo(); + $memory = $this->getToolkit()->memoryCheck($rect->getBoundingWidth(), $rect->getBoundingHeight()); + if (!empty($memory)) { throw new \RuntimeException("Not enough memory (required: {$memory['required']['size']} {$memory['required']['unit']}, free: {$memory['free']['size']} {$memory['free']['unit']}, total: {$memory['total']['size']} {$memory['total']['unit']}) to perform the image '{$this->getPluginId()}' operation"); } diff --git a/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php index 23edaee5..e66cde4 100644 --- a/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php +++ b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php @@ -48,6 +48,24 @@ class GdToolkitTest extends UnitTestCase { protected $toolkitOperation; /** + * Pass result for GDToolkit::memoryCheck, for mocking. + * + * @var array + */ + protected $memoryCheckPass = []; + + /** + * Fail result for GDToolkit::memoryCheck, for mocking. + * + * @var array + */ + protected $memoryCheckFail = [ + 'free' => ['unit' => 'MB', 'size' => 10], + 'required' => ['unit' => 'MB', 'size' => 20], + 'total' => ['unit' => 'MB', 'size' => 20], + ]; + + /** * @inheritdoc */ protected function setUp() { @@ -109,10 +127,10 @@ public function applyMockedOperation($operation, array $arguments = []) { * @covers ::load */ public function testLoad() { - $this->setToolkitMock(['checkAvailableMemory']); + $this->setToolkitMock(['memoryCheck']); $this->toolkit->expects($this->any()) - ->method('checkAvailableMemory') - ->will($this->returnValue(TRUE)); + ->method('memoryCheck') + ->will($this->returnValue($this->memoryCheckPass)); $this->image = new Image($this->toolkit, $this->source); $this->assertTrue(is_resource($this->image->getToolkit()->getResource())); @@ -127,10 +145,10 @@ public function testLoad() { * @covers ::load */ public function testNotEnoughMemoryOnLoad() { - $this->setToolkitMock(['checkAvailableMemory']); + $this->setToolkitMock(['memoryCheck']); $this->toolkit->expects($this->any()) - ->method('checkAvailableMemory') - ->will($this->returnValue(FALSE)); + ->method('memoryCheck') + ->will($this->returnValue($this->memoryCheckFail)); // Getting image from file validates the Image object, since GD toolkit is // lazy loading the resource. @@ -152,11 +170,11 @@ public function testNotEnoughMemoryOnLoad() { * @expectedException \RuntimeException */ public function testNotEnoughMemoryOnCreateNew() { - $this->setToolkitMock(['checkAvailableMemory', 'getToolkitOperation', 'apply']); + $this->setToolkitMock(['memoryCheck', 'getToolkitOperation', 'apply']); $this->setToolkitOperationMock('CreateNew', $this->toolkit); $this->toolkit->expects($this->any()) - ->method('checkAvailableMemory') - ->will($this->onConsecutiveCalls(TRUE, FALSE)); + ->method('memoryCheck') + ->will($this->onConsecutiveCalls($this->memoryCheckPass, $this->memoryCheckFail)); $this->toolkit->expects($this->any()) ->method('getToolkitOperation') ->will($this->returnValue($this->toolkitOperation)); @@ -181,11 +199,11 @@ public function testNotEnoughMemoryOnCreateNew() { * @expectedException \RuntimeException */ public function testNotEnoughMemoryOnRotate() { - $this->setToolkitMock(['checkAvailableMemory', 'getToolkitOperation', 'apply']); + $this->setToolkitMock(['memoryCheck', 'getToolkitOperation', 'apply']); $this->setToolkitOperationMock('Rotate', $this->toolkit); $this->toolkit->expects($this->any()) - ->method('checkAvailableMemory') - ->will($this->onConsecutiveCalls(TRUE, FALSE)); + ->method('memoryCheck') + ->will($this->onConsecutiveCalls($this->memoryCheckPass, $this->memoryCheckFail)); $this->toolkit->expects($this->any()) ->method('getToolkitOperation') ->will($this->returnValue($this->toolkitOperation));