Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions lib/private/legacy/OC_Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

@kesselb kesselb Dec 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value depends on your machine. I fear we are going to introduce the next "wonky" test ;)
Could you try to set the memory limit for this test to a fixed value?

ini_set('memory_limit', '256M');

Best would be to mock the MemoryInfo class but that's a bit tricky here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try to set the memory limit for this test to a fixed value?

ini_set('memory_limit', '256M');

Good idea, thanks! :)

->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'));
Expand Down