Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resources/models/*.onnx filter=lfs diff=lfs merge=lfs -text
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/vendor/
/.transformers-cache/
/examples/focus-crop/
/.vscode/
.phpunit.result.cache
.idea
Expand Down
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ $target = 'image_100x100.jpg';
$image->crop(100, 100, Image::GRAVITY_TOP_LEFT);
$image->save($target, 'jpg', 100);

//automatically keep salient subjects in frame
$image = new Image(\file_get_contents('image.jpg'));
$image->crop(400, 300, Image::GRAVITY_AUTO);
$image->save('automatic.jpg', 'jpg', 100);

$image = new Image(\file_get_contents('image.jpg'));
$target = 'image_border.jpg';
$image->setBorder(2, "#ff0000"); //add border 2 px, red
Expand All @@ -43,9 +48,52 @@ $image->save($target, 'png', 100);

```

### Automatic Cropping

Use `Image::GRAVITY_AUTO` to position the crop around the most visually salient parts of an image:

```php
$image->crop(400, 300, Image::GRAVITY_AUTO);
```

Automatic cropping uses the bundled full U2NET saliency model. Empty or uniform saliency maps fall back to a centered crop. Applications do not need to download or configure a model.

Detection can run separately in a worker and be stored as JSON. The result contains the normalized saliency mask and its dimensions. Passing it to `crop()` skips model inference in the image worker:

```php
// Detection worker
$image = new Image(\file_get_contents('image.jpg'));
$detectionJson = json_encode($image->detect(), JSON_THROW_ON_ERROR);
// Store $detectionJson in the database.

// Image worker
$image = new Image(\file_get_contents('image.jpg'));
$detection = json_decode($detectionJson, true, flags: JSON_THROW_ON_ERROR);
$image->crop(400, 300, Image::GRAVITY_AUTO, $detection);
```

ONNX Runtime is installed per platform. Add its verified download hook to the root `composer.json` of the application using this library:

```json
{
"scripts": {
"post-install-cmd": "OnnxRuntime\\Vendor::check",
"post-update-cmd": "OnnxRuntime\\Vendor::check"
}
}
```

Then run:

```bash
composer install
```

The provided Linux runtime targets glibc. Alpine and other musl-based systems require a compatible custom ONNX Runtime library.

## System requirements

Utopia Image requires PHP 8.1 or later. We recommend using the latest PHP version whenever possible.
Utopia Image requires PHP 8.1 or later with the Imagick, GD, and FFI extensions. We recommend using the latest PHP version whenever possible.

## Testing

Expand Down
16 changes: 14 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@
}
},
"scripts": {
"test": "phpunit --testsuite unit"
"lint": "./vendor/bin/pint --test",
"format": "./vendor/bin/pint",
"check": "./vendor/bin/phpstan analyse --level max src tests --memory-limit=512M",
"test": "phpunit --testsuite unit",
"post-install-cmd": "OnnxRuntime\\Vendor::check",
"post-update-cmd": "OnnxRuntime\\Vendor::check"
},
"require": {
"php": ">=8.1",
"ext-ffi": "*",
"ext-imagick": "*",
"ext-gd": "*"
"ext-gd": "*",
"ankane/onnxruntime": "^0.2.9"
},
"require-dev": {
"phpunit/phpunit": "10.5.*",
"phpstan/phpstan": "2.1.*",
"laravel/pint": "1.24.*"
},
"suggest": {
"ext-imagick": "Imagick extension is required for Imagick adapter"
Expand Down
14 changes: 14 additions & 0 deletions resources/models/NOTICE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# U2NET Model

`u2net.onnx` is an ONNX export of the full U2NET model from the U-2-Net project:

- Source: https://github.com/xuebinqin/U-2-Net
- ONNX distribution: https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx
- MD5: `60024c5c889badc19c04ad937298a77b`
- SHA-256: `8d10d2f3bb75ae3b6d527c77944fc5e7dcd94b29809d47a739a7a728a912b491`
- License: Apache License 2.0

Copyright 2020 Xuebin Qin.

Licensed under the Apache License, Version 2.0. You may obtain a copy at
https://www.apache.org/licenses/LICENSE-2.0.
3 changes: 3 additions & 0 deletions resources/models/u2net.onnx
Git LFS file not shown
Loading