PHP code example of contenir / contenir-storage

1. Go to this page and download the library: Download contenir/contenir-storage library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

contenir / contenir-storage example snippets


use Contenir\Storage\Config\StorageConfig;
use Contenir\Storage\Image\ImageResizer;
use Contenir\Storage\UploadInput;

$manager = StorageConfig::fromArray($config['storage'] ?? null, new ImageResizer(), '/var/www/public');
$storage = $manager->primary();

$entry = $storage->store(UploadInput::fromFilesArray($_FILES['image']), 'asset/library/news');

$storage->url($entry->path);                     // "/asset/library/news/photo.jpg"
$storage->url($entry->path, 'admin-thumb');      // variant URL, or null when not materialised
$storage->thumbnailUrl($entry->path);            // the same, null when the variant is undeclared
$storage->variantUrls($entry->path, 'card-480'); // ['avif' => …, 'source' => …], no existence checks

use Contenir\Storage\Adapter\LocalFilesystem;
use Contenir\Storage\Image\ImageResizer;
use Contenir\Storage\StorageManager;
use Contenir\Storage\Variant;
use Contenir\Storage\VariantFit;
use Contenir\Storage\VariantRegistry;

$variants = new VariantRegistry(
    new Variant('admin-thumb', 180, 180, VariantFit::Contain),
    new Variant('card', 600, 400, VariantFit::Cover, formats: ['avif', 'webp'], quality: 80),
);

$manager = new StorageManager();
$manager->register('local', new LocalFilesystem(
    rootPath: '/var/www/public',
    publicPath: '',
    variants: $variants,
    resizer: new ImageResizer(),
), isPrimary: true);