An image as a separate asset: identity, originals, and derivatives
Model original replacement without losing historical references: a stable media asset, byte revisions, derivative recipes, and readiness checks before switching the current version.
Imagine a cover image appearing in a large hero area, a small card, and a square collection. There are three files on disk. To the editor, this is still one image. Once the original is replaced, the system must answer a harder question: which files belong to the new version, and which should remain in a historical publication?
The Media module runs in memory inside the same application. Originals and derivatives are synthetic strings: it performs no decoding, resizing, upload, CDN delivery or operations against real R2 storage.
A name such as cover-small.webp does not explain that. It describes a location or an approximate size, but not provenance. We will separate the media asset, source revision, and derivative file in an independent teaching model. It helps define replacement and deletion rules; processing photographs, uploading them, and delivering them over a network require separate implementations.
One asset, several identities
The media asset asset-7 is a stable logical object. Several articles can use it without turning every size into a separate editorial item. A source revision identifies specific immutable content. A derivative combines that revision with an exact transformation recipe.
| Entity | Question it answers | Example |
|---|---|---|
| Media asset | Which image did the editor select? | asset-7 |
| Source revision | Exactly which bytes were used to prepare the result? | r1 or r2 |
| Recipe | How should the representation be produced? | 800 × 450, WebP, a defined crop |
| Derivative file | What result was created for this revision and recipe? | A reference to the bytes and their fingerprint |
| Usage | How does a particular article select a version and description? | The current revision or a pinned r1 |
The labels r1 and r2 make the explanation easier to follow. In the example code, a revision identifier is the SHA-256 of its synthetic bytes, calculated with PHP's hash() function. Adding the same bytes to the same asset therefore returns the same revision.
This is a deliberate simplification. If repeated uploads of an identical file must be distinguished, those events need their own identifiers. Changing a caption does not necessarily create a new revision of the bytes either. Meanwhile, identical fingerprints in two different assets do not authorize transferring permissions or editorial relationships between them: lookups in the teaching model always include assetId.
Width is not enough for a derivative key
Two files that are 800 pixels wide can differ in height, crop, format, or encoding quality. After an algorithm changes, an old result should not accidentally satisfy a new recipe.
Our Recipe therefore contains width, height, format, crop rule, quality, and implementation revision. We use an ordered set of values to calculate its fingerprint. A derivative key combines the asset identity, source revision, and recipe fingerprint. A complete storage implementation should keep the file location in a separate field; the teaching model stores only the result's fingerprint and implements no file locations.
That key does not have to become a public URL. It describes the identity of a result, not an access policy. Moving files between storage systems should not force an editor to select the image again in every article either.
In the model, registering the same derivative with different bytes raises an error. This helps reveal an incomplete recipe: for example, the library has changed, but its revision in the transformation description has not. A real processing pipeline needs a separate decision about how reproducible its encoder is and which parameters affect the result.
A replacement starts alongside the old original
Suppose r1 is current. Its two required representations are ready: an 800 × 450 card and a 1600 × 900 hero image. The editor uploads a corrected original. This creates r2, but does not yet change the asset's current pointer.
The required derivatives for r2 must be created first. Existing files retain their provenance from r1; attributing them to the new source would conceal a mismatch. If only the card is ready, the hero area does not yet have an accepted new representation.
In our example, activate() checks for every recipe the caller declares mandatory. It rejects an empty list so that accidentally omitted requirements cannot allow activation. The example uses synthetic strings instead of decoded photographs:
$card = new Recipe(800, 450, 'webp', 'center-safe', 85, 'lab-v1');
$hero = new Recipe(1600, 900, 'webp', 'center-safe', 85, 'lab-v1');
$r2 = $model->addSource('asset-7', 'synthetic original two');
$model->registerDerivative('asset-7', $r2, $card, 'synthetic card two');
$model->registerDerivative('asset-7', $r2, $hero, 'synthetic hero two');
$model->activate('asset-7', $r2, [$card, $hero]);
Here, a derivative record represents readiness only inside the model. In a file processing pipeline, a success record must follow the actual creation and verification of an accessible result. Otherwise, a correct metadata table can still point to a missing file.
The article reference determines replacement behavior
An article can refer either to the asset's current revision or to a particular accepted revision. These choices serve different editorial needs. The model allows pinning only a revision that has already passed activate(); its accepted state remains recorded after the current pointer moves to a newer version.
Following the current version can be appropriate for a decorative cover: a corrected crop will appear in all such usages. Pinning a revision is useful for a screenshot that explains a particular interface or experiment. A new image should not silently alter the historical meaning of the explanation.
| Event | A usage that follows the current revision | A usage pinned to r1 |
|---|---|---|
r2 exists; derivatives are not ready |
r1 |
r1 |
| Only some mandatory derivatives are ready | r1 |
r1 |
r2 is accepted after the complete set is checked |
r2 |
r1 |
| The editor explicitly changes the pinned usage | r2 |
The revision selected by the editor |
Changing the current pointer is a separate operation. A multi-process application must coordinate it with concurrent editing: for example, update only if the expected previous revision is still current. The teaching model operates in one process's memory and implements neither transactions nor concurrent writes.
Browsers need representations; editors need an asset
MDN distinguishes resolution switching from changes in cropping: srcset and sizes offer different sizes, while picture offers alternative sources. Files reach the browser after a revision has been selected. Mixing arbitrary results from r1 and r2 in one srcset would make the content depend on the device.
HTML generation should therefore receive a consistent set of derivatives from one selected revision. When a required variant is missing, the product defines its behavior explicitly: retain the previous revision, show an available fallback representation, or decline to show the image. This model chooses the first option at activation time.
Descriptions also belong to a particular usage
A caption can explain editorial context, while alternative text communicates an image's information or function. W3C WAI relates the choice of text alternative to the image's purpose. A purely decorative image may need an empty alt; an absent attribute and an empty value are different cases.
Our Usage contains a locale and alt, so the same file can have Ukrainian and English descriptions in different usages. The filename does not determine that description. General asset information may be inherited, but the precedence of a usage-specific description should be explicit.
Cleaning up old files starts with references
Activating r2 does not necessarily make r1 redundant. A pinned article may still reference it. The model prohibits deleting a current or pinned revision. Once its last pinned usage is removed, a noncurrent revision can be deleted along with its derivatives.
A real system has a broader dependency map: drafts, revision history, galleries, and pending jobs. Deleting metadata and deleting physical bytes can also be separate steps. The condition “no references found in this table” is sufficient only if that table actually covers every relevant usage.
Reproducing the model
Continue the single Symfony 8.1 project, article_demo, on PHP 8.4+. Run commands from its root and preserve earlier routes, services and stores. The added modules use synthetic data and their own teaching context.
Execution should check incomplete readiness of r2, the difference between current and pinned usages, recipe changes, and rejection of derivative overwrites and deletion of a referenced revision. Separate negative cases try to use another asset's revision and to pin a source that has not yet been accepted.
This model gives the editor defined behavior: the asset retains its identity, new bytes follow their own preparation path, and a historical usage changes only through an explicit decision. File dimensions become a property of a representation rather than a substitute for editorial identity.
Add the module to the same application
Add the teaching src/Media/{Recipe,Usage,MediaModel}.php and src/Command/MediaLifecycleCommand.php files to the existing article_demo. The command constructs the model; register only the command. It needs neither another Symfony project nor object storage.
The following extends the existing services block in config/services.yaml. Preserve earlier registrations, imports and _defaults; keep the rest of the file.
services:
App\Command\MediaLifecycleCommand: ~
php bin/console app:media:lifecycle
APP_ENV=test php bin/phpunit tests/Media