@props([
'src' => '',
'alt' => '',
'width' => null,
'height' => null,
'class' => '',
'sizes' => '100vw',
'widths' => [],
'base' => null,
'lazy' => true,
'priority' => false,
])
@php
/**
* Optimized static picture:
* - base: path prefix under public/ without extension, e.g. images/hero/optimized/assistant-woman
* - widths: [480, 960]
* Falls back to $src when optimized files are missing OR when $src is a custom
* media-library / storage upload (admin gallery pick must always win).
*/
$widths = array_values(array_filter(array_map('intval', (array) $widths)));
sort($widths);
$webpSrcset = [];
$jpgSrcset = [];
$fallback = $src;
$srcString = trim((string) $src);
$isCustomMedia = $srcString !== '' && (
str_contains($srcString, '/storage/')
|| str_contains($srcString, '/media/')
|| (bool) preg_match('#^https?://#i', $srcString) && ! str_contains($srcString, '/images/')
);
// Only use static optimized variants when src is empty or still the default /images asset.
$useOptimizedBase = $base && $widths && ! $isCustomMedia;
if ($useOptimizedBase) {
foreach ($widths as $w) {
$webpRel = ltrim($base, '/')."-{$w}.webp";
$jpgRel = ltrim($base, '/')."-{$w}.jpg";
$webpAbs = public_path($webpRel);
$jpgAbs = public_path($jpgRel);
if (is_file($webpAbs)) {
$webpSrcset[] = asset($webpRel).' '.$w.'w';
}
if (is_file($jpgAbs)) {
$jpgSrcset[] = asset($jpgRel).' '.$w.'w';
// Prefer admin/CMS $src when it is a static default; only override fallback
// when src is empty or clearly the same static family.
if ($srcString === '' || str_contains($srcString, ltrim((string) $base, '/')) || str_contains($srcString, '/images/')) {
$fallback = asset($jpgRel);
}
}
}
// If CMS picked a different static path under /images/, keep that src as fallback.
if ($srcString !== '' && str_contains($srcString, '/images/') && ! str_contains($srcString, ltrim((string) $base, '/'))) {
$webpSrcset = [];
$jpgSrcset = [];
$fallback = $srcString;
}
}
@endphp
@if($webpSrcset || $jpgSrcset)
@if($webpSrcset)
@endif
@if($jpgSrcset)
@endif
except(['src', 'alt', 'width', 'height', 'class', 'sizes', 'widths', 'base', 'lazy', 'priority']) }}
>
@else
except(['src', 'alt', 'width', 'height', 'class', 'sizes', 'widths', 'base', 'lazy', 'priority']) }}
>
@endif