Skip to content
Draft
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
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI
on:
pull_request: ~
push:
branches: [1.x, 'codex/image-1.3']
branches: [2.x, 'codex/image-2.x']
tags: ['*']
schedule:
- cron: '0 13 * * MON,TUE'
Expand All @@ -19,11 +19,11 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
php: ['8.3', '8.4', '8.5']
php: ['8.4', '8.5']
dependencies: [highest]
include:
- {os: ubuntu-latest, php: '8.3', dependencies: lowest}
- {os: windows-latest, php: '8.3', dependencies: lowest}
- {os: ubuntu-latest, php: '8.4', dependencies: lowest}
- {os: windows-latest, php: '8.4', dependencies: lowest}
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
Expand All @@ -37,7 +37,7 @@ jobs:
- run: composer validate --strict
- run: composer unit-tests
- name: Run consumer with local version override
run: php examples/consumer/install.php 1.3.99
run: php examples/consumer/install.php 2.0.99
- run: php examples/consumer/run.php

imagick:
Expand Down
106 changes: 19 additions & 87 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Contao image library
====================

[![](https://img.shields.io/github/actions/workflow/status/contao/image/ci.yml?branch=1.x&style=flat-square)](https://github.com/contao/image/actions?query=branch%3A1.x)
[![](https://img.shields.io/codecov/c/github/contao/image/1.x.svg?style=flat-square)](https://codecov.io/gh/contao/image)
[![](https://img.shields.io/github/actions/workflow/status/contao/image/ci.yml?branch=2.x&style=flat-square)](https://github.com/contao/image/actions?query=branch%3A2.x)
[![](https://img.shields.io/codecov/c/github/contao/image/2.x.svg?style=flat-square)](https://codecov.io/gh/contao/image)
[![](https://img.shields.io/packagist/v/contao/image.svg?style=flat-square)](https://packagist.org/packages/contao/image)
[![](https://img.shields.io/packagist/dt/contao/image.svg?style=flat-square)](https://packagist.org/packages/contao/image)

Expand All @@ -20,94 +20,26 @@ php composer.phar require contao/image
Usage
-----

### Simple resize:
PHP 8.4 or later is required. Clear the complete image cache when migrating to 2.0.

```php
$imagine = new \Imagine\Gd\Imagine();
$resizer = new Resizer('/path/to/cache/dir');
$image = new Image('/path/to/image.jpg', $imagine);

$config = (new ResizeConfiguration())
->setWidth(100)
->setHeight(100)
->setMode(ResizeConfiguration::MODE_CROP)
;

$options = (new ResizeOptions())
->setImagineOptions([
'jpeg_quality' => 95,
'interlace' => \Imagine\Image\ImageInterface::INTERLACE_PLANE,
])
->setBypassCache(true)
->setTargetPath('/custom/target/path.jpg')
;

$resizedImage = $resizer->resize($image, $config, $options);

$resizedImage->getPath(); // /custom/target/path.jpg
$resizedImage->getUrl('/custom/target'); // path.jpg
$resizedImage->getUrl('/custom/target', 'https://example.com/'); // https://example.com/path.jpg
$resolver = new Contao\Image\FilesystemSourceResolver(['uploads' => '/path/to/uploads']);
$factory = new Contao\Image\FilesystemImageFactory(
new Contao\Image\FilesystemImageConfiguration('/path/to/cache', '/images', $secret),
$resolver,
new Contao\Image\ImagineImageDecoderEncoder(new Imagine\Gd\Imagine())
);
$source = $resolver->resolve(new Contao\Image\SourceReference('uploads', 'photo.jpg'));
$result = $factory->getResizer()->resize(
$source,
(new Contao\Image\ResizeConfiguration())->withWidth(640),
new Contao\Image\ProcessingOptions()
);
echo $result->getUrl();
```

### Responsive image:

```php
$imagine = new \Imagine\Gd\Imagine();

$resizer = new Resizer('/path/to/cache/dir');
$pictureGenerator = new PictureGenerator($resizer);
$image = new Image('/path/to/image.jpg', $imagine);

$config = (new PictureConfiguration())
->setSize((new PictureConfigurationItem())
->setResizeConfig((new ResizeConfiguration())
->setWidth(100)
->setHeight(100)
->setMode(ResizeConfiguration::MODE_CROP)
)
->setDensities('1x, 2x')
->setSizes('100vw')
)
->setSizeItems([
(new PictureConfigurationItem())
->setResizeConfig((new ResizeConfiguration())
->setWidth(400)
->setHeight(200)
->setMode(ResizeConfiguration::MODE_CROP)
)
->setDensities('1x, 2x')
->setSizes('100vw')
->setMedia('(min-width: 900px)')
])
;

$options = (new ResizeOptions());
$picture = $pictureGenerator->generate($image, $config, $options);

$picture->getImg('/path/to');
/* [
'src' => 'cache/dir/4/image-de332f09.jpg',
'width' => 100,
'height' => 100,
'srcset' => 'cache/dir/4/image-de332f09.jpg 100w, cache/dir/4/image-9e0829dd.jpg 200w',
'sizes' => '100vw',
] */

$picture->getSources('/path/to', 'https://example.com/');
/* [
[
'src' => 'https://example.com/cache/dir/c/image-996db4cf.jpg',
'width' => 400,
'height' => 200,
'srcset' => 'https://example.com/cache/dir/c/image-996db4cf.jpg 400w, https://example.com/cache/dir/2/image-457dc5e0.jpg 800w',
'sizes' => '100vw',
'media' => '(min-width: 900px)',
],
] */
```
See the [API guide](docs/image-api.md), [migration guide](docs/migration-2.0.md),
[storage contract](docs/storage.md), [format matrix](docs/formats.md) and
[validation results](docs/validation.md). A runnable consumer supports both 1.3 and 2.0.

[1]: https://contao.org

## Shared image API

The new API is available in 1.3 and retained in 2.0. See the [API guide](docs/image-api.md), [migration guide](docs/migration-2.0.md), [storage contract](docs/storage.md) and [format matrix](docs/formats.md). The legacy API below remains supported in 1.3.
14 changes: 3 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,22 @@
}
],
"require": {
"php": "^8.3",
"php": "^8.4",
"ext-dom": "*",
"ext-gd": "*",
"ext-hash": "*",
"ext-json": "*",
"ext-zlib": "*",
"imagine/imagine": "^1.5.4",
"symfony/deprecation-contracts": "^2.1 || ^3.0",
"symfony/filesystem": "^5.4 || ^6.0 || ^7.0 || ^8.0",
"symfony/polyfill-php73": "^1.27",
"symfony/polyfill-php80": "^1.27",
"symfony/filesystem": "^7.4.18 || ^8.0",
"ext-xml": "*",
"ext-fileinfo": "*",
"contao/imagine-svg": "^1.0.4"
},
"conflict": {
"contao/imagine-svg": "<1.0 || >=2.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4",
"contao/test-case": "^4.5 <4.13.51",
"phpunit/phpunit": "^9.6.37",
"symfony/phpunit-bridge": "^7.4.18",
"symfony/polyfill-php81": "^1.27"
"symfony/phpunit-bridge": "^7.4.18"
},
"suggest": {
"ext-exif": "To support EXIF auto-rotation and preserve copyright metadata"
Expand Down
47 changes: 47 additions & 0 deletions docs/benchmark-2.x.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"php": "8.4.26",
"os": "Darwin",
"iterations": 200,
"jpg": {
"new": {
"median_us": 798.21,
"p95_us": 859.54,
"source_content_opens": 0,
"target_content_operations": [],
"information_operations_per_picture": {
"get": 4
},
"record_operations_per_picture": {
"lockSource": 4,
"getLifecycle": 4,
"lockTarget": 3,
"get": 3
}
},
"source_shared_lock": {
"median_us": 26.38,
"p95_us": 35.04
}
},
"svg": {
"new": {
"median_us": 323.08,
"p95_us": 395.67,
"source_content_opens": 0,
"target_content_operations": [],
"information_operations_per_picture": {
"get": 2
},
"record_operations_per_picture": {
"lockSource": 2,
"getLifecycle": 2,
"lockTarget": 1,
"get": 1
}
},
"source_shared_lock": {
"median_us": 25.33,
"p95_us": 29.79
}
}
}
2 changes: 1 addition & 1 deletion docs/image-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $result = $factory->getResizer()->resize(
echo $result->getUrl();
```

The URL prefix must serve the factory's `<cacheDirectory>/image-v2/content` directory. Keep the secret stable and private. A changed secret or output-affecting backend namespace creates different target identities. A changed URL prefix does not.
The URL prefix must serve the factory's `<cacheDirectory>/image-v2/content` directory. Keep the secret stable and private. A changed secret or output-affecting backend namespace creates different target identities. A changed URL prefix does not. Change `withNamespace()` when replacing a codec, changing its driver or changing output-affecting backend configuration.

`SourceReference` identifies a storage and key. `SourceVersion` adds an opaque version token. Neither stores credentials or resources. Resolve credentials through your application services. Sources open a fresh readable stream for each `openStream()` call and transfer ownership to the caller. The codec similarly transfers ownership of its output stream. Close streams in `finally` blocks.

Expand Down
30 changes: 24 additions & 6 deletions docs/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ The inspected starting point is `06cafbc`. The additive implementation is on `co

| Environment | Result |
| --- | --- |
| PHP 8.3.35, GD and Imagick | 379 tests, 2,601 assertions |
| PHP 8.4.20, GD | 379 tests, 2,068 assertions, Imagick test skipped |
| PHP 8.5, GD and Imagick | 379 tests, 2,601 assertions |
| Lowest dependencies, PHP 8.3 | 379 tests, 2,601 assertions |
| PHP 8.3.35, GD and Imagick | 380 tests, 2,604 assertions |
| PHP 8.4.26, GD | 380 tests, 2,071 assertions, Imagick test skipped |
| PHP 8.5.5, GD and Imagick | 380 tests, 2,604 assertions |
| Lowest dependencies, PHP 8.3 | 380 tests, 2,604 assertions |
| PHPStan 1.12.34 on PHP 8.4 | No errors |
| ECS non-mutating check | No errors |
| ECS non-mutating check on PHP 8.3 | No errors |
| Composer strict validation | Passed |
| actionlint 1.7.7 | Passed |
| Local Composer consumer override 1.3.99 | Passed, five real transformations |

The repaired PHP 8.3 runtime works locally. Compatible minimum dependency versions are declared explicitly. The old PHPStan runtime crash and the 13 ECS findings are resolved. The legacy suite still prints Symfony bridge's non-failing “THE ERROR HANDLER HAS CHANGED!” diagnostic at shutdown, with exit status zero. The shared-engine test groups do not produce that diagnostic.

Coverage includes exact legacy filename and JSON fixtures, 54 preserved calculation cases, stream inspection and ownership, unknown adapter options and precedence, copyright and orientation, SVG/SVGZ, source mutation, pending/completed recovery, exports, revisions and chaining, lifecycle restoration, corrupt records, and a URL-only backend. Process barriers cover cold inspection, duplicate registration, blocking/non-blocking workers, process death, publication-before-completion failure and pruning interrupted during deletion. These tests use independent local processes and shared filesystem storage, not an untested distributed lease implementation.
Coverage includes exact legacy filename and JSON fixtures, 54 preserved calculation cases, stream inspection and ownership, unknown adapter options and precedence, copyright and orientation with pixel-level verification for custom metadata readers, SVG/SVGZ, source mutation, pending/completed recovery, exports, revisions and chaining, lifecycle restoration, corrupt records, and a URL-only backend. Process barriers cover cold inspection, duplicate registration, blocking/non-blocking workers, process death, publication-before-completion failure and pruning interrupted during deletion. These tests use independent local processes and shared filesystem storage, not an untested distributed lease implementation.

Linux and Windows PHP 8.3–8.5 matrices, lowest dependency jobs and an Imagick coverage job are configured. Hosted jobs were not run because this task does not push changes. Local checks ran on macOS.

Expand All @@ -36,3 +36,21 @@ Both new warm paths perform **zero source-content opens and zero target-content
The new local path has higher latency than the legacy baseline. Lifecycle locking and retained JSON records introduce work that the legacy path does not perform. The lock-only measurements quantify part of that cost. The full difference also includes JSON reads, validation and identity calculation, so it must not be attributed entirely to locks. Legacy source byte counts are not instrumented by this benchmark. Its legacy numbers are end-to-end latency measurements.

Stat-derived versions retain the documented limitations around concurrent in-place edits. Production service adapters, signed URL expiry handling, automatic driver fallback and core-bundle integration remain outside this implementation.

## 2.x local results

The shared consumer runs unchanged using the 2.0.99 local Composer override. The branch requires PHP 8.4 and Symfony Filesystem 7.4.18 or 8.x. Earlier Filesystem releases were excluded because the lowest-dependency run exposed PHP 8.4 deprecations.

| Environment | Result |
| --- | --- |
| PHP 8.4.26, GD | 189 tests, 753 assertions, Imagick test skipped |
| PHP 8.5.5, GD and Imagick | 189 tests, 758 assertions |
| Lowest dependencies, PHP 8.4 | 189 tests, 753 assertions, Imagick test skipped |
| PHPStan 1.12.34 | No errors |
| ECS with Contao configuration 6.14.2 | No errors or tool-runtime deprecations |
| Composer strict validation and actionlint | Passed |
| Local Composer consumer override 2.0.99 | Passed, five real transformations |

The smaller suite removes tests for APIs removed in 2.0. Shared calculations retain 54 golden cases, and six responsive-picture scenarios are ported directly from legacy expectations. Metadata regressions remain. The legacy deprecation extension is removed, and the 2.x suite exits without its error-handler diagnostic. Linux and Windows PHP 8.4–8.5 and lowest-dependency jobs are configured but have not run remotely.

`benchmark-2.x.json` contains the PHP 8.4 measurements. Both raster and SVG warm hits again have zero source-content opens and zero target-content operations. Operation counts match the 1.3 shared engine. Compare the historical 1.3 report for the legacy baseline because the legacy classes no longer exist on 2.x.
5 changes: 4 additions & 1 deletion examples/consumer/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
http_response_code(404);
} elseif ($result instanceof ReadableImageResultInterface) {
$stream = $result->openStream();
header('Content-Type: '.('jpg' === $result->getFormat() ? 'image/jpeg' : 'image/'.$result->getFormat()));
$format = $result->getFormat();
$mime = match ($format) { 'jpg' => 'image/jpeg', 'svg', 'svgz' => 'image/svg+xml', default => 'image/'.$format };
header('Content-Type: '.$mime);
if ('svgz' === $format) { header('Content-Encoding: gzip'); }
try { fpassthru($stream); } finally { fclose($stream); }
} else {
header('Location: '.$result->getUrl(), true, 302);
Expand Down
16 changes: 0 additions & 16 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,3 @@ parameters:
paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests

ignoreErrors:
-
# Ignore the forward compatible phpDoc comment
message: '#PHPDoc tag @param references unknown parameter:#'
path: %currentWorkingDirectory%/src/Resizer.php
-
# Ignore the forward compatible phpDoc comment
message: '#PHPDoc tag @param references unknown parameter:#'
path: %currentWorkingDirectory%/src/DeferredResizer.php
-
message: '#class Imagine\\Image\\(Box|Point) constructor expects int, float given#'
path: %currentWorkingDirectory%/tests/ResizeCoordinatesTest.php
-
message: '#ResizeCoordinates::isEqualTo\(\) expects .*, stdClass given#'
path: %currentWorkingDirectory%/tests/ResizeCoordinatesTest.php
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,4 @@
<directory>./tests</directory>
</testsuite>
</testsuites>
<extensions>
<extension class="Contao\Image\Tests\PhpunitExtension\DeprecatedClasses"/>
</extensions>
</phpunit>
29 changes: 0 additions & 29 deletions src/DeferredImage.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/DeferredImageInterface.php

This file was deleted.

Loading