From 830c0eaa54198ee87eae63f438755d9949f8f535 Mon Sep 17 00:00:00 2001 From: craig410 Date: Thu, 17 Oct 2024 15:22:10 +0100 Subject: [PATCH] Support VIPS thumbnail size option to control whether to downsize, upsize or both --- CHANGELOG.md | 4 ++++ src/Processor/ImageOperations.php | 2 ++ .../unit/Processor/BaseImageProcessorTestCases.php | 14 +++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26e51d8..3fddf90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ### Unreleased +### v2.2.0 (2024-10-17) + +* Support `size` option on thumbnail() to control whether to upsize, downsize or both(default). See https://www.libvips.org/API/current/libvips-resample.html#vips-thumbnail + ### v2.1.0 (2024-10-01) * Support PHP 8.3 diff --git a/src/Processor/ImageOperations.php b/src/Processor/ImageOperations.php index 1a74e1e..93e78c3 100644 --- a/src/Processor/ImageOperations.php +++ b/src/Processor/ImageOperations.php @@ -14,6 +14,7 @@ use Jcupitt\Vips\Extend; use Jcupitt\Vips\Image; use Jcupitt\Vips\Interpretation; +use Jcupitt\Vips\Size; use JetBrains\PhpStorm\ArrayShape; class ImageOperations implements ImageProcessorInterface @@ -65,6 +66,7 @@ public function thumbnail(string $source_path, string $output_path, array $opera $source_path, $operations['scale']['width'], [ 'height' => $operations['scale']['height'] ?? 10_000_000, + 'size' => $operations['scale']['size'] ?? Size::BOTH, 'export-profile' => Interpretation::SRGB, ] ); diff --git a/test/unit/Processor/BaseImageProcessorTestCases.php b/test/unit/Processor/BaseImageProcessorTestCases.php index 844141b..e7f7ed8 100644 --- a/test/unit/Processor/BaseImageProcessorTestCases.php +++ b/test/unit/Processor/BaseImageProcessorTestCases.php @@ -6,6 +6,7 @@ namespace test\unit\Processor; use Ingenerator\ImageProcessing\Processor\ImageProcessorInterface; +use Jcupitt\Vips\Size; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Ingenerator\ImageProcessing\Processor\ImageCompare; @@ -150,6 +151,14 @@ public static function providerThumbnailOperations(): array ], self::RESOURCE_DIR.'read_alpha.png', ], + 'Scale down only if src image greater than requested size, ie DO NOT SCALE UP' => [ + self::RESOURCE_DIR.'porto_1024.jpg', + [ + 'scale' => ['width' => 1025, 'height' => 787, 'size' => Size::DOWN], + 'save' => ['type' => 'webp'], + ], + self::RESOURCE_DIR.'porto_1024.webp', + ], 'Scale up' => [ self::RESOURCE_DIR.'logo.png', [ @@ -198,7 +207,10 @@ public function test_thumbnail(string $source_image, array $operations, string $ pathinfo($path_expected_result, PATHINFO_EXTENSION) ); $subject->thumbnail($source_image, $output_file, $operations); - $this->assertTrue(ImageCompare::isSame($path_expected_result, $output_file)); + $this->assertTrue( + ImageCompare::isSame($path_expected_result, $output_file), + 'Failed asserting that '.$output_file.' is same image as '.$path_expected_result + ); } protected function setUp(): void