spatie/statamic-responsive-images
Statamic addon by Spatie that adds a responsive image fieldtype with art direction and breakpoint support. Automatically generates responsive variants on asset upload and renders them in Antlers via the responsive tag, with configurable breakpoints and options.
Installation:
composer require spatie/statamic-responsive-images
No additional configuration is required for basic usage.
Fieldtype Usage:
Add the responsive fieldtype to your Statamic blueprint:
fields:
hero_image:
type: responsive
display: Hero Image
container: assets
allow_uploads: true
First Use Case: Render the image in your Twig template:
{{ responsive:hero_image }}
This generates responsive <img> tags with srcset and sizes attributes, optimized for different viewport sizes.
Art Direction:
Define breakpoints in config/responsive-images.php (default: Tailwind breakpoints):
'breakpoints' => ['sm', 'md', 'lg', 'xl', '2xl'],
Use breakpoint-specific parameters in Twig:
{{ responsive:hero_image ratio="16/9" lg:ratio="4/3" }}
Dynamic Asset Swapping: Override assets per breakpoint:
{{ responsive:hero_image :lg:src="desktop_hero" :2xl:src="large_desktop_hero" }}
Quality Control: Set global quality in config:
'quality' => [
'webp' => 80,
'avif' => 75,
],
Override per breakpoint or format:
{{ responsive:hero_image quality:webp="50" md:quality:webp="75" }}
Glide Integration:
Pass Glide parameters with glide: prefix:
{{ responsive:hero_image glide:width="1200" glide:blur="10" }}
Note: glide:width acts as a max-width to prevent oversized images.
Placeholder Handling: Disable placeholders for performance-critical images:
{{ responsive:hero_image placeholder="false" }}
Format Control: Toggle WebP/AVIF globally or per tag:
{{ responsive:hero_image webp="true" avif="false" }}
Or in config/responsive-images.php:
'webp' => true,
'avif' => env('ENABLE_AVIF', false),
HTML Attributes: Add custom attributes dynamically:
{{ responsive:hero_image alt="{title}" class="lazyload" loading="lazy" }}
GraphQL: Fetch responsive data in GraphQL:
{
entries {
data {
hero_image {
responsive(ratio: "16/9") {
sources {
srcSet
mediaString
}
}
}
}
}
}
Asset Caching:
statamic.assets.image_manipulation.cache (recommended) means the first load generates images on-demand, which can be slow.responsive:regenerate command:
php please responsive:regenerate
Note: Only works if statamic.assets.image_manipulation.cache is true.Breakpoint Mismatch:
config/responsive-images.php and Twig templates. Example:
{{ responsive:hero_image lg:ratio="16/9" }} {# Ensure 'lg' exists in config #}
Queue Delays:
'queue' => env('QUEUE_CONNECTION', 'sync'), {# Force sync for testing #}
Placeholder Artifacts:
placeholder="false" or ensure consistent ratios across breakpoints.Excluded Containers:
excluded_containers in config:
'excluded_containers' => ['pdfs', 'videos'],
View Generation: Publish views for customization:
php artisan vendor:publish --provider="Spatie\ResponsiveImages\ServiceProvider"
Modify resources/views/vendor/responsive-images/default.blade.php.
Log Breakpoints: Debug breakpoint logic by logging the active breakpoint in Twig:
{{ dump(responsive_breakpoint) }}
Glide Parameters: Validate Glide parameters with:
{{ responsive:hero_image glide:width="invalid" }} {# Will throw an error #}
Dimension Calculator:
dimension_calculator_threshold in config to control when images are regenerated:
'dimension_calculator_threshold' => 0.1, {# Default: 0.1 #}
0.01) regenerate more frequently but reduce storage.AVIF Fallback:
{{ responsive:hero_image avif="true" fallback="webp" }}
Live Preview:
'live_preview' => true,
Custom Breakpoints: Extend breakpoints dynamically via a service provider:
Spatie\ResponsiveImages\ResponsiveImages::extendBreakpoints(function () {
return ['custom' => 1024];
});
Job Queues: Override the default queue for specific containers:
'queues' => [
'default' => 'default',
'hero_images' => 'high',
],
Then tag images:
{{ responsive:hero_image queue="hero_images" }}
Asset Events: Listen for asset uploads to trigger custom logic:
use Spatie\ResponsiveImages\Events\ResponsiveImageGenerated;
ResponsiveImageGenerated::listen(function ($event) {
// Log or process generated images
});
GraphQL Extensions:
Add custom fields to GraphQL responses by extending the ResponsiveImage type in a service provider.
How can I help you explore Laravel packages today?