From d4819b58edda02578ad871d2b32b447f1a25e066 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Mon, 7 Sep 2026 10:07:47 +0000 Subject: [PATCH] Use focal point image cropping --- composer.lock | 12 +++--- src/Appwrite/Autogravity/Gravity.php | 38 ------------------- .../Http/Buckets/Files/Preview/Get.php | 7 ++-- .../Storage/StorageCustomServerTest.php | 4 -- tests/unit/Autogravity/GravityTest.php | 19 ---------- 5 files changed, 10 insertions(+), 70 deletions(-) diff --git a/composer.lock b/composer.lock index d029ff1eb8..6213a05d26 100644 --- a/composer.lock +++ b/composer.lock @@ -4108,16 +4108,16 @@ }, { "name": "utopia-php/image", - "version": "0.8.9", + "version": "0.8.12", "source": { "type": "git", "url": "https://github.com/utopia-php/image.git", - "reference": "6647714267e43bae25c8e8ae7021264eca4a1e84" + "reference": "cab2779faf1e02f3bed9bc5cddd6350a69056948" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/image/zipball/6647714267e43bae25c8e8ae7021264eca4a1e84", - "reference": "6647714267e43bae25c8e8ae7021264eca4a1e84", + "url": "https://api.github.com/repos/utopia-php/image/zipball/cab2779faf1e02f3bed9bc5cddd6350a69056948", + "reference": "cab2779faf1e02f3bed9bc5cddd6350a69056948", "shasum": "" }, "require": { @@ -4148,9 +4148,9 @@ ], "support": { "issues": "https://github.com/utopia-php/image/issues", - "source": "https://github.com/utopia-php/image/tree/0.8.9" + "source": "https://github.com/utopia-php/image/tree/0.8.12" }, - "time": "2026-07-20T18:04:09+00:00" + "time": "2026-09-07T10:00:40+00:00" }, { "name": "utopia-php/locale", diff --git a/src/Appwrite/Autogravity/Gravity.php b/src/Appwrite/Autogravity/Gravity.php index 6b5580a9ac..8c0aff5f06 100644 --- a/src/Appwrite/Autogravity/Gravity.php +++ b/src/Appwrite/Autogravity/Gravity.php @@ -2,8 +2,6 @@ namespace Appwrite\Autogravity; -use Utopia\Image\Image; - class Gravity { public function __construct( @@ -15,42 +13,6 @@ class Gravity } } - public function getType(int $width, int $height, int $sourceWidth, int $sourceHeight): string - { - $sourceAspect = $sourceWidth / $sourceHeight; - $targetAspect = $width / $height; - if ($targetAspect > $sourceAspect) { - $resizeWidth = $width; - $resizeHeight = (int) \ceil($width / $sourceAspect); - } else { - $resizeWidth = (int) \ceil($height * $sourceAspect); - $resizeHeight = $height; - } - - $horizontal = match (true) { - $this->x * $resizeWidth < ($resizeWidth - $width) / 2 => 'left', - $this->x * $resizeWidth > ($resizeWidth + $width) / 2 => 'right', - default => '', - }; - $vertical = match (true) { - $this->y * $resizeHeight < ($resizeHeight - $height) / 2 => 'top', - $this->y * $resizeHeight > ($resizeHeight + $height) / 2 => 'bottom', - default => '', - }; - - return match ([$vertical, $horizontal]) { - ['', ''] => Image::GRAVITY_CENTER, - ['', 'left'] => Image::GRAVITY_LEFT, - ['', 'right'] => Image::GRAVITY_RIGHT, - ['top', ''] => Image::GRAVITY_TOP, - ['top', 'left'] => Image::GRAVITY_TOP_LEFT, - ['top', 'right'] => Image::GRAVITY_TOP_RIGHT, - ['bottom', ''] => Image::GRAVITY_BOTTOM, - ['bottom', 'left'] => Image::GRAVITY_BOTTOM_LEFT, - ['bottom', 'right'] => Image::GRAVITY_BOTTOM_RIGHT, - }; - } - public function unrotate(int $rotation): self { return match (($rotation % 360 + 360) % 360) { diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php index fc4af4a84b..5a2b0718c3 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php @@ -276,6 +276,7 @@ class Get extends Action ); } + $focalPoint = null; if ( $gravity === self::GRAVITY_AUTO && $width > 0 @@ -283,10 +284,10 @@ class Get extends Action && isset($sourceWidth, $sourceHeight) && \abs($width / $height - $sourceWidth / $sourceHeight) > 0.000001 ) { - $automaticGravity = $autogravity + $focalPoint = $autogravity ->get($source) ->unrotate($this->getAutogravityRotation($source)); - $gravity = $automaticGravity->getType($width, $height, $sourceWidth, $sourceHeight); + $gravity = Image::GRAVITY_CENTER; } elseif ($gravity === self::GRAVITY_AUTO) { $gravity = Image::GRAVITY_CENTER; } @@ -295,7 +296,7 @@ class Get extends Action Span::add('storage.transform.crop.width', $width); Span::add('storage.transform.crop.height', $height); Span::add('storage.transform.crop.gravity', $gravity); - $image->crop($width, $height, $gravity); + $image->crop($width, $height, $gravity, x: $focalPoint?->x, y: $focalPoint?->y); } if ($opacity !== 1.0) { diff --git a/tests/e2e/Services/Storage/StorageCustomServerTest.php b/tests/e2e/Services/Storage/StorageCustomServerTest.php index 0292790abb..f514c6d595 100644 --- a/tests/e2e/Services/Storage/StorageCustomServerTest.php +++ b/tests/e2e/Services/Storage/StorageCustomServerTest.php @@ -116,13 +116,9 @@ final class StorageCustomServerTest extends Scope $this->assertSame(120, $image->getImageWidth()); $this->assertSame(320, $image->getImageHeight()); - $right = $this->client->call(Client::METHOD_GET, $path, $headers, [...$params, 'gravity' => 'right']); $center = $this->client->call(Client::METHOD_GET, $path, $headers, [...$params, 'gravity' => 'center']); - $rightImage = new \Imagick(); - $rightImage->readImageBlob($right['body']); $centerImage = new \Imagick(); $centerImage->readImageBlob($center['body']); - $this->assertSame($rightImage->getImageSignature(), $image->getImageSignature()); $this->assertNotSame($centerImage->getImageSignature(), $image->getImageSignature()); $cached = []; diff --git a/tests/unit/Autogravity/GravityTest.php b/tests/unit/Autogravity/GravityTest.php index 773a556a3f..3e06f332c7 100644 --- a/tests/unit/Autogravity/GravityTest.php +++ b/tests/unit/Autogravity/GravityTest.php @@ -8,28 +8,9 @@ use Appwrite\Autogravity\Exception; use Appwrite\Autogravity\Gravity; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; -use Utopia\Image\Image; final class GravityTest extends TestCase { - /** - * @return \Iterator - */ - public static function typeProvider(): \Iterator - { - yield 'landscape to portrait, subject right' => [0.7, 0.5, 180, 320, 1280, 720, Image::GRAVITY_RIGHT]; - yield 'landscape to portrait, subject left' => [0.2, 0.5, 180, 320, 1280, 720, Image::GRAVITY_LEFT]; - yield 'landscape to wide, subject remains visible' => [0.7, 0.5, 800, 600, 1280, 720, Image::GRAVITY_CENTER]; - yield 'portrait to landscape, subject top' => [0.5, 0.1, 600, 300, 600, 900, Image::GRAVITY_TOP]; - yield 'portrait to landscape, subject bottom' => [0.9, 0.9, 600, 300, 600, 900, Image::GRAVITY_BOTTOM]; - } - - #[DataProvider('typeProvider')] - public function testGetType(float $x, float $y, int $width, int $height, int $sourceWidth, int $sourceHeight, string $expected): void - { - $this->assertSame($expected, (new Gravity($x, $y))->getType($width, $height, $sourceWidth, $sourceHeight)); - } - public function testRejectsInvalidCoordinates(): void { $this->expectException(Exception::class);