spatie/statamic-responsive-images
Statamic addon that generates responsive images and variants on demand. Includes a “responsive” fieldtype for art-directed images with configurable breakpoints, ratios, and fit options, plus an Antlers tag to render the correct sources for each breakpoint.
Installation
composer require spatie/statamic-responsive-images
Publish the config (optional):
php artisan vendor:publish --provider="Spatie\ResponsiveImages\ServiceProvider" --tag="config"
Fieldtype Integration
fields:
responsive_image:
type: spatie/responsive_images
display: "Responsive Image"
config/responsive-images.php (e.g., set default breakpoints).First Use Case
{{ entry.responsive_image|responsive_image }}
<picture> element with optimized srcset and sizes attributes.Art Direction
config/responsive-images.php:
'breakpoints' => [
'sm' => 640,
'md' => 1024,
'lg' => 1440,
],
Dynamic Image Generation
responsive_image Twig filter:
{{ entry.hero_image|responsive_image({
'breakpoints': ['sm', 'md', 'lg'],
'sizes': '(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 33vw',
'fallback': entry.hero_image.default
}) }}
Integration with Statamic Assets
assets container.Caching and Performance
'cache' => [
'enabled' => true,
'driver' => 'file', // or 'redis'
],
php artisan responsive-images:regenerate
Custom Directives
{{ entry.responsive_image|responsive_image({
'directives': ['loading="lazy"']
}) }}
Breakpoint Mismatch
sizes attribute in Twig matches the breakpoints in config. Mismatches cause unnecessary image loads.breakpoints: ['sm', 'md'], omit lg from sizes unless needed.Asset Container Paths
assets/images) in config breaks if containers are renamed. Use Statamic’s asset() helper or dynamic paths:
'asset_container' => 'assets/images',
Cache Invalidation
php artisan statamic:clear) doesn’t reset responsive image cache. Use:
php artisan responsive-images:clear-cache
Fieldtype Configuration Overrides
breakpoints) take precedence over global config. Test thoroughly.Log Breakpoints
Add this to AppServiceProvider to debug generated breakpoints:
Spatie\ResponsiveImages\ResponsiveImage::macro('debug', function () {
return [
'breakpoints' => $this->breakpoints,
'sizes' => $this->sizes,
'srcset' => $this->srcset,
];
});
Usage:
{{ entry.responsive_image|responsive_image|debug }}
Check Disk Permissions
If images fail to generate, verify storage/app/responsive-images is writable:
chmod -R 755 storage/app/responsive-images
Custom Image Processing
Spatie\ResponsiveImages\Image class to add filters (e.g., blur effects):
namespace App\Extensions;
use Spatie\ResponsiveImages\Image;
class CustomImage extends Image {
public function blur($amount) {
return $this->applyFilter('blur', $amount);
}
}
AppServiceProvider:
Spatie\ResponsiveImages\ResponsiveImage::macro('custom', function () {
return new CustomImage($this->asset);
});
WebP Fallback
responsive_image filter:
{{ entry.responsive_image|responsive_image({
'formats': ['webp', 'jpg'],
'fallback_format': 'jpg'
}) }}
Statamic Entry Events
use Spatie\ResponsiveImages\Facades\ResponsiveImage;
Event::listen(EntryUpdated::class, function ($event) {
if ($event->entry->has('responsive_image_field')) {
ResponsiveImage::regenerate($event->entry->responsive_image_field->value);
}
});
Custom Storage
'storage' => [
'path' => 'custom/path/responsive-images',
'disk' => 's3', // Use AWS S3, etc.
],
How can I help you explore Laravel packages today?