diff --git a/.env b/.env index 905b9b5e84..2ab4f1005c 100644 --- a/.env +++ b/.env @@ -76,6 +76,7 @@ _APP_EMBEDDING_TIMEOUT=30000 _APP_STORAGE_DEVICE=Local _APP_STORAGE_LIMIT=30000000 _APP_STORAGE_PREVIEW_LIMIT=20000000 +_APP_AUTOGRAVITY_HOST=http://appwrite-autogravity:8080 _APP_STORAGE_ANTIVIRUS=disabled _APP_STORAGE_ANTIVIRUS_HOST=clamav _APP_STORAGE_ANTIVIRUS_PORT=3310 diff --git a/app/config/variables.php b/app/config/variables.php index aed2f57f54..ef208047a6 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -904,6 +904,16 @@ return [ 'question' => '', 'filter' => '' ], + [ + 'name' => '_APP_AUTOGRAVITY_HOST', + 'description' => 'The host used by Appwrite to determine automatic image crop gravity.', + 'introduction' => '2.0.0', + 'default' => '', + 'required' => false, + 'overwrite' => true, + 'question' => '', + 'filter' => '' + ], [ 'name' => '_APP_STORAGE_ANTIVIRUS', 'description' => 'This variable allows you to disable the internal anti-virus scans. This value is set to \'disabled\' by default, to enable the scans set the value to \'enabled\'. Before enabling, you must add the ClamAV service and depend on it on main Appwrite service.', diff --git a/app/init/resources.php b/app/init/resources.php index 1d2cd54a9d..98fce25e52 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -1,5 +1,7 @@ set('screenshots', function () { return new ScreenshotsClient($client); }, []); +$container->set('autogravity', function (Cache $cache) { + $host = System::getEnv('_APP_AUTOGRAVITY_HOST', ''); + $client = $host === '' + ? null + : (new Client(new SwooleClientAdapter())) + ->withBaseUri($host) + ->withTimeout(30); + + return new AutogravityDetector($client === null ? null : new AutogravityClient($client), $cache); +}, ['cache']); + $container->set('telemetry', fn () => new NoTelemetry(), []); $container->set('authorization', fn () => new Authorization(), []); diff --git a/composer.lock b/composer.lock index dd24ea6ef0..fe3a121a87 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/docker-compose.yml b/docker-compose.yml index f2894a5ee3..db402af49e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -76,6 +76,7 @@ services: - redis - ${_APP_DB_HOST:-postgresql} - appwrite-geo + - appwrite-autogravity environment: - _APP_ENV - _APP_USAGE_STATS @@ -163,6 +164,7 @@ services: - _APP_SMTP_PASSWORD - _APP_STORAGE_LIMIT - _APP_STORAGE_PREVIEW_LIMIT + - _APP_AUTOGRAVITY_HOST - _APP_STORAGE_ANTIVIRUS - _APP_STORAGE_ANTIVIRUS_HOST - _APP_STORAGE_ANTIVIRUS_PORT @@ -1578,6 +1580,13 @@ services: networks: - appwrite + appwrite-autogravity: + container_name: appwrite-autogravity + image: ghcr.io/appwrite/autogravity:0.0.6 + restart: unless-stopped + networks: + - appwrite + clickhouse: image: clickhouse/clickhouse-server:26.4.3-alpine restart: unless-stopped diff --git a/src/Appwrite/Autogravity/Client.php b/src/Appwrite/Autogravity/Client.php new file mode 100644 index 0000000000..03dea42189 --- /dev/null +++ b/src/Appwrite/Autogravity/Client.php @@ -0,0 +1,51 @@ +factory = new Factory(); + } + + /** + * Find the image's focal point. + * + * @throws Exception on an error response or malformed result + * @throws ClientExceptionInterface on transport failure + */ + public function analyze(string $image): Gravity + { + $request = $this->factory->body( + Method::POST, + 'analyze', + $image, + 'application/octet-stream' + ); + $response = $this->client->sendRequest($request); + $status = $response->getStatusCode(); + $body = (string) $response->getBody(); + $result = \json_decode($body, true); + + if ($status >= 400) { + $error = \is_array($result) ? ($result['error'] ?? null) : null; + throw new Exception(\is_string($error) ? $error : 'Autogravity failed with status ' . $status, $status); + } + + $x = \is_array($result) ? ($result['gravity']['x'] ?? null) : null; + $y = \is_array($result) ? ($result['gravity']['y'] ?? null) : null; + if (!\is_numeric($x) || !\is_numeric($y)) { + throw new Exception('Autogravity returned an invalid gravity', $status); + } + + return new Gravity((float) $x, (float) $y); + } +} diff --git a/src/Appwrite/Autogravity/Detector.php b/src/Appwrite/Autogravity/Detector.php new file mode 100644 index 0000000000..187e99a69e --- /dev/null +++ b/src/Appwrite/Autogravity/Detector.php @@ -0,0 +1,48 @@ +client !== null; + } + + public function get(string $source): Gravity + { + if ($this->client === null) { + throw new Exception('Autogravity needs to be configured with _APP_AUTOGRAVITY_HOST to use automatic gravity'); + } + + $key = 'autogravity-' . \hash('sha256', $source); + $cached = $this->cache->load($key, self::CACHE_TTL); + if ( + \is_array($cached) + && isset($cached['x'], $cached['y']) + && \is_numeric($cached['x']) + && \is_numeric($cached['y']) + && $cached['x'] >= 0 + && $cached['x'] <= 1 + && $cached['y'] >= 0 + && $cached['y'] <= 1 + ) { + return new Gravity((float) $cached['x'], (float) $cached['y']); + } + + $gravity = $this->client->analyze($source); + $this->cache->save($key, $gravity->getArrayCopy()); + + return $gravity; + } +} diff --git a/src/Appwrite/Autogravity/Exception.php b/src/Appwrite/Autogravity/Exception.php new file mode 100644 index 0000000000..a94252b7b5 --- /dev/null +++ b/src/Appwrite/Autogravity/Exception.php @@ -0,0 +1,7 @@ + 1 || $y < 0 || $y > 1) { + throw new Exception('Autogravity returned an invalid gravity'); + } + } + + public function unrotate(int $rotation): self + { + return match (($rotation % 360 + 360) % 360) { + 0 => $this, + 90 => new self($this->y, 1 - $this->x), + 180 => new self(1 - $this->x, 1 - $this->y), + 270 => new self(1 - $this->y, $this->x), + default => throw new Exception('Autogravity cannot reverse an unsupported rotation'), + }; + } + + /** + * @return array{x: float, y: float} + */ + public function getArrayCopy(): array + { + return ['x' => $this->x, 'y' => $this->y]; + } +} 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 7c7eaacf71..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 @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\Storage\Http\Buckets\Files\Preview; +use Appwrite\Autogravity\Detector as AutogravityDetector; use Appwrite\Extend\Exception; use Appwrite\OpenSSL\OpenSSL; use Appwrite\Platform\Action; @@ -40,6 +41,8 @@ class Get extends Action { use HTTP; + private const GRAVITY_AUTO = 'auto'; + public static function getName() { return 'getFilePreview'; @@ -79,7 +82,7 @@ class Get extends Action ->param('fileId', '', new UID(), 'File ID') ->param('width', 0, new Range(0, 4000), 'Resize preview image width, Pass an integer between 0 to 4000.', true) ->param('height', 0, new Range(0, 4000), 'Resize preview image height, Pass an integer between 0 to 4000.', true) - ->param('gravity', Image::GRAVITY_CENTER, new WhiteList(Image::getGravityTypes()), 'Image crop gravity. Can be one of ' . implode(",", Image::getGravityTypes()), true, enum: new Enum(name: 'ImageGravity')) + ->param('gravity', Image::GRAVITY_CENTER, new WhiteList([self::GRAVITY_AUTO, ...Image::getGravityTypes()]), 'Image crop gravity. Can be one of ' . implode(",", [self::GRAVITY_AUTO, ...Image::getGravityTypes()]), true, enum: new Enum(name: 'ImageGravity')) ->param('quality', -1, new Range(-1, 100), 'Preview image quality. Pass an integer between 0 to 100. Defaults to keep existing image quality.', true) ->param('borderWidth', 0, new Range(0, 100), 'Preview image border in pixels. Pass an integer between 0 to 100. Defaults to 0.', true) ->param('borderColor', '', new HexColor(), 'Preview image border color. Use a valid HEX color, no # is needed for prefix.', true) @@ -100,6 +103,7 @@ class Get extends Action ->inject('authorization') ->inject('user') ->inject('cacheControlForStorage') + ->inject('autogravity') ->callback($this->action(...)); } @@ -127,7 +131,8 @@ class Get extends Action Document $project, Authorization $authorization, User $user, - callable $cacheControlForStorage + callable $cacheControlForStorage, + AutogravityDetector $autogravity ) { if (!\extension_loaded('imagick')) { @@ -264,11 +269,34 @@ class Get extends Action throw new Exception(Exception::STORAGE_FILE_TYPE_UNSUPPORTED, $e->getMessage()); } + if ($gravity === self::GRAVITY_AUTO && !$autogravity->isEnabled()) { + throw new Exception( + Exception::GENERAL_ARGUMENT_INVALID, + 'Autogravity needs to be configured with _APP_AUTOGRAVITY_HOST to use automatic gravity' + ); + } + + $focalPoint = null; + if ( + $gravity === self::GRAVITY_AUTO + && $width > 0 + && $height > 0 + && isset($sourceWidth, $sourceHeight) + && \abs($width / $height - $sourceWidth / $sourceHeight) > 0.000001 + ) { + $focalPoint = $autogravity + ->get($source) + ->unrotate($this->getAutogravityRotation($source)); + $gravity = Image::GRAVITY_CENTER; + } elseif ($gravity === self::GRAVITY_AUTO) { + $gravity = Image::GRAVITY_CENTER; + } + if ($width > 0 || $height > 0 || $gravity !== Image::GRAVITY_CENTER) { 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) { @@ -351,4 +379,18 @@ class Get extends Action unset($image); } + + private function getAutogravityRotation(string $source): int + { + $metadata = new \Imagick(); + $metadata->pingImageBlob($source); + $orientation = $metadata->getImageProperties()['exif:Orientation'] ?? null; + + return match ($orientation) { + '3' => 180, + '6' => 90, + '8' => -90, + default => 0, + }; + } } diff --git a/tests/e2e/Services/Storage/StorageCustomServerTest.php b/tests/e2e/Services/Storage/StorageCustomServerTest.php index 39a1b112d7..f514c6d595 100644 --- a/tests/e2e/Services/Storage/StorageCustomServerTest.php +++ b/tests/e2e/Services/Storage/StorageCustomServerTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Tests\E2E\Services\Storage; +use Appwrite\Extend\Exception; use CURLFile; use PHPUnit\Framework\Attributes\Group; use Tests\E2E\Client; @@ -55,6 +56,85 @@ final class StorageCustomServerTest extends Scope return self::$cachedBucket[$cacheKey]; } + public function testGetFilePreviewWithAutomaticGravity(): void + { + $bucketId = $this->setupBucket()['bucketId']; + $source = \tempnam(\sys_get_temp_dir(), 'appwrite-autogravity-'); + if ($source === false) { + $this->fail('Failed to create a temporary image'); + } + $subject = new \Imagick(__DIR__ . '/../../../resources/disk-a/kitten-1.jpg'); + $subject->resizeImage(900, 0, \Imagick::FILTER_LANCZOS, 1); + $canvas = new \Imagick(); + $canvas->newImage(2400, 1920, 'white', 'png'); + $canvas->compositeImage($subject, \Imagick::COMPOSITE_OVER, 1450, 0); + $canvas->writeImage($source); + + $file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge([ + 'content-type' => 'multipart/form-data', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'fileId' => ID::unique(), + 'file' => new CURLFile($source, 'image/png', 'autogravity.png'), + ]); + \unlink($source); + + $this->assertEquals(201, $file['headers']['status-code']); + + $headers = array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()); + $params = [ + 'width' => 120, + 'height' => 320, + 'gravity' => 'auto', + 'output' => 'png', + ]; + $path = '/storage/buckets/' . $bucketId . '/files/' . $file['body']['$id'] . '/preview'; + + $preview = $this->client->call(Client::METHOD_GET, $path, $headers, $params); + + if (System::getEnv('_APP_AUTOGRAVITY_HOST', '') === '') { + $this->assertEquals(400, $preview['headers']['status-code']); + $this->assertSame(Exception::GENERAL_ARGUMENT_INVALID, $preview['body']['type']); + $this->assertSame( + 'Autogravity needs to be configured with _APP_AUTOGRAVITY_HOST to use automatic gravity', + $preview['body']['message'] + ); + + return; + } + + $this->assertEquals(200, $preview['headers']['status-code']); + $this->assertEquals('image/png', $preview['headers']['content-type']); + $this->assertEquals('miss', $preview['headers']['x-appwrite-cache']); + $this->assertNotEmpty($preview['body']); + + $image = new \Imagick(); + $image->readImageBlob($preview['body']); + $this->assertSame(120, $image->getImageWidth()); + $this->assertSame(320, $image->getImageHeight()); + + $center = $this->client->call(Client::METHOD_GET, $path, $headers, [...$params, 'gravity' => 'center']); + $centerImage = new \Imagick(); + $centerImage->readImageBlob($center['body']); + $this->assertNotSame($centerImage->getImageSignature(), $image->getImageSignature()); + + $cached = []; + $this->assertEventually(function () use (&$cached, $path, $headers, $params, $image): void { + $cached = $this->client->call(Client::METHOD_GET, $path, $headers, $params); + + $this->assertSame('hit', $cached['headers']['x-appwrite-cache']); + $cachedImage = new \Imagick(); + $cachedImage->readImageBlob($cached['body']); + $this->assertSame($image->getImageSignature(), $cachedImage->getImageSignature()); + }); + + $this->assertEquals(200, $cached['headers']['status-code']); + $this->assertEquals('image/png', $cached['headers']['content-type']); + } + public function testCreateBucket(): void { /** diff --git a/tests/unit/Autogravity/ClientTest.php b/tests/unit/Autogravity/ClientTest.php new file mode 100644 index 0000000000..06220f8d7d --- /dev/null +++ b/tests/unit/Autogravity/ClientTest.php @@ -0,0 +1,80 @@ +test->setRequest($request); + return $this->response; + } + }); + } + + public function setRequest(RequestInterface $request): void + { + $this->request = $request; + } + + public function testAnalyze(): void + { + $response = (new Response(200, body: new Stream('{"gravity":{"x":0.7,"y":0.4},"confidence":0.9}'))) + ->withHeader('Content-Type', 'application/json'); + + $gravity = $this->client($response)->analyze('image-bytes'); + + $this->assertEqualsWithDelta(0.7, $gravity->x, PHP_FLOAT_EPSILON); + $this->assertEqualsWithDelta(0.4, $gravity->y, PHP_FLOAT_EPSILON); + $this->assertInstanceOf(RequestInterface::class, $this->request); + $this->assertSame('POST', $this->request->getMethod()); + $this->assertSame('analyze', (string) $this->request->getUri()); + $this->assertSame('application/octet-stream', $this->request->getHeaderLine('Content-Type')); + $this->assertSame('image-bytes', (string) $this->request->getBody()); + } + + public function testAnalyzeError(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('invalid image'); + $this->expectExceptionCode(400); + + $this->client(new Response(400, body: new Stream('{"error":"invalid image"}')))->analyze('invalid'); + } + + public function testAnalyzeInvalidResponse(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Autogravity returned an invalid gravity'); + + $this->client(new Response(200, body: new Stream('{"gravity":{"x":2,"y":0.5}}')))->analyze('image'); + } + + public function testAnalyzeErrorWithoutBody(): void + { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Autogravity failed with status 502'); + + $this->client(new Response(502, body: new Stream('')))->analyze('image'); + } +} diff --git a/tests/unit/Autogravity/DetectorTest.php b/tests/unit/Autogravity/DetectorTest.php new file mode 100644 index 0000000000..0b60ab29bc --- /dev/null +++ b/tests/unit/Autogravity/DetectorTest.php @@ -0,0 +1,84 @@ +assertFalse($detector->isEnabled()); + $this->expectException(Exception::class); + $this->expectExceptionMessage('Autogravity needs to be configured with _APP_AUTOGRAVITY_HOST to use automatic gravity'); + + $detector->get('source-image'); + } + + public function testCachesDetectionBySource(): void + { + $http = new CountingClient(new Response( + 200, + body: new Stream('{"gravity":{"x":0.7,"y":0.4},"confidence":0.9}') + )); + $detector = new Detector(new Client($http), new Cache(new Memory())); + + $this->assertTrue($detector->isEnabled()); + + $first = $detector->get('source-image'); + $second = $detector->get('source-image'); + + $this->assertEqualsWithDelta(0.7, $first->x, PHP_FLOAT_EPSILON); + $this->assertEqualsWithDelta(0.4, $first->y, PHP_FLOAT_EPSILON); + $this->assertSame($first->getArrayCopy(), $second->getArrayCopy()); + $this->assertSame(1, $http->requests); + $this->assertSame('source-image', $http->lastBody); + } + + public function testDifferentSourcesAreDetectedSeparately(): void + { + $http = new CountingClient(new Response( + 200, + body: new Stream('{"gravity":{"x":0.5,"y":0.5},"confidence":0.9}') + )); + $detector = new Detector(new Client($http), new Cache(new Memory())); + + $detector->get('first-source'); + $detector->get('second-source'); + + $this->assertSame(2, $http->requests); + } +} + +final class CountingClient implements ClientInterface +{ + public int $requests = 0; + + public string $lastBody = ''; + + public function __construct(private readonly ResponseInterface $response) + { + } + + public function sendRequest(RequestInterface $request): ResponseInterface + { + $this->requests++; + $this->lastBody = (string) $request->getBody(); + + return $this->response; + } +} diff --git a/tests/unit/Autogravity/GravityTest.php b/tests/unit/Autogravity/GravityTest.php new file mode 100644 index 0000000000..3e06f332c7 --- /dev/null +++ b/tests/unit/Autogravity/GravityTest.php @@ -0,0 +1,45 @@ +expectException(Exception::class); + $this->expectExceptionMessage('Autogravity returned an invalid gravity'); + + new Gravity(1.1, 0.5); + } + + /** + * @return \Iterator + */ + public static function rotationProvider(): \Iterator + { + yield 'clockwise' => [90, 0.8, 0.75]; + yield 'upside down' => [180, 0.75, 0.2]; + yield 'counterclockwise' => [-90, 0.2, 0.25]; + } + + #[DataProvider('rotationProvider')] + public function testUnrotate(int $rotation, float $expectedX, float $expectedY): void + { + $gravity = (new Gravity(0.25, 0.8))->unrotate($rotation); + + $this->assertEqualsWithDelta($expectedX, $gravity->x, 0.000001); + $this->assertEqualsWithDelta($expectedY, $gravity->y, 0.000001); + } + + public function testGetArrayCopy(): void + { + $this->assertSame(['x' => 0.2, 'y' => 0.8], (new Gravity(0.2, 0.8))->getArrayCopy()); + } +} diff --git a/tests/unit/Docker/Compose/GeneratorTest.php b/tests/unit/Docker/Compose/GeneratorTest.php index 7202fff215..e946d516fc 100644 --- a/tests/unit/Docker/Compose/GeneratorTest.php +++ b/tests/unit/Docker/Compose/GeneratorTest.php @@ -68,6 +68,8 @@ final class GeneratorTest extends TestCase $this->assertArrayHasKey('appwrite-task-scheduler', $compose['services']); $this->assertArrayHasKey('appwrite-task-interval', $compose['services']); $this->assertArrayHasKey('appwrite-embedding', $compose['services']); + $this->assertArrayHasKey('appwrite-autogravity', $compose['services']); + $this->assertSame('ghcr.io/appwrite/autogravity:0.0.6', $compose['services']['appwrite-autogravity']['image']); $this->assertArrayNotHasKey('profiles', $compose['services']['appwrite-worker']); $this->assertArrayNotHasKey('profiles', $compose['services']['appwrite-task-scheduler']); }