Search by

contenir / contenir-asset

peptolab

Asset records, storage, image derivatives and view helpers for laminas-mvc and Mezzio, on contenir-db-model

v2.0.0-RC2 2026-10-23 01:28 UTC

README

Asset records, file storage, image derivatives and view helpers for laminas-mvc and Mezzio applications, built on contenir-db-model 2.x. The package itself needs no framework: it registers through a ConfigProvider (Mezzio) or Module (laminas-mvc), and the resize route for the legacy .WxH scheme comes from the matching adapter, contenir-asset-laminas-mvc or contenir-asset-mezzio.

  • AssetManager. Create, store, update, sequence and delete asset records and their files.
  • Image derivatives. A large copy and a thumbnail are generated through ImageMagick on upload.
  • View helpers:
    • asset(), assetUrl(), assetSize(), assetAspect() and assetSizes();
    • the legacy assetSrcSet(), with its on-demand resizer.

Status: 2.0 pre-release. 2.0 is not compatible with 1.x. See UPGRADE-2.0.md.

Requirements

  • PHP 8.3, 8.4 or 8.5
  • contenir/contenir-db-model 2.x and a php-db/phpdb platform package
  • laminas-view 2 (which Mezzio's laminas-view renderer and laminas-mvc both bring)
  • ImageMagick (convert) for image derivatives
  • ext-intl (recommended) for transliterating accented file names

Installation

composer require contenir/contenir-asset:^2.0@RC

The consuming project needs "minimum-stability": "dev" and "prefer-stable": true until php-db/phpdb 0.6.0 is tagged. With the laminas component installer the package registers itself: as the Contenir\Asset module in laminas-mvc, as Contenir\Asset\ConfigProvider in Mezzio. Add the adapter for your framework to get the legacy resize route: contenir/contenir-asset-laminas-mvc or contenir/contenir-asset-mezzio.

At a glance

// Your asset entity: the standard columns come from BaseAssetEntity.
#[Table('asset')]
final class Asset extends BaseAssetEntity
{
    #[Column('entry_id')]
    public ?int $entryId = null;
}

// config/autoload/asset.global.php
return ['asset' => ['entity' => Asset::class]];

// In a controller or service:
$asset = $assetManager->addAsset($entry, $currentUser);             // links entry_id and user_id
$assetManager->storeAsset($asset, $entry, 'image', $_FILES['photo'], '1200x800');
<!-- In a view -->
<img src="<?= $this->escapeHtmlAttr($this->asset($id)?->imageLg ?? "") ?>"
     width="<?= $this->assetSize($asset)['width'] ?? '' ?>">

Documentation

  1. Configuration: every asset option and the reference table schema
  2. AssetManager: creating, storing, updating, sequencing and deleting assets
  3. View helpers: asset, assetUrl, assetSize, assetAspect, assetSizes
  4. Legacy resizer: the deprecated .WxH scheme, assetSrcSet and its resize route

Upgrading from 1.x: UPGRADE-2.0.md. Changes: CHANGELOG.md. llms.txt indexes the docs for LLM tooling.

Development

Mago is required (brew install mago).

composer install
composer check              # format check, lint, static analysis, unit + integration tests
composer cs-fix
composer test               # unit suite
composer test-integration   # SQLite + temporary directory suite (needs gd)

License

BSD-3-Clause. See LICENSE.md.