Dispatch an event after an image style derivative is saved

Problem/motivation

Core has no hook after ImageStyle::createDerivative() writes the file. Image effects run before the save, and the GD toolkit saves pixels only: imagejpeg(), imagewebp() and imageavif() write no EXIF, XMP or IPTC, and the PHP exif extension only reads. file_mdm writes EXIF for JPEG and TIFF, but the IPTC source type lives in XMP. So anything that marks a derivative has to run after the save.

Proposed resolution

In ai_disclosure_image:

  • Swap the image_style entity class through hook_entity_type_alter() for a subclass whose createDerivative() calls the parent and then dispatches a DerivativeSavedEvent with the style, the original URI and the derivative URI.
  • imageapi_optimize swaps the same class. Extend its class when it is installed, or document the conflict.
  • Derivatives are built on the first request for the style URL, so listeners run inside that request. Keep them fast, and queue slow work such as C2PA signing.
  • File a core feature request for a native event, and drop the swap when it lands.

Remaining tasks

  • Entity class, event class and docs for listeners
  • Kernel test: the event fires once per derivative, and not when the save fails
  • Core feature request

User interface changes

None.

API changes

New event DerivativeSavedEvent.

Data model changes

None.

Part of #56 (closed).

Edited by Giorgio Alfredo Pagano