From 0966d4a627f037b38a5e79dbfe6448d433ea6650 Mon Sep 17 00:00:00 2001 From: tobimori Date: Fri, 8 Sep 2023 22:56:41 +0200 Subject: [PATCH] add option for string to og image, closes #24 --- blueprints/fields/og-image.php | 3 ++- classes/Meta.php | 23 +++++++++++++++++++++++ config/api.php | 8 +------- snippets/head.php | 16 +++++----------- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/blueprints/fields/og-image.php b/blueprints/fields/og-image.php index aff0844..20cf21b 100644 --- a/blueprints/fields/og-image.php +++ b/blueprints/fields/og-image.php @@ -1,8 +1,9 @@ 'files', 'multiple' => false, diff --git a/classes/Meta.php b/classes/Meta.php index 6d9b79b..69a12c7 100644 --- a/classes/Meta.php +++ b/classes/Meta.php @@ -4,6 +4,7 @@ use Kirby\Content\Field; use Kirby\Cms\Page; +use Kirby\Exception\InvalidArgumentException; use Kirby\Toolkit\Str; use Kirby\Toolkit\A; @@ -224,4 +225,26 @@ public function robots() return A::join($robots, ','); } + + /** + * Get the og:image url + */ + public function ogImage(): string|null + { + $field = $this->get('ogImage'); + + if ($ogImage = $field->toFile()?->thumb([ + 'width' => 1200, + 'height' => 630, + 'crop' => true, + ])) { + return $ogImage->url(); + } + + if ($field->isNotEmpty()) { + return $field->value(); + } + + return null; + } } diff --git a/config/api.php b/config/api.php index 68cc063..016a295 100644 --- a/config/api.php +++ b/config/api.php @@ -67,12 +67,6 @@ if ($model instanceof Page) { $meta = $model->metadata(); - $ogImage = $meta->ogImage()->toFile()?->thumb([ - 'width' => 1200, - 'height' => 630, - 'crop' => true, - ]); - return [ 'page' => $model->slug(), 'url' => $model->url(), @@ -81,7 +75,7 @@ 'ogSiteName' => $meta->ogSiteName()->value(), 'ogTitle' => $meta->ogTitle()->value(), 'ogDescription' => $meta->ogDescription()->value(), - 'ogImage' => $ogImage?->url(), + 'ogImage' => $meta->ogImage(), 'twitterCardType' => $meta->twitterCardType()->value(), ]; } diff --git a/snippets/head.php b/snippets/head.php index 49dfa42..524fd91 100644 --- a/snippets/head.php +++ b/snippets/head.php @@ -12,12 +12,6 @@ ogImage()->toFile()?->thumb([ - 'width' => 1200, - 'height' => 630, - 'crop' => true, -]); - $map = [ 'description' => $meta->metaDescription(), 'author' => $meta->metaAuthor(), @@ -26,7 +20,7 @@ 'twitter:card' => $meta->twitterCardType(), 'twitter:title' => $meta->ogTitle(), 'twitter:description' => $meta->ogDescription(), - 'twitter:image' => $ogImage?->url(), + 'twitter:image' => $ogImage = $meta->ogImage(), 'twitter:site' => $meta->twitterSite(), 'twitter:creator' => $meta->twitterCreator(), ]; @@ -46,10 +40,10 @@ 'og:description' => $meta->ogDescription(), 'og:url' => $meta->canonicalUrl(), 'og:site_name' => $meta->ogSiteName(), - 'og:image' => $ogImage?->url(), - 'og:image:width' => $ogImage?->width(), - 'og:image:height' => $ogImage?->height(), - 'og:image:alt' => $ogImage?->alt(), + 'og:image' => $ogImage, + 'og:image:width' => $ogImage ? 1200 : null, // TODO: replace this with custom crop preset + 'og:image:height' => $ogImage ? 630 : null, + 'og:image:alt' => $meta->get('ogImage')->toFile()?->alt(), 'og:type' => $meta->ogType(), ];