mirror of
https://github.com/nextcloud/server.git
synced 2026-11-22 13:03:44 +01:00
Small previews like the 64px and 256px ones of the files list and grid view were resized from the fully decoded max preview, a 12 MP image for a camera photo. libjpeg can scale by 1/2, 1/4 or 1/8 while decoding: with Imagick, decoding a 12 MP JPEG this way takes about 30ms instead of about 90ms with GD, and the resize then starts from a 500x380 px image instead of a 4032x3024 px one. Use it when Imagick is available and the requested preview is at most 1/8 of the max preview, otherwise decode the full max preview as before. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Carl Schwan <carl@carlschwan.eu>
63 lines
2.0 KiB
PHP
63 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace Test\Preview;
|
|
|
|
use OC\Preview\GeneratorHelper;
|
|
use OC\Preview\IMagickSupport;
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
|
use Test\TestCase;
|
|
|
|
class GeneratorHelperTest extends TestCase {
|
|
private function getPreviewFile(string $mimeType, int $width, int $height): ISimpleFile {
|
|
$image = imagecreatetruecolor($width, $height);
|
|
ob_start();
|
|
if ($mimeType === 'image/jpeg') {
|
|
imagejpeg($image);
|
|
} else {
|
|
imagepng($image);
|
|
}
|
|
$data = ob_get_clean();
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
$file->method('getMimeType')->willReturn($mimeType);
|
|
$file->method('getContent')->willReturn($data);
|
|
return $file;
|
|
}
|
|
|
|
private function getHelper(bool $hasImagick): GeneratorHelper {
|
|
$imagickSupport = $this->createMock(IMagickSupport::class);
|
|
$imagickSupport->method('hasExtension')->willReturn($hasImagick);
|
|
return new GeneratorHelper($imagickSupport);
|
|
}
|
|
|
|
public function testGetScaledImage(): void {
|
|
if (!extension_loaded('imagick')) {
|
|
$this->markTestSkipped('Imagick is required to decode JPEG images at a reduced size');
|
|
}
|
|
|
|
$image = $this->getHelper(true)->getScaledImage($this->getPreviewFile('image/jpeg', 4000, 3000), 256, 192);
|
|
|
|
$this->assertNotNull($image);
|
|
$this->assertSame('image/jpeg', $image->dataMimeType());
|
|
$this->assertGreaterThanOrEqual(256, $image->width());
|
|
$this->assertGreaterThanOrEqual(192, $image->height());
|
|
$this->assertLessThan(4000, $image->width());
|
|
$this->assertEqualsWithDelta(4 / 3, $image->width() / $image->height(), 0.01);
|
|
}
|
|
|
|
public function testGetScaledImageWithoutImagick(): void {
|
|
$this->assertNull($this->getHelper(false)->getScaledImage($this->getPreviewFile('image/jpeg', 800, 600), 64, 48));
|
|
}
|
|
|
|
public function testGetScaledImageOfPng(): void {
|
|
$this->assertNull($this->getHelper(true)->getScaledImage($this->getPreviewFile('image/png', 800, 600), 64, 48));
|
|
}
|
|
}
|