Skip to content

Commit 0966d4a

Browse files
committed
add option for string to og image, closes #24
1 parent 4311b08 commit 0966d4a

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

blueprints/fields/og-image.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3+
use Kirby\Cms\App;
34
use Kirby\Toolkit\Str;
45

5-
return function ($app) {
6+
return function (App $app) {
67
$blueprint = [
78
'type' => 'files',
89
'multiple' => false,

classes/Meta.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Kirby\Content\Field;
66
use Kirby\Cms\Page;
7+
use Kirby\Exception\InvalidArgumentException;
78
use Kirby\Toolkit\Str;
89
use Kirby\Toolkit\A;
910

@@ -224,4 +225,26 @@ public function robots()
224225

225226
return A::join($robots, ',');
226227
}
228+
229+
/**
230+
* Get the og:image url
231+
*/
232+
public function ogImage(): string|null
233+
{
234+
$field = $this->get('ogImage');
235+
236+
if ($ogImage = $field->toFile()?->thumb([
237+
'width' => 1200,
238+
'height' => 630,
239+
'crop' => true,
240+
])) {
241+
return $ogImage->url();
242+
}
243+
244+
if ($field->isNotEmpty()) {
245+
return $field->value();
246+
}
247+
248+
return null;
249+
}
227250
}

config/api.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@
6767
if ($model instanceof Page) {
6868
$meta = $model->metadata();
6969

70-
$ogImage = $meta->ogImage()->toFile()?->thumb([
71-
'width' => 1200,
72-
'height' => 630,
73-
'crop' => true,
74-
]);
75-
7670
return [
7771
'page' => $model->slug(),
7872
'url' => $model->url(),
@@ -81,7 +75,7 @@
8175
'ogSiteName' => $meta->ogSiteName()->value(),
8276
'ogTitle' => $meta->ogTitle()->value(),
8377
'ogDescription' => $meta->ogDescription()->value(),
84-
'ogImage' => $ogImage?->url(),
78+
'ogImage' => $meta->ogImage(),
8579
'twitterCardType' => $meta->twitterCardType()->value(),
8680
];
8781
}

snippets/head.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212

1313
<?php
1414

15-
$ogImage = $meta->ogImage()->toFile()?->thumb([
16-
'width' => 1200,
17-
'height' => 630,
18-
'crop' => true,
19-
]);
20-
2115
$map = [
2216
'description' => $meta->metaDescription(),
2317
'author' => $meta->metaAuthor(),
@@ -26,7 +20,7 @@
2620
'twitter:card' => $meta->twitterCardType(),
2721
'twitter:title' => $meta->ogTitle(),
2822
'twitter:description' => $meta->ogDescription(),
29-
'twitter:image' => $ogImage?->url(),
23+
'twitter:image' => $ogImage = $meta->ogImage(),
3024
'twitter:site' => $meta->twitterSite(),
3125
'twitter:creator' => $meta->twitterCreator(),
3226
];
@@ -46,10 +40,10 @@
4640
'og:description' => $meta->ogDescription(),
4741
'og:url' => $meta->canonicalUrl(),
4842
'og:site_name' => $meta->ogSiteName(),
49-
'og:image' => $ogImage?->url(),
50-
'og:image:width' => $ogImage?->width(),
51-
'og:image:height' => $ogImage?->height(),
52-
'og:image:alt' => $ogImage?->alt(),
43+
'og:image' => $ogImage,
44+
'og:image:width' => $ogImage ? 1200 : null, // TODO: replace this with custom crop preset
45+
'og:image:height' => $ogImage ? 630 : null,
46+
'og:image:alt' => $meta->get('ogImage')->toFile()?->alt(),
5347
'og:type' => $meta->ogType(),
5448
];
5549

0 commit comments

Comments
 (0)