diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 574e03e..99817d1 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -185,19 +185,19 @@ protected function load() { return FALSE; } - // Check if there are memory limitations to load the image. - $unavailable_memory = $this->unavailableMemoryCheck($this->getWidth(), $this->getHeight()); - if ($unavailable_memory) { - // If yes, it is not possible to load the GD resource. Mark the image as + // Check if enough memory is available. + $memory = $this->isMemoryAvailable($this->getWidth(), $this->getHeight()); + if ($memory !== TRUE) { + // 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; $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' => $unavailable_memory['required']['size'], - '%req_unit' => $unavailable_memory['required']['unit'], - '%free_size' => $unavailable_memory['free']['size'], - '%free_unit' => $unavailable_memory['free']['unit'], - '%tot_size' => $unavailable_memory['total']['size'], - '%tot_unit' => $unavailable_memory['total']['unit'], + '%req_size' => $memory['required']['size'], + '%req_unit' => $memory['required']['unit'], + '%free_size' => $memory['free']['size'], + '%free_unit' => $memory['free']['unit'], + '%tot_size' => $memory['total']['size'], + '%tot_unit' => $memory['total']['unit'], '%file' => $this->getSource() ]); return FALSE; @@ -460,7 +460,7 @@ protected static function supportedTypes() { } /** - * Ensures that there is enough memory available for a GD operation. + * Checks that there is enough memory available for a GD operation. * * GD functions that create new GD resources will fail fatally if there is * not enough memory available to perform the operation. This method checks @@ -481,23 +481,23 @@ protected static function supportedTypes() { * http://php.net/manual/en/function.imagecreatetruecolor.php#99623. * Defaults to 1.7. * - * @return array[] - * An empty array if the memory is available. If not enough memory is - * available, an associative array with the following keys: + * @return TRUE|array[] + * TRUE if the memory is available. 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 unavailableMemoryCheck($width, $height, $bits_per_pixel = 31, $tweak_factor = 1.7) { + public function isMemoryAvailable($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)) { - return []; + return TRUE; } else { return [ 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 df9b397..d394c2b 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php @@ -84,10 +84,10 @@ protected function validateArguments(array $arguments) { * {@inheritdoc} */ protected function execute(array $arguments) { - // Check if there are memory limitations to perform the operation. - $unavailable_memory = $this->getToolkit()->unavailableMemoryCheck($arguments['width'], $arguments['height']); - if ($unavailable_memory) { - throw new \RuntimeException("Not enough memory (required: {$unavailable_memory['required']['size']} {$unavailable_memory['required']['unit']}, free: {$unavailable_memory['free']['size']} {$unavailable_memory['free']['unit']}, total: {$unavailable_memory['total']['size']} {$unavailable_memory['total']['unit']}) to perform the image '{$this->getPluginId()}' operation"); + // Check if enough memory is available. + $memory = $this->getToolkit()->isMemoryAvailable($arguments['width'], $arguments['height']); + if ($memory !== TRUE) { + 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"); } // Get the image type. 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 5df5f10..8437974 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php @@ -99,12 +99,12 @@ protected function execute(array $arguments) { return FALSE; } - // Check if there are memory limitations to perform the operation. + // Check if enough memory is available. $rect = new Rectangle($this->getToolkit()->getWidth(), $this->getToolkit()->getHeight()); $rect = $rect->rotate($arguments['degrees']); - $unavailable_memory = $this->getToolkit()->unavailableMemoryCheck($rect->getBoundingWidth(), $rect->getBoundingHeight()); - if ($unavailable_memory) { - throw new \RuntimeException("Not enough memory (required: {$unavailable_memory['required']['size']} {$unavailable_memory['required']['unit']}, free: {$unavailable_memory['free']['size']} {$unavailable_memory['free']['unit']}, total: {$unavailable_memory['total']['size']} {$unavailable_memory['total']['unit']}) to perform the image '{$this->getPluginId()}' operation"); + $memory = $this->getToolkit()->isMemoryAvailable($arguments['width'], $arguments['height']); + if ($memory !== TRUE) { + 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"); } // Stores the original GD resource. 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 20708fe..86a21a4 100644 --- a/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php +++ b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php @@ -48,18 +48,11 @@ class GdToolkitTest extends UnitTestCase { protected $toolkitOperation; /** - * Result for GDToolkit::unavailableMemoryCheck, for mocking. + * Result for GDToolkit::isMemoryAvailable, for mocking. * * @var array */ - protected $unavailableMemoryFalse = []; - - /** - * Result for GDToolkit::unavailableMemoryCheck, for mocking. - * - * @var array - */ - protected $unavailableMemoryTrue = [ + protected $availableMemoryFalse = [ 'free' => ['unit' => 'MB', 'size' => 10], 'required' => ['unit' => 'MB', 'size' => 20], 'total' => ['unit' => 'MB', 'size' => 20], @@ -127,10 +120,10 @@ public function applyMockedOperation($operation, array $arguments = []) { * @covers ::load */ public function testLoad() { - $this->setToolkitMock(['unavailableMemoryCheck']); + $this->setToolkitMock(['isMemoryAvailable']); $this->toolkit->expects($this->any()) - ->method('unavailableMemoryCheck') - ->will($this->returnValue($this->unavailableMemoryFalse)); + ->method('isMemoryAvailable') + ->will($this->returnValue(TRUE); $this->image = new Image($this->toolkit, $this->source); $this->assertTrue(is_resource($this->image->getToolkit()->getResource())); @@ -145,10 +138,10 @@ public function testLoad() { * @covers ::load */ public function testNotEnoughMemoryOnLoad() { - $this->setToolkitMock(['unavailableMemoryCheck']); + $this->setToolkitMock(['isMemoryAvailable']); $this->toolkit->expects($this->any()) - ->method('unavailableMemoryCheck') - ->will($this->returnValue($this->unavailableMemoryTrue)); + ->method('isMemoryAvailable') + ->will($this->returnValue($this->availableMemoryFalse)); // Getting image from file validates the Image object, since GD toolkit is // lazy loading the resource. @@ -170,11 +163,11 @@ public function testNotEnoughMemoryOnLoad() { * @expectedException \RuntimeException */ public function testNotEnoughMemoryOnCreateNew() { - $this->setToolkitMock(['unavailableMemoryCheck', 'getToolkitOperation', 'apply']); + $this->setToolkitMock(['isMemoryAvailable', 'getToolkitOperation', 'apply']); $this->setToolkitOperationMock('CreateNew', $this->toolkit); $this->toolkit->expects($this->any()) - ->method('unavailableMemoryCheck') - ->will($this->onConsecutiveCalls($this->unavailableMemoryFalse, $this->unavailableMemoryTrue)); + ->method('isMemoryAvailable') + ->will($this->onConsecutiveCalls(TRUE, $this->availableMemoryFalse)); $this->toolkit->expects($this->any()) ->method('getToolkitOperation') ->will($this->returnValue($this->toolkitOperation)); @@ -199,11 +192,11 @@ public function testNotEnoughMemoryOnCreateNew() { * @expectedException \RuntimeException */ public function testNotEnoughMemoryOnRotate() { - $this->setToolkitMock(['unavailableMemoryCheck', 'getToolkitOperation', 'apply']); + $this->setToolkitMock(['isMemoryAvailable', 'getToolkitOperation', 'apply']); $this->setToolkitOperationMock('Rotate', $this->toolkit); $this->toolkit->expects($this->any()) - ->method('unavailableMemoryCheck') - ->will($this->onConsecutiveCalls($this->unavailableMemoryFalse, $this->unavailableMemoryTrue)); + ->method('isMemoryAvailable') + ->will($this->onConsecutiveCalls(TRUE, $this->availableMemoryFalse)); $this->toolkit->expects($this->any()) ->method('getToolkitOperation') ->will($this->returnValue($this->toolkitOperation));