Use focal point image cropping

This commit is contained in:
Torsten Dittmann committed 2026-09-07 10:07:47 +00:00
1 parent 28b008aa29
commit d4819b58ed
5 files changed
+10 -70

No files matched your search

Generated
+6 -6
View File
@@ -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",
-38
View File
@@ -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) {
@@ -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) {
@@ -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 = [];
-19
View File
@@ -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<string, array{float, float, int, int, int, int, string}>
*/
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);