PHP code example of zoker / responsive-images

1. Go to this page and download the library: Download zoker/responsive-images 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/ */

    

zoker / responsive-images example snippets


// config/responsive-images.php
'driver' => env('RESPONSIVE_IMAGES_DRIVER', extension_loaded('imagick') ? 'imagick' : 'gd'),

'imagick' => [
    'memory' => env('RESPONSIVE_IMAGES_IMAGICK_MEMORY', '256M'),
    'map' => env('RESPONSIVE_IMAGES_IMAGICK_MAP', '512M'),
    'area' => env('RESPONSIVE_IMAGES_IMAGICK_AREA'),   // pixels, e.g. 16000000; null = default
    'disk' => env('RESPONSIVE_IMAGES_IMAGICK_DISK'),   // null = default (unlimited)
],

// config/responsive-images.php
'extensions' => ['jpg', 'jpeg', 'png', 'webp', 'gif', 'avif', 'bmp', 'tif', 'tiff', 'heic', 'heif'],

use Zoker\ResponsiveImages\Facades\ResponsiveImages;

// With specific dimensions
$image = ResponsiveImages::make(
    'uploads/products/image.jpg',
    width: 1200,
    height: 800,
    disk: 'public',
);

// Or use original image dimensions
$image = ResponsiveImages::make(
    'uploads/products/image.jpg',
    disk: 'public',
);

// Default: lazy loading
echo $image->toHtml('Product image');

// For hero images: eager loading
echo $image->toHtml('Hero image', 'eager');

$image = ResponsiveImages::make('uploads/products/image.jpg', width: 1200, height: 800);

// Get all generated images as [width => url] array
$image->getImages();
// e.g. [320 => 'https://...', 640 => 'https://...', 1200 => 'https://...']

// Get the URL of the closest generated image to the given width
// Prefers equal or larger sizes; falls back to largest available if all are smaller.
// Returns $src when nothing has been generated (e.g. SVG or before the job has run)
$image->getImage(500);
// returns URL of the nearest generated size (e.g. 640px version)

// Get srcset string
$image->getSrcset();
// e.g. "https://.../image-320-abc.webp 320w, https://.../image-640-def.webp 640w, ..."

// Whether a <source> is rendered (generated images in the output format exist)
$image->hasSource();

// Render as HTML <picture> element
$image->toHtml('Alt text');
$image->toHtml('Alt text', 'eager');
$image->toHtml('Alt text', 'lazy', ['class' => 'my-image']);
// Override the sizes attribute (the image itself is not affected, so it is not part of the cache key)
$image->toHtml('Alt text', 'lazy', [], '(min-width: 1024px) 33vw, 100vw');
bash
php artisan vendor:publish --tag="responsive-images-config"
bash
# Clear all
php artisan responsive-images:clear

# Clear for specific file
php artisan responsive-images:clear uploads/products/image.jpg