Skip to content

Commit bcaebb3

Browse files
committed
fix: better (none) multilang setup
1 parent af4b9b7 commit bcaebb3

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
---
88

9-
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.
9+
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.
1010

1111
## Installation
1212

@@ -75,6 +75,7 @@ return [
7575
| --------------------------------- | -------- | ------------------------------------------------------------------------------------------------ |
7676
| `width` | `1600` | width of the resulting og-image |
7777
| `height` | `900` | height of the resulting og-image |
78+
| `field` | `'ogImage'` | field for manualy setting an image |
7879
| `image.template` | `./../assets/template.png` | path to your og-image template image |
7980
| `font.path` | `''` | **mandatory** (missing font will result in an error) |
8081
| `font.color` | `[0, 0, 0]` | color of the font [r,g,b] |

index.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,61 @@
1111
'pageMethods' => require_once __DIR__ . '/lib/page-methods.php',
1212
'routes' => [
1313
[
14-
'pattern' => '(:all)/og-image',
15-
'language' => '*',
16-
'action' => function ($lang, $slug) {
17-
$page = ($slug == $lang) ? site()->homePage() : $page = page($slug);
14+
'pattern' => ['og-image'],
15+
'action' => function () {
16+
$page = site()->homePage();
1817

19-
$imageWidth = option('mauricerenck.ogimage.width', 1600);
20-
$imageHeight = option('mauricerenck.ogimage.height', 900);
18+
if ($page->hasOgImage()) {
19+
return new Response($page->getOgImage()->read(), 'image/png');
20+
}
21+
22+
if ($page->hasGeneratedOgImage()) {
23+
return new Response($page->image('generated-og-image.default.png')->read(), 'image/png');
24+
}
25+
26+
try {
27+
$page->createOgImage();
28+
return new Response($page->image('generated-og-image.default.png')->read(), 'image/png');
29+
} catch (\Exception $e) {
30+
return new Response($e->getMessage(), 'text/plain', 500);
31+
}
32+
}
33+
],
34+
[
35+
'pattern' => ['(:all)/og-image', 'og-image'],
36+
'action' => function ($slug) {
37+
$languages = kirby()->languages();
38+
$language = null;
39+
if (count($languages) > 1) {
40+
$language = kirby()->language()->code();
41+
$slugParts = explode('/', $slug);
42+
43+
if (in_array($slugParts[0], $languages->codes())) {
44+
$language = $slugParts[0];
45+
$slug = implode('/', array_slice($slugParts, 1));
46+
}
47+
}
48+
49+
$languageString = is_null($language) ? 'default' : $language;
50+
$page = ($slug == '/' || $slug == 'og-image') ? site()->homePage() : $page = page($slug);
2151

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

26-
if ($page->ogimage()->isNotEmpty()) {
27-
return new Response($page->ogimage()->toFile()->crop($imageWidth, $imageHeight)->read(), 'image/png');
56+
if ($page->hasOgImage()) {
57+
return new Response($page->getOgImage()->read(), 'image/png');
2858
}
2959

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

3464
try {
3565
$page->createOgImage();
36-
return new Response($page->image('generated-og-image.' . $lang . '.png')->read(), 'image/png');
66+
return new Response($page->image('generated-og-image.' . $languageString . '.png')->read(), 'image/png');
3767
} catch (\Exception $e) {
38-
return $e->getMessage();
68+
return new Response($e->getMessage(), 'text/plain', 500);
3969
}
4070
},
4171
],

lib/page-methods.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,27 @@
33
namespace mauricerenck\OgImage;
44

55
return [
6+
'hasOgImage' => function () {
7+
$ogImageField = option('mauricerenck.ogimage.field', 'ogImage');
8+
return (!is_null($this->{$ogImageField}()) && $this->{$ogImageField}()->isNotEmpty());
9+
},
610
'hasGeneratedOgImage' => function () {
711
$language = $this->kirby()->language();
8-
$filename = 'generated-og-image.' . $language->code() . '.png';
12+
$languageString = is_null($language) ? 'default' : $language->code();
13+
$filename = 'generated-og-image.' . $languageString . '.png';
914

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

13-
return $savedOgImage || $setOgImage;
17+
return $savedOgImage;
1418
},
15-
'hasHeroImage' => function () {
16-
return $this->hero()->isNotEmpty();
19+
'getOgImage' => function () {
20+
$ogImageField = option('mauricerenck.ogimage.field', 'ogImage');
21+
$imageWidth = option('mauricerenck.ogimage.width', 1600);
22+
$imageHeight = option('mauricerenck.ogimage.height', 900);
23+
24+
return (!is_null($this->{$ogImageField}()) && $this->{$ogImageField}()->isNotEmpty())
25+
? $this->{$ogImageField}()->toFile()->crop($imageWidth, $imageHeight)
26+
: null;
1727
},
1828
'createOgImage' => function () {
1929
$imageWidth = option('mauricerenck.ogimage.width', 1600);
@@ -36,7 +46,6 @@
3646
$titlePosition = option('mauricerenck.ogimage.title.position', [0, 0]);
3747
$titleCharactersPerLine = option('mauricerenck.ogimage.title.charactersPerLine', 20);
3848

39-
// check mandatory options
4049
if (is_null($font)) {
4150
return;
4251
}
@@ -127,7 +136,8 @@
127136
imagepng($canvas, $tempFile);
128137

129138
$language = $this->kirby()->language();
130-
$filename = 'generated-og-image.' . $language->code() . '.png';
139+
$languageString = is_null($language) ? 'default' : $language->code();
140+
$filename = 'generated-og-image.' . $languageString . '.png';
131141

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

0 commit comments

Comments
 (0)