Skip to content

Commit

Permalink
fix: better (none) multilang setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricerenck committed Nov 4, 2024
1 parent af4b9b7 commit bcaebb3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

---

This plugin creates an og-image for a page based on a template image and a text input. Simply add `/og-image` to any url to get the og-image for that page.
This plugin creates an og-image for a page, based on a template image and a text input. Simply add `/og-image` to any url to get the og-image for that page.

## Installation

Expand Down Expand Up @@ -75,6 +75,7 @@ return [
| --------------------------------- | -------- | ------------------------------------------------------------------------------------------------ |
| `width` | `1600` | width of the resulting og-image |
| `height` | `900` | height of the resulting og-image |
| `field` | `'ogImage'` | field for manualy setting an image |
| `image.template` | `./../assets/template.png` | path to your og-image template image |
| `font.path` | `''` | **mandatory** (missing font will result in an error) |
| `font.color` | `[0, 0, 0]` | color of the font [r,g,b] |
Expand Down
52 changes: 41 additions & 11 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,61 @@
'pageMethods' => require_once __DIR__ . '/lib/page-methods.php',
'routes' => [
[
'pattern' => '(:all)/og-image',
'language' => '*',
'action' => function ($lang, $slug) {
$page = ($slug == $lang) ? site()->homePage() : $page = page($slug);
'pattern' => ['og-image'],
'action' => function () {
$page = site()->homePage();

$imageWidth = option('mauricerenck.ogimage.width', 1600);
$imageHeight = option('mauricerenck.ogimage.height', 900);
if ($page->hasOgImage()) {
return new Response($page->getOgImage()->read(), 'image/png');
}

if ($page->hasGeneratedOgImage()) {
return new Response($page->image('generated-og-image.default.png')->read(), 'image/png');
}

try {
$page->createOgImage();
return new Response($page->image('generated-og-image.default.png')->read(), 'image/png');
} catch (\Exception $e) {
return new Response($e->getMessage(), 'text/plain', 500);
}
}
],
[
'pattern' => ['(:all)/og-image', 'og-image'],
'action' => function ($slug) {
$languages = kirby()->languages();
$language = null;
if (count($languages) > 1) {
$language = kirby()->language()->code();
$slugParts = explode('/', $slug);

if (in_array($slugParts[0], $languages->codes())) {
$language = $slugParts[0];
$slug = implode('/', array_slice($slugParts, 1));
}
}

$languageString = is_null($language) ? 'default' : $language;
$page = ($slug == '/' || $slug == 'og-image') ? site()->homePage() : $page = page($slug);

if (!$page) {
return new Response('Page "' . $slug . '" not found', 'text/plain', 404);
}

if ($page->ogimage()->isNotEmpty()) {
return new Response($page->ogimage()->toFile()->crop($imageWidth, $imageHeight)->read(), 'image/png');
if ($page->hasOgImage()) {
return new Response($page->getOgImage()->read(), 'image/png');
}

if ($page->hasGeneratedOgImage()) {
return new Response($page->image('generated-og-image.' . $lang . '.png')->read(), 'image/png');
return new Response($page->image('generated-og-image.' . $languageString . '.png')->read(), 'image/png');
}

try {
$page->createOgImage();
return new Response($page->image('generated-og-image.' . $lang . '.png')->read(), 'image/png');
return new Response($page->image('generated-og-image.' . $languageString . '.png')->read(), 'image/png');
} catch (\Exception $e) {
return $e->getMessage();
return new Response($e->getMessage(), 'text/plain', 500);
}
},
],
Expand Down
24 changes: 17 additions & 7 deletions lib/page-methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@
namespace mauricerenck\OgImage;

return [
'hasOgImage' => function () {
$ogImageField = option('mauricerenck.ogimage.field', 'ogImage');
return (!is_null($this->{$ogImageField}()) && $this->{$ogImageField}()->isNotEmpty());
},
'hasGeneratedOgImage' => function () {
$language = $this->kirby()->language();
$filename = 'generated-og-image.' . $language->code() . '.png';
$languageString = is_null($language) ? 'default' : $language->code();
$filename = 'generated-og-image.' . $languageString . '.png';

$savedOgImage = !is_null($this->image($filename)) && $this->image($filename)->exists();
$setOgImage = $this->ogImage()->isNotEmpty();

return $savedOgImage || $setOgImage;
return $savedOgImage;
},
'hasHeroImage' => function () {
return $this->hero()->isNotEmpty();
'getOgImage' => function () {
$ogImageField = option('mauricerenck.ogimage.field', 'ogImage');
$imageWidth = option('mauricerenck.ogimage.width', 1600);
$imageHeight = option('mauricerenck.ogimage.height', 900);

return (!is_null($this->{$ogImageField}()) && $this->{$ogImageField}()->isNotEmpty())
? $this->{$ogImageField}()->toFile()->crop($imageWidth, $imageHeight)
: null;
},
'createOgImage' => function () {
$imageWidth = option('mauricerenck.ogimage.width', 1600);
Expand All @@ -36,7 +46,6 @@
$titlePosition = option('mauricerenck.ogimage.title.position', [0, 0]);
$titleCharactersPerLine = option('mauricerenck.ogimage.title.charactersPerLine', 20);

// check mandatory options
if (is_null($font)) {
return;
}
Expand Down Expand Up @@ -127,7 +136,8 @@
imagepng($canvas, $tempFile);

$language = $this->kirby()->language();
$filename = 'generated-og-image.' . $language->code() . '.png';
$languageString = is_null($language) ? 'default' : $language->code();
$filename = 'generated-og-image.' . $languageString . '.png';

kirby()->impersonate('kirby');
if (!is_null($this->file($filename)) && $this->file($filename)->exist()) {
Expand Down

0 comments on commit bcaebb3

Please sign in to comment.