Skip to content

Commit 34f4a76

Browse files
[Feat/1173] implement fallback article images (#1178)
* feat: add fallback image to article image on summary page * feat: we don't need to add class * feat: fix quotes * feat: add fallback image to article image on show page * fix: update alt tag * fix: use asset so the fallback image loads * feat: add object-over to img to match behaviour when it used to be div * feat: add back up image to article on overview summary * feat: add back up image to article on user summary * feat: add gradient background * feat: fix gradient background * feat: remove hasHeroImage because it is now handled in front end * feat: fix indentation * wip --------- Co-authored-by: Dries Vints <[email protected]>
1 parent cd89b93 commit 34f4a76

File tree

5 files changed

+33
-23
lines changed

5 files changed

+33
-23
lines changed

app/Models/Article.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,6 @@ public function excerpt(int $limit = 100): string
101101
return Str::limit(strip_tags(md_to_html($this->body())), $limit);
102102
}
103103

104-
public function hasHeroImage(): bool
105-
{
106-
return $this->hero_image_url !== null;
107-
}
108-
109104
public function hasHeroImageAuthor(): bool
110105
{
111106
return $this->hero_image_author_name !== null &&
@@ -114,11 +109,7 @@ public function hasHeroImageAuthor(): bool
114109

115110
public function heroImage($width = 400, $height = 300): string
116111
{
117-
if ($this->hasHeroImage()) {
118-
return "{$this->hero_image_url}&fit=clip&w={$width}&h={$height}&utm_source=Laravel.io&utm_medium=referral";
119-
}
120-
121-
return asset('images/default-background.svg');
112+
return "{$this->hero_image_url}&fit=clip&w={$width}&h={$height}&utm_source=Laravel.io&utm_medium=referral";
122113
}
123114

124115
public function originalUrl(): ?string

resources/views/articles/show.blade.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@
2121
<div class="container mx-auto">
2222
<div class="px-4 lg:px-0 lg:mx-48">
2323
<div
24-
class="w-full bg-center {{ $article->hasHeroImage() ? 'bg-cover' : '' }} bg-gray-800 p-6 lg:p-8"
25-
style="background-image: linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.4)), url({{ $article->heroImage(2000,384) }});"
24+
class="w-full bg-center bg-gray-800"
25+
style="background-image: url('{{ asset('images/default-background.svg') }}')"
2626
>
27+
<div class="relative w-full bg-center p-6 lg:p-8 z-10">
28+
<img class="absolute w-full h-full left-0 top-0 object-cover -z-10"
29+
src="{{ $article->heroImage(2000,384) }}"
30+
alt="Article Hero Image"
31+
onerror="
32+
this.onerror=null
33+
this.src=''"
34+
>
35+
<div class="absolute inset-0 bg-gradient-to-b from-black/40 to-black/40 -z-10"></div>
2736
<div class="flex items-center justify-between mb-28 text-sm lg:text-base">
2837
<a href="{{ route('articles') }}" class="flex items-center text-white hover:underline">
2938
<x-heroicon-s-arrow-left class="w-4 h-4 fill-current" />
@@ -158,7 +167,7 @@ class="prose prose-lg text-gray-800 prose-lio"
158167
@endif
159168

160169
@if ($article->author()->hasTwitterAccount())
161-
<a href="https://x.com/{{ $article->author()->twitter() }}" class="text-twitter">
170+
<a href="https://twitter.com/{{ $article->author()->twitter() }}" class="text-twitter">
162171
<x-si-x class="w-6 h-6" />
163172
</a>
164173
@endif

resources/views/components/articles/overview-summary.blade.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
<div class="h-full rounded-lg shadow-lg bg-white lg:p-5">
88
<div class="flex flex-col gap-x-8 lg:flex-row">
9-
<a href="{{ route('articles.show', $article->slug()) }}" class="block">
10-
<div
11-
class="w-full h-32 rounded-t-lg bg-center {{ $article->hasHeroImage() ? 'bg-cover' : '' }} bg-gray-800 lg:w-48 lg:h-full lg:rounded-lg"
12-
style="background-image: url({{ $article->heroImage() }});"
9+
<a href="{{ route('articles.show', $article->slug()) }}" class="block lg:aspect-video">
10+
<img class="w-full h-32 rounded-t-lg bg-center bg-gray-800 lg:h-full lg:w-48 lg:rounded-lg object-none max-w-none"
11+
src="{{ $article->heroImage() }}"
12+
alt="Article Hero Image"
13+
onerror="
14+
this.onerror=null;
15+
this.src='images/default-background.svg';"
1316
>
14-
</div>
1517
</a>
1618

1719
<div class="flex flex-col gap-y-3 p-4 lg:p-0 lg:gap-y-3.5 w-full">

resources/views/components/articles/summary.blade.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
<div class="break-words">
88
@if ($isFeatured)
99
<a href="{{ route('articles.show', $article->slug()) }}">
10-
<div class="w-full h-72 mb-6 rounded-lg bg-center {{ $article->hasHeroImage() ? 'bg-cover' : '' }} bg-gray-800" style="background-image: url({{ $article->heroImage() }});"></div>
10+
<img class="w-full h-72 mb-6 rounded-lg bg-center bg-gray-800 object-cover"
11+
src="{{ $article->heroImage() }}"
12+
alt="Article Hero Image"
13+
onerror="
14+
this.onerror=null;
15+
this.src='{{ asset('images/default-background.svg') }}'"
16+
>
1117
</a>
1218
@endif
1319

resources/views/components/articles/user-summary.blade.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
<div class="h-full rounded-lg shadow-lg bg-white">
66
<div class="flex flex-col h-full gap-x-8">
77
<a href="{{ route('articles.show', $article->slug()) }}" class="block">
8-
<div
9-
class="w-full h-32 rounded-t-lg bg-center {{ $article->hasHeroImage() ? 'bg-cover' : '' }} bg-gray-800 lg:h-40"
10-
style="background-image: url({{ $article->heroImage() }});"
8+
<img class="w-full h-32 rounded-t-lg bg-center bg-gray-800 lg:h-40 object-cover"
9+
src="{{ $article->heroImage() }}"
10+
alt="Article Hero Image"
11+
onerror="
12+
this.onerror=null;
13+
this.src='{{ asset('images/default-background.svg') }}'"
1114
>
12-
</div>
1315
</a>
1416

1517
<div class="flex flex-col h-full gap-y-3 p-4">

0 commit comments

Comments
 (0)