Skip to content

Commit 793d048

Browse files
committed
Added minimal threshold for image resize
1 parent f80a4d2 commit 793d048

File tree

3 files changed

+55
-36
lines changed

3 files changed

+55
-36
lines changed

config/glide-directive.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@
3232

3333
// Set the default queue to use for generating the images.
3434
'default_queue' => env('STATAMIC_GLIDE_DIRECTIVE_DEFAULT_QUEUE', 'default'),
35+
36+
// Set the threshold width to use for the image source sets.
37+
'image_resize_threshold' => 480
3538
];

resources/views/image.blade.php

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
11
@if($image)
2-
<picture>
3-
@if( $image->extension() == 'svg' || $image->extension() == 'gif')
4-
<img
5-
class="{{ $class }}"
6-
src="{{ $image->url() }}"
7-
alt="{{ $alt }}"
8-
width="{{ $width }}"
9-
height="{{ $height }}"
10-
/>
11-
@else
12-
@isset($presets['webp'])
13-
<source
14-
srcset="{{ $presets['webp'] }}"
15-
sizes="32px"
16-
type="image/webp"
17-
>
18-
@endisset
19-
@isset($presets[$image->mimeType()])
20-
<source
21-
srcset="{{ $presets[$image->mimeType()] }}"
22-
sizes="32px"
23-
type="{{ $image->mimeType() }}"
24-
>
25-
@endisset
26-
<img
27-
{!! $attributes ?? '' !!}
2+
@if($image->width() > config('justbetter.glide-directive.image_resize_threshold'))
3+
<picture>
4+
@if( $image->extension() == 'svg' || $image->extension() == 'gif')
5+
<img
286
class="{{ $class }}"
29-
src="{{ $presets['placeholder'] ?? $image->url() }}"
30-
alt="{{ $alt ?? $image->alt() }}"
7+
src="{{ $image->url() }}"
8+
alt="{{ $alt }}"
319
width="{{ $width }}"
3210
height="{{ $height }}"
33-
loading="lazy"
34-
onload="
35-
this.onload = null;
36-
window.responsiveResizeObserver.observe(this);
37-
"
38-
>
39-
@endif
40-
</picture>
11+
/>
12+
@else
13+
@isset($presets['webp'])
14+
<source
15+
srcset="{{ $presets['webp'] }}"
16+
sizes="32px"
17+
type="image/webp"
18+
>
19+
@endisset
20+
@isset($presets[$image->mimeType()])
21+
<source
22+
srcset="{{ $presets[$image->mimeType()] }}"
23+
sizes="32px"
24+
type="{{ $image->mimeType() }}"
25+
>
26+
@endisset
27+
<img
28+
{!! $attributes ?? '' !!}
29+
class="{{ $class }}"
30+
src="{{ $presets['placeholder'] ?? $image->url() }}"
31+
alt="{{ $alt ?? $image->alt() }}"
32+
width="{{ $width }}"
33+
height="{{ $height }}"
34+
loading="lazy"
35+
onload="
36+
this.onload = null;
37+
window.responsiveResizeObserver.observe(this);
38+
"
39+
>
40+
@endif
41+
</picture>
42+
@else
43+
<img
44+
{!! $attributes ?? '' !!}
45+
class="{{ $class }}"
46+
src="{{ $image->url() }}"
47+
alt="{{ $alt ?? $image->alt() }}"
48+
width="{{ $width }}"
49+
height="{{ $height }}"
50+
loading="lazy"
51+
>
52+
@endif
4153
@endif

src/Responsive.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public static function handle(mixed ...$arguments): Factory|View|string
3939

4040
public static function getPresets(Asset $asset): array
4141
{
42+
if ($asset->width() <= config('justbetter.glide-directive.image_resize_threshold')) {
43+
return [];
44+
}
45+
4246
$config = config('statamic.assets.image_manipulation.presets');
4347

4448
if (! config('justbetter.glide-directive.placeholder') && isset($config['placeholder'])) {

0 commit comments

Comments
 (0)