diff --git a/config/config.sample.php b/config/config.sample.php index a74f03b5cc77f..22e7e567ec573 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -1171,9 +1171,9 @@ * If creating the image would allocate more memory, preview generation will * be disabled and the default mimetype icon is shown. Set to -1 for no limit. * - * Defaults to ``128`` megabytes + * Defaults to half of the max PHP memory which is ``256`` megabytes in case of 512 max php memory */ -'preview_max_memory' => 128, +'preview_max_memory' => 256, /** * custom path for LibreOffice/OpenOffice binary diff --git a/lib/private/legacy/OC_Image.php b/lib/private/legacy/OC_Image.php index 9ccc6409ba030..7beacbda03319 100644 --- a/lib/private/legacy/OC_Image.php +++ b/lib/private/legacy/OC_Image.php @@ -50,9 +50,6 @@ */ class OC_Image implements \OCP\IImage { - // Default memory limit for images to load (128 MBytes). - protected const DEFAULT_MEMORY_LIMIT = 128; - // Default quality for jpeg images protected const DEFAULT_JPEG_QUALITY = 80; @@ -101,6 +98,18 @@ public function __construct($imageRef = null, \OCP\ILogger $logger = null, \OCP\ } } + public static function getDefaultMemoryLimit(): int { + static $memoryLimit; + if (!isset($memoryLimit)) { + $memoryInfo = \OC::$server->query(\OC\MemoryInfo::class); + $memoryLimit = $memoryInfo->getMemoryLimit(); + if ($memoryLimit > 0) { + $memoryLimit = $memoryLimit / (1024 * 1024 * 2); // getMemoryLimit returns the value in bytes but we need it in megabytes and divide it by 2 + } + } + return $memoryLimit; + } + /** * Determine whether the object contains an image resource. * @@ -577,7 +586,7 @@ public function loadFromFileHandle($handle) { * @return bool true if allocating is allowed, false otherwise */ private function checkImageMemory($width, $height) { - $memory_limit = $this->config->getSystemValueInt('preview_max_memory', self::DEFAULT_MEMORY_LIMIT); + $memory_limit = $this->config->getSystemValueInt('preview_max_memory', self::getDefaultMemoryLimit()); if ($memory_limit < 0) { // Not limited. return true; diff --git a/tests/lib/ImageTest.php b/tests/lib/ImageTest.php index 2a753b0155022..f738b73b6916e 100644 --- a/tests/lib/ImageTest.php +++ b/tests/lib/ImageTest.php @@ -144,8 +144,8 @@ public function testData() { ->willReturn(null); $config->expects($this->once()) ->method('getSystemValueInt') - ->with('preview_max_memory', 128) - ->willReturn(128); + ->with('preview_max_memory', 384) + ->willReturn(384); $img = new \OC_Image(null, null, $config); $img->loadFromFile(OC::$SERVERROOT.'/tests/data/testimage.jpg'); $raw = imagecreatefromstring(file_get_contents(OC::$SERVERROOT.'/tests/data/testimage.jpg'));