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/ */
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');