Skip to content

Commit

Permalink
add option for string to og image, closes #24
Browse files Browse the repository at this point in the history
  • Loading branch information
tobimori committed Sep 8, 2023
1 parent 4311b08 commit 0966d4a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
3 changes: 2 additions & 1 deletion blueprints/fields/og-image.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php

use Kirby\Cms\App;
use Kirby\Toolkit\Str;

return function ($app) {
return function (App $app) {
$blueprint = [
'type' => 'files',
'multiple' => false,
Expand Down
23 changes: 23 additions & 0 deletions classes/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Kirby\Content\Field;
use Kirby\Cms\Page;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\Str;
use Kirby\Toolkit\A;

Expand Down Expand Up @@ -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;
}
}
8 changes: 1 addition & 7 deletions config/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
];
}
Expand Down
16 changes: 5 additions & 11 deletions snippets/head.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@

<?php

$ogImage = $meta->ogImage()->toFile()?->thumb([
'width' => 1200,
'height' => 630,
'crop' => true,
]);

$map = [
'description' => $meta->metaDescription(),
'author' => $meta->metaAuthor(),
Expand All @@ -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(),
];
Expand All @@ -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(),
];

Expand Down

0 comments on commit 0966d4a

Please sign in to comment.