diff --git a/core/lib/Drupal/Component/Utility/EnvironmentMemory.php b/core/lib/Drupal/Component/Utility/EnvironmentMemory.php index eef2806..7c22588 100644 --- a/core/lib/Drupal/Component/Utility/EnvironmentMemory.php +++ b/core/lib/Drupal/Component/Utility/EnvironmentMemory.php @@ -58,6 +58,9 @@ public function setTotal($size = NULL) { if (!$size) { $size = ini_get('memory_limit'); } + // ini_get() may return -1 or null for memory_limit to indicate there is no + // limit set. We check that explicitly, and in that case map the result to + // the internal constant static::UNLIMITED. $this->total = (!$size || (int) $size === -1) ? static::UNLIMITED : Bytes::toInt($size); } diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index b79fb0a..574e03e 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 enough memory is available. - $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 + // 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 // 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' => $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'], + '%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'], '%file' => $this->getSource() ]); return FALSE; @@ -482,15 +482,15 @@ protected static function supportedTypes() { * Defaults to 1.7. * * @return array[] - * An empty array if the memory check is successful. If not enough memory - * is available, an associative array with the following keys: + * An empty array 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 memoryCheck($width, $height, $bits_per_pixel = 31, $tweak_factor = 1.7) { + public function unavailableMemoryCheck($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); 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 7bcabca..df9b397 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 enough memory is available. - $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"); + // 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"); } // 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 a9ffc9f..5df5f10 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 that enough memory is available. + // Check if there are memory limitations to perform the operation. $rect = new Rectangle($this->getToolkit()->getWidth(), $this->getToolkit()->getHeight()); $rect = $rect->rotate($arguments['degrees']); - $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"); + $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"); } // 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 e66cde4..20708fe 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,18 @@ class GdToolkitTest extends UnitTestCase { protected $toolkitOperation; /** - * Pass result for GDToolkit::memoryCheck, for mocking. + * Result for GDToolkit::unavailableMemoryCheck, for mocking. * * @var array */ - protected $memoryCheckPass = []; + protected $unavailableMemoryFalse = []; /** - * Fail result for GDToolkit::memoryCheck, for mocking. + * Result for GDToolkit::unavailableMemoryCheck, for mocking. * * @var array */ - protected $memoryCheckFail = [ + protected $unavailableMemoryTrue = [ 'free' => ['unit' => 'MB', 'size' => 10], 'required' => ['unit' => 'MB', 'size' => 20], 'total' => ['unit' => 'MB', 'size' => 20], @@ -127,10 +127,10 @@ public function applyMockedOperation($operation, array $arguments = []) { * @covers ::load */ public function testLoad() { - $this->setToolkitMock(['memoryCheck']); + $this->setToolkitMock(['unavailableMemoryCheck']); $this->toolkit->expects($this->any()) - ->method('memoryCheck') - ->will($this->returnValue($this->memoryCheckPass)); + ->method('unavailableMemoryCheck') + ->will($this->returnValue($this->unavailableMemoryFalse)); $this->image = new Image($this->toolkit, $this->source); $this->assertTrue(is_resource($this->image->getToolkit()->getResource())); @@ -145,10 +145,10 @@ public function testLoad() { * @covers ::load */ public function testNotEnoughMemoryOnLoad() { - $this->setToolkitMock(['memoryCheck']); + $this->setToolkitMock(['unavailableMemoryCheck']); $this->toolkit->expects($this->any()) - ->method('memoryCheck') - ->will($this->returnValue($this->memoryCheckFail)); + ->method('unavailableMemoryCheck') + ->will($this->returnValue($this->unavailableMemoryTrue)); // Getting image from file validates the Image object, since GD toolkit is // lazy loading the resource. @@ -170,11 +170,11 @@ public function testNotEnoughMemoryOnLoad() { * @expectedException \RuntimeException */ public function testNotEnoughMemoryOnCreateNew() { - $this->setToolkitMock(['memoryCheck', 'getToolkitOperation', 'apply']); + $this->setToolkitMock(['unavailableMemoryCheck', 'getToolkitOperation', 'apply']); $this->setToolkitOperationMock('CreateNew', $this->toolkit); $this->toolkit->expects($this->any()) - ->method('memoryCheck') - ->will($this->onConsecutiveCalls($this->memoryCheckPass, $this->memoryCheckFail)); + ->method('unavailableMemoryCheck') + ->will($this->onConsecutiveCalls($this->unavailableMemoryFalse, $this->unavailableMemoryTrue)); $this->toolkit->expects($this->any()) ->method('getToolkitOperation') ->will($this->returnValue($this->toolkitOperation)); @@ -199,11 +199,11 @@ public function testNotEnoughMemoryOnCreateNew() { * @expectedException \RuntimeException */ public function testNotEnoughMemoryOnRotate() { - $this->setToolkitMock(['memoryCheck', 'getToolkitOperation', 'apply']); + $this->setToolkitMock(['unavailableMemoryCheck', 'getToolkitOperation', 'apply']); $this->setToolkitOperationMock('Rotate', $this->toolkit); $this->toolkit->expects($this->any()) - ->method('memoryCheck') - ->will($this->onConsecutiveCalls($this->memoryCheckPass, $this->memoryCheckFail)); + ->method('unavailableMemoryCheck') + ->will($this->onConsecutiveCalls($this->unavailableMemoryFalse, $this->unavailableMemoryTrue)); $this->toolkit->expects($this->any()) ->method('getToolkitOperation') ->will($this->returnValue($this->toolkitOperation));