Skip to content

Commit

Permalink
implement fallback support
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Feb 4, 2024
1 parent 0da95a0 commit 6c4841b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
8 changes: 5 additions & 3 deletions resources/views/components/img.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
'width' => null,
'height' => null,
'sizes' => '1px',
'fallback' => false,
])

<img {!! $attributes !!} src="{{ $media->getUrl($conversion) }}" loading="{{ $loading }}"
height="{{ $height ?? $media->getHeight($conversion) }}" width="{{ $width ?? $media->getWidth($conversion) }}"
alt="{{ $alt ?? $media->getName($conversion) }}"
<img {!! $attributes !!} loading="{{ $loading }}" src="{{ $media->getUrl($conversion, $fallback) }}"
height="{{ $height ?? $media->getHeight($conversion, $fallback) }}"
width="{{ $width ?? $media->getWidth($conversion, $fallback) }}"
alt="{{ $alt ?? $media->getName($conversion, $fallback) }}"
@if ($responsive) srcset="{{ $media->getSrcset($conversion)->join(', ') }}"
onload="window.requestAnimationFrame(function(){if(!(size=getBoundingClientRect().width))return;onload=null;sizes=Math.ceil(size/window.innerWidth*100)+'vw';});"
sizes="{{ $sizes }}" @endif>
44 changes: 26 additions & 18 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ public function hasGeneratedConversion(string $conversion, ?string $state = null
* Retreive the path of a conversion or nested conversion
* Ex: $media->getPath('poster.480p')
*/
public function getPath(?string $conversion = null): ?string
public function getPath(?string $conversion = null, bool $fallback = false): ?string
{
if ($conversion) {
return $this->getGeneratedConversion($conversion)?->path;
$path = $this->getGeneratedConversion($conversion)?->path;

return $fallback ? ($path ?? $this->path) : $path;
}

return $this->path;
Expand Down Expand Up @@ -191,9 +193,7 @@ public function getUrl(?string $conversion = null, null|true|string|callable $fa
return $this->getUrl(conversion: $fallback);
}

if (is_callable($fallback)) {
return value($fallback);
}
return value($fallback);
}

return $url;
Expand Down Expand Up @@ -231,54 +231,62 @@ public function getTemporaryUrl(
return $this->getTemporaryUrl(conversion: $fallback, expiration: $expiration, options: $options);
}

if (is_callable($fallback)) {
return value($fallback);
}
return value($fallback);
}

return $url;
}

public function getWidth(?string $conversion = null): ?int
public function getWidth(?string $conversion = null, bool $fallback = false): ?int
{
if ($conversion) {
return $this->getGeneratedConversion($conversion)?->width;
$width = $this->getGeneratedConversion($conversion)?->width;

return $fallback ? ($width ?? $this->width) : $width;
}

return $this->width;
}

public function getHeight(?string $conversion = null): ?int
public function getHeight(?string $conversion = null, bool $fallback = false): ?int
{
if ($conversion) {
return $this->getGeneratedConversion($conversion)?->height;
$height = $this->getGeneratedConversion($conversion)?->height;

return $fallback ? ($height ?? $this->height) : $height;
}

return $this->height;
}

public function getName(?string $conversion = null): ?string
public function getName(?string $conversion = null, bool $fallback = false): ?string
{
if ($conversion) {
return $this->getGeneratedConversion($conversion)?->name;
$name = $this->getGeneratedConversion($conversion)?->name;

return $fallback ? ($name ?? $this->name) : $name;
}

return $this->name;
}

public function getSize(?string $conversion = null): ?int
public function getSize(?string $conversion = null, bool $fallback = false): ?int
{
if ($conversion) {
return $this->getGeneratedConversion($conversion)?->size;
$size = $this->getGeneratedConversion($conversion)?->size;

return $fallback ? ($size ?? $this->size) : $size;
}

return $this->size;
}

public function getAspectRatio(?string $conversion = null): ?float
public function getAspectRatio(?string $conversion = null, bool $fallback = false): ?float
{
if ($conversion) {
return $this->getGeneratedConversion($conversion)?->aspect_ratio;
$aspect_ratio = $this->getGeneratedConversion($conversion)?->aspect_ratio;

return $fallback ? ($aspect_ratio ?? $this->aspect_ratio) : $aspect_ratio;
}

return $this->aspect_ratio;
Expand Down

0 comments on commit 6c4841b

Please sign in to comment.