PHP code example of fruitcake / laravel-image-variants

1. Go to this page and download the library: Download fruitcake/laravel-image-variants 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/ */

    

fruitcake / laravel-image-variants example snippets


'source' => [
    'disk' => 'public',
    'prefix' => null,
],

Variants::url('uploads/avatar.png', 'thumb');   // storage/app/public/uploads/avatar.png

// config/filesystems.php
'assets' => ['driver' => 'local', 'root' => public_path()],

// config/image-variants.php
'source' => ['disk' => 'assets', 'prefix' => null],

'source' => ['disk' => 'public', 'prefix' => 'uploads'],

Variants::url('avatar.png', 'thumb');   // storage/app/public/uploads/avatar.png

use Fruitcake\ImageVariants\Facades\Variants;

// A named preset from the config
Variants::url('img/bg.jpg', 'hero');
// /storage/variants/hero/9a8c70ca57/bg.jpg?src=img/bg.jpg

// Ad-hoc operations, converting to webp
Variants::url('img/bg.jpg', ['cover' => [600, 500], 'quality' => 80], 'webp');
// /storage/variants/custom/b754ff4bfa/bg.webp?src=img/bg.jpg&cover=600,500&quality=80

// A preset, a nicer filename, and one operation added on top
Variants::url('img/bg.jpg', 'thumb', 'webp', 'team-photo', ['grayscale' => true]);
// /storage/variants/thumb/cb20a26822/team-photo.webp?src=img/bg.jpg&grayscale=1

Variants::url(
    string $src,                       // relative to the configured source disk
    string|array $preset = 'custom',   // a preset name, or operations
    ?string $format = null,            // output extension; defaults to the source's
    ?string $name = null,              // filename to serve as; defaults to the source's
    array $operations = [],            // merged over the preset
);

Variants::srcset('img/bg.jpg', [640, 1024], 'webp');
// /storage/variants/custom/cf6b9805e5/bg.webp?src=img/bg.jpg&scale=640 640w,
// /storage/variants/custom/e27357f48a/bg.webp?src=img/bg.jpg&scale=1024 1024w

Variants::dimensions('img/bg.jpg', 'thumb');   // ['width' => 100, 'height' => 100]

'blade' => ['component' => 'variant'],   // null leaves it unregistered

$twig->addExtension($container->make(\Fruitcake\ImageVariants\Twig\VariantExtension::class));

'presets' => [
    'thumb' => ['cover' => [100, 100], 'quality' => 80],
    'photo' => ['scale' => [800, null], 'quality' => 80],
    'hero'  => ['scale' => [1600, null], 'quality' => 80],
],

'quality' => 80,

'presets' => [
    'original' => ['scale' => [2000, null], 'quality' => false],
],

'cache_store' => null,   // null = default store

'lock' => [
    'enabled' => true,
    'ttl' => 30,   // how long the lock survives a worker that dies holding it
    'wait' => 10,  // how long a queued request waits before generating anyway
],

use Illuminate\Support\Facades\Schedule;

Schedule::command('image-variants:cleanup')->weekly();

'cleanup' => [
    'days' => 30,
],
bash
php artisan storage:link
bash
php artisan vendor:publish --tag=image-variants-config
nginx
location ^~ /storage/variants/ {
    expires max;
    add_header Cache-Control "public, immutable";
}
apache
<LocationMatch "^/storage/variants/">
    Header set Cache-Control "public, max-age=31536000, immutable"
</LocationMatch>
html
<img src="…/bg.webp?src=img/bg.jpg&amp;scale=1600"
     srcset="…scale=640 640w, …scale=1024 1024w, …scale=1600 1600w"
     sizes="(max-width: 900px) 100vw, 900px"
     width="1600" height="1067" alt="Our office">
bash
php artisan image-variants:cleanup --accessed --days=90