diff --git a/core/lib/Drupal/Component/Utility/Bytes.php b/core/lib/Drupal/Component/Utility/Bytes.php index 0bcac31..4232635 100644 --- a/core/lib/Drupal/Component/Utility/Bytes.php +++ b/core/lib/Drupal/Component/Utility/Bytes.php @@ -44,4 +44,33 @@ public static function toInt($size) { } } + /** + * Returns a string representing a byte size. + * + * @param int $size + * An integer representation of the size in bytes. + * + * @return string + * A string size expressed as a number of bytes or multiples. + */ + public static function toString($size) { + if ($size < Bytes::KILOBYTE) { + return $size . ' B'; + } + else { + $size = $size / Bytes::KILOBYTE; + $units = ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + foreach ($units as $unit) { + if (round($size, 2) >= Bytes::KILOBYTE) { + $size = $size / Bytes::KILOBYTE; + } + else { + $size = round($size, 2); + break; + } + } + return $size . ' ' . $unit; + } + } + } diff --git a/core/lib/Drupal/Component/Utility/Environment.php b/core/lib/Drupal/Component/Utility/Environment.php index 087bcad..356a515 100644 --- a/core/lib/Drupal/Component/Utility/Environment.php +++ b/core/lib/Drupal/Component/Utility/Environment.php @@ -13,7 +13,7 @@ class Environment { /** - * Compares the memory required for an operation to the available memory. + * Compares the memory required for an operation to the total memory. * * @param string $required * The memory required for the operation, expressed as a number of bytes with @@ -42,4 +42,37 @@ public static function checkMemoryLimit($required, $memory_limit = NULL) { return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::toInt($memory_limit) >= Bytes::toInt($required))); } + /** + * Compares the memory required for an operation to the remaining memory. + * + * @param string $required + * The memory required for the operation, expressed as a number of bytes with + * optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8bytes, + * 9mbytes). + * @param string $memory_limit + * (optional) The memory limit for the operation, expressed as a number of + * bytes with optional SI or IEC binary unit prefix (e.g. 2, 3K, 5MB, 10G, + * 6GiB, 8bytes, 9mbytes). If no value is passed, the current PHP + * memory_limit will be used. Defaults to NULL. + * + * @return bool + * TRUE if there is sufficient memory to allow the operation, or FALSE + * otherwise. + */ + public static function checkAvailableMemory($required, $memory_limit = NULL) { + return static::checkMemoryLimit(static::getUsedMemory() + $required, $memory_limit); + } + + /** + * Returns the current memory in use. + * + * Wrapper of memory_get_usage(), to allow unit testing. + * + * @return int + * The amount of memory, in bytes, currently in use. + */ + protected static function getUsedMemory() { + return memory_get_usage(TRUE); + } + } diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 1f5c89b..f8b7f81 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -45,13 +45,6 @@ class GDToolkit extends ImageToolkitBase { protected $type; /** - * Memory required to perform a GD operation, in bytes. - * - * @var int - */ - protected $requiredMemory; - - /** * Image information from a file, available prior to loading the GD resource. * * This contains a copy of the array returned by executing getimagesize() @@ -156,17 +149,6 @@ public function getResource() { } /** - * Returns the memory required to perform a GD function. - * - * @return string|null - * A human readable representation of the memory required to perform a GD - * operation (e.g. '15.6 Mb'). - */ - public function getRequiredMemory() { - return $this->requiredMemory !== NULL ? format_size($this->requiredMemory) : NULL; - } - - /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { @@ -204,11 +186,11 @@ protected function load() { } // Check if enough memory is available. - if (!$this->checkAvailableMemory($this->getWidth(), $this->getHeight())) { - // If not, need to mark the image as invalid, to avoid useless attempts - // to manipulate it. + if (($required_memory = $this->checkAvailableMemory($this->getWidth(), $this->getHeight())) > 0) { + // 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: %required_memory) to load image '%file'", ['%required_memory' => $this->getRequiredMemory(), '%file' => $this->getSource()]); + $this->logger->error("Not enough memory (required: %required_memory) to load image '%file'", ['%required_memory' => Bytes::toString($required_memory), '%file' => $this->getSource()]); return FALSE; } @@ -488,17 +470,15 @@ 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. + * @return int + * -1 if there is sufficient memory to allow the operation. Otherwise, the + * required memory, in bytes. * * @see http://php.net/manual/en/function.imagecreatetruecolor.php#99623 */ public function checkAvailableMemory($width, $height, $bits_per_pixel = 32, $tweak_factor = 1.7) { - $this->memoryRequired = (int) ($width * $height * ($bits_per_pixel / 8) * $tweak_factor); - $usage = memory_get_usage(TRUE); - $memory_limit = ini_get('memory_limit'); - $size = Bytes::toInt($memory_limit); - return Environment::checkMemoryLimit($usage + $this->memoryRequired, $memory_limit); + $required = (int) ($width * $height * ($bits_per_pixel / 8) * $tweak_factor); + return Environment::checkAvailableMemory($required) ? -1 : $required; } } 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 8226dc7..3846aa8 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php @@ -7,6 +7,7 @@ namespace Drupal\system\Plugin\ImageToolkit\Operation\gd; +use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Color; /** @@ -85,8 +86,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'])) { - throw new \RuntimeException("Not enough memory (required: {$this->getToolkit()->getRequiredMemory()}) to perform the image '{$this->getPluginId()}' operation"); + if (($required_memory = $this->getToolkit()->checkAvailableMemory($arguments['width'], $arguments['height'])) > 0) { + throw new \RuntimeException("Not enough memory (required: " . Bytes::toString($required_memory) . ") 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 f5fc048..6712456 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php @@ -7,6 +7,7 @@ namespace Drupal\system\Plugin\ImageToolkit\Operation\gd; +use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Color; /** @@ -101,11 +102,11 @@ protected function execute(array $arguments) { // Check that enough memory is available. // @todo more accurate width and height of the image after rotation will be // available after #1551686. In the meantime, the maximum width and height - // can never exceed the lenght of the diagonal of the rectangle represented + // can never exceed the length of the diagonal of the rectangle represented // by the image. $diagonal = (int) sqrt(pow($this->getToolkit()->getWidth(), 2) + pow($this->getToolkit()->getHeight(), 2)) + 1; - if (!$this->getToolkit()->checkAvailableMemory($diagonal, $diagonal)) { - throw new \RuntimeException("Not enough memory (required: {$this->getToolkit()->getRequiredMemory()}) to perform the image '{$this->getPluginId()}' operation"); + if (($required_memory = $this->getToolkit()->checkAvailableMemory($diagonal, $diagonal)) > 0) { + throw new \RuntimeException("Not enough memory (required: " . Bytes::toString($required_memory) . ") to perform the image '{$this->getPluginId()}' operation"); } // Stores the original GD resource. diff --git a/core/modules/system/src/Tests/Image/ToolkitGdTest.php b/core/modules/system/src/Tests/Image/ToolkitGdTest.php index 8d9b476..1cbec51 100644 --- a/core/modules/system/src/Tests/Image/ToolkitGdTest.php +++ b/core/modules/system/src/Tests/Image/ToolkitGdTest.php @@ -436,7 +436,7 @@ public function testMemoryAllocation() { // Test creation of an image that will exceed the memory available. $old_res = $image->getToolkit()->getResource(); - $image->createNew(20000, 20000); + $this->assertFalse($image->createNew(20000, 20000)); $new_res = $image->getToolkit()->getResource(); // Check that a new resource has not been created, and the old one is still // valid. diff --git a/core/tests/Drupal/Tests/Component/Utility/BytesTest.php b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php index 8e72b6c..4e7f671 100644 --- a/core/tests/Drupal/Tests/Component/Utility/BytesTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php @@ -37,6 +37,23 @@ public function testToInt($size, $expected_int) { } /** + * Tests \Drupal\Component\Utility\Bytes::toString(). + * + * @param int $size + * The value for the size argument for + * \Drupal\Component\Utility\Bytes::toString(). + * @param string $expected_string + * The expected return value from + * \Drupal\Component\Utility\Bytes::toString(). + * + * @dataProvider providerTestToString + * @covers ::toString + */ + public function testToString($size, $expected_string) { + $this->assertEquals($expected_string, Bytes::toString($size)); + } + + /** * Provides data for testToInt. * * @return array @@ -62,4 +79,29 @@ public function providerTestToInt() { ); } + /** + * Provides data for testToString. + * + * @return array[] + * An array of arrays, each containing the argument for + * \Drupal\Component\Utility\Bytes::toString(): size, and the expected return + * value. + */ + public function providerTestToString() { + return [ + [1, '1 B'], + [768, '768 B'], + [Bytes::KILOBYTE, '1 KB'], + [pow(Bytes::KILOBYTE, 2), '1 MB'], + [pow(Bytes::KILOBYTE, 3), '1 GB'], + [pow(Bytes::KILOBYTE, 4), '1 TB'], + [pow(Bytes::KILOBYTE, 5), '1 PB'], + [pow(Bytes::KILOBYTE, 6), '1 EB'], + [pow(Bytes::KILOBYTE, 7), '1 ZB'], + [pow(Bytes::KILOBYTE, 8), '1 YB'], + [23476892, '23476892'], + [79691776, '76 MB'], + [81862076662, '76.24 GB'], + ]; + } } diff --git a/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php index 72f04f7..181457a 100644 --- a/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php @@ -7,6 +7,7 @@ namespace Drupal\Tests\Component\Utility; +use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Environment; use Drupal\Tests\UnitTestCase; @@ -41,6 +42,37 @@ public function testCheckMemoryLimit($required, $custom_memory_limit, $expected) } /** + * Tests \Drupal\Component\Utility\Environment::checkAvailableMemory(). + * + * @dataProvider providerTestCheckAvailableMemory + * @covers ::checkAvailableMemory + * + * @param string $custom_memory_limit + * A custom memory limit argument for + * \Drupal\Component\Utility\Environment::checkAvailableMemory(). + * @param string $used_memory + * A mocked value for used memory. + * @param string $required + * The required memory argument for + * \Drupal\Component\Utility\Environment::checkAvailableMemory(). + * @param bool $expected + * The expected return value from + * \Drupal\Component\Utility\Environment::checkAvailableMemory(). + */ + public function testCheckAvailableMemory($custom_memory_limit, $used_memory, $required, $expected) { + $mock_builder = $this->getMockBuilder('Drupal\Component\Utility\Environment'); + $environment = $mock_builder + ->setMethods('getUsedMemory') + ->getMock(); + $environment->expects($this->any()) + ->method('getUsedMemory') + ->will($this->returnValue(Bytes::toInt($usedMemory))); + + $actual = $environment::checkAvailableMemory($required, $custom_memory_limit); + $this->assertEquals($expected, $actual); + } + + /** * Provides data for testCheckMemoryLimit(). * * @return array @@ -64,4 +96,26 @@ public function providerTestCheckMemoryLimit() { ); } + /** + * Provides data for testCheckAvailableMemory(). + * + * @return array[] + * An array of arrays, each containing the arguments for + * \Drupal\Component\Utility\Environment::checkAvailableMemory(): + * memory_limit, mocked memory in use, required memory and the expected + * return value. + */ + public function providerTestCheckAvailableMemory() { + return [ + // 30MB should be available with limit of 60MB and usage of 10MB. + ['60MB', '10MB', '30MB', TRUE], + // Limit = required + used. + ['40MB', '30MB', '10MB', TRUE], + // Exceed memory limit. + ['20MB', '10MB', '30MB', FALSE], + // Exceed memory limit by 1 byte. + ['20MB', '20MB', '1', FALSE], + ]; + } + }