Skip to content

Commit 405291a

Browse files
committed
better blade
1 parent 12245af commit 405291a

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

resources/views/components/img.blade.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,21 @@
1313
])
1414

1515
@php
16-
$placeholder = $placeholder === true ? 'placeholder' : $placeholder;
17-
$placeholderValue = $placeholder ? $media->getConversion($placeholder)?->contents : null;
16+
$source = $conversion
17+
? $media->getConversion(
18+
name: $conversion,
19+
state: \Elegantly\Media\Enums\MediaConversionState::Succeeded,
20+
fallback: is_bool($fallback) ? null : $fallback,
21+
dispatch: $dispatch,
22+
)
23+
: null;
24+
25+
$source ??= $fallback === true ? $media : null;
1826
19-
$url =
20-
$src ??
21-
$media->getUrl(conversion: $conversion, fallback: $fallback, parameters: $parameters, dispatch: $dispatch);
27+
$placeholder = $placeholder === true ? 'placeholder' : $placeholder;
28+
$placeholderContents = $placeholder ? $media->getConversion($placeholder)?->contents : null;
2229
@endphp
2330

24-
<img {!! $attributes !!} loading="{{ $loading }}" src="{!! $url !!}"
25-
height="{{ $height ?? $media->getHeight($conversion, $fallback) }}"
26-
width="{{ $width ?? $media->getWidth($conversion, $fallback) }}"
27-
alt="{{ $alt ?? $media->getName($conversion, $fallback) }}"
28-
@if ($placeholderValue) style="background-size:cover;background-image: url(data:image/jpeg;base64,{{ $placeholderValue }})" @endif>
31+
<img {!! $attributes !!} loading="{{ $loading }}" src="{!! $src ?? $source?->getUrl($parameters) !!}"
32+
height="{{ $height ?? $source?->height }}" width="{{ $width ?? $source?->width }}" alt="{{ $alt ?? $source?->name }}"
33+
@if ($placeholderContents) style="background-size:cover;background-image: url(data:image/jpeg;base64,{{ $placeholderContents }})" @endif>

resources/views/components/video.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
3737
@endphp
3838

39-
<video {!! $attributes !!} src="{!! $src ?? $source->url !!}" height="{{ $height ?? $source->height }}"
40-
width="{{ $width ?? $source->width }}" alt="{{ $alt ?? $source->name }}" poster="{{ $poster }}"
39+
<video {!! $attributes !!} src="{!! $src ?? $source?->getUrl($parameters) !!}" height="{{ $height ?? $source?->height }}"
40+
width="{{ $width ?? $source?->width }}" alt="{{ $alt ?? $source?->name }}" poster="{{ $poster }}"
4141
{{ when($autoplay, 'autoplay') }} {{ when($muted, 'muted') }} {{ when($playsinline, 'playsinline') }}
4242
{{ when($loop, 'loop') }}>
4343
{{ $slot }}

src/Concerns/InteractWithFiles.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,18 @@ public function humanReadableDuration(
324324
->cascade()
325325
->forHumans($syntax, $short, $parts, $options);
326326
}
327+
328+
/**
329+
* Whether the file is safe to use with <img>, <video>, etc. in the browser.
330+
*/
331+
public function isBrowserSafe(): bool
332+
{
333+
return match ($this->type) {
334+
MediaType::Image => in_array($this->mime_type, ['image/apng', 'image/avif', 'image/gif', 'image/jpeg', 'image/png', 'image/svg', 'image/webp']),
335+
MediaType::Video => in_array($this->mime_type, ['video/mp4', 'video/x-m4v', 'video/webm']),
336+
MediaType::Audio => in_array($this->mime_type, ['audio/mpeg', 'audio/mp4', 'audio/webm', 'audio/wav']),
337+
MediaType::Pdf => true,
338+
default => false,
339+
};
340+
}
327341
}

0 commit comments

Comments
 (0)