Hi there
I am at a loss and hope someone can help me out. Months ago, I created a function in the config.php file to generate an og image. It is triggered when a page is updated or the title changed. It worked spendidly for months.
Now I have made some additional changes, totally unrelated to that - yes, I have added two other routes but they should not matter.
Anyway, what happens now is that once I try to publish a page (blog-article), I get the following error:
Line 184 is the line where the function is declared (not in the below code because I remove some “sensitive” stuff) - and as you can see by the exotic name, it certainly is NOT declared somewhere else.
I tried to
- remove the two added routes I added - no change
- uncommented the call to the function in the two hooks - no change
What “worked” was removing the function/commenting it out.
I am really at a loss what causes this problem. Any ideas?
Here is my config.php:
<?php
/**
* This is the general config file for the xxxxx website.
* For environment specific config and sensitive data such as API keys,
* please see the env.php file.
*
* For all config options see https://getkirby.com/docs/reference/system/options
*
*/
return [
'debug' => false,
'home' => 'blog',
//'languages.detect' => true, ## Lets enable automatic language detection
'languages' => true, ## Lets enable I18N
'smartypants' => true, // https://getkirby.com/docs/reference/system/options/smartypants
'auth' => [
'trials' => 3, // Number of wrong trials per timeout period before login gets blocked for the current IP address and user
'methods' => [
'password' => ['2fa' => false], // Send second factor (Code via E-Mail) - current TOTP plugins all look dead/old
],
'debug' => false, // We don't want to debug auth
],
'panel' => [
'css' => 'assets/css/custom-panel.css' // Lets add a custom design to our panel
],
'routes' => [
// Let's get rid of the "blog" in the URL
[
'pattern' => '(:any)',
'language' => '*',
'action' => function($lang, $uid) {
$page = page($uid);
if(!$page) $page = page('blog/' . $uid);
if(!$page) $page = site()->errorPage();
return site()->visit($page, $lang); // activate the selected page and set a language
}
],
[
'pattern' => 'blog/(:any)',
'language' => '*',
'action' => function($lang, $uid) {
go($uid);
}
],
// Lets add a multi-lingual sitemap - nobody really uses the weights, right? So lets ignore them
[
'pattern' => 'sitemap.xml',
'action' => function () {
// Define the pages to ignore
$ignore = ['home'];
$pages = site()->pages()->listed()->index();
$languages = site()->translations() ?: [];
$sitemap = '<?xml version="1.0" encoding="utf-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">';
foreach ($pages as $page) {
if (in_array($page->uri(), $ignore)) continue;
$sitemap .= '<url>
<loc>'.html($page->url()).'</loc>';
foreach ($languages as $lang) {
$sitemap .= '<xhtml:link rel="alternate" hreflang="'.$lang->code().'" href="'.html($page->url($lang->code())).'"/>';
}
$sitemap .= '</url>';
}
$sitemap .= '</urlset>';
return new Kirby\Cms\Response($sitemap, 'application/xml');
}
],
// And make sure that we redirect /sitemap to /sitemap.xml with HTTP Code 301 - Moved Permanently
[
'pattern' => 'sitemap',
'action' => function() {
return go('sitemap.xml', 301);
}
],
],
// We are adding hooks to maintain created/updated by/on fields and
// to generate ogimages dynamically if the title of a page changes
// Also, we want to allow for automation for published articles
'hooks' => [
'page.create:after' => function (Kirby\Cms\Page $page) {
$createdOn = Kirby\Toolkit\Date::now();
$user = kirby()->user();
$page->update([
'createdOn' => $createdOn,
'createdBy' => $user,
]);
},
'page.update:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage) {
$updatedOn = Kirby\Toolkit\Date::now();
$updatedBy = kirby()->user();
//$ogImageURL = generateOgImage90210($newPage);
$newPage->update([
'lastUpdatedOn' => $updatedOn,
'lastUpdatedBy' => $updatedBy,
'Ogimage' => $ogImageURL,
]);
},
'page.changeTitle:after' => function (Kirby\Cms\Page $newPage, Kirby\Cms\Page $oldPage) {
// $ogImageURL = generateOgImage90210($newPage);
$newPage->update([
'Ogimage' => $ogImageURL,
]);
},
],
// Matomo Plugin Config
'sylvainjule.matomo.url' => 'https:/xxxxxx.com',
'sylvainjule.matomo.id' => '1',
'sylvainjule.matomo.disableCookies' => true,
'sylvainjule.matomo.debug' => false,
// Content Security Policy Plugin
'bnomei.securityheaders.enabled' => true,
// Let's make sure we add the analytics subdomain dynamically
'bnomei.securityheaders.setter' => function (\Bnomei\SecurityHeaders $instance) {
// https://github.com/paragonie/csp-builder#build-a-content-security-policy-programmatically
// Add a new source domain to the whitelist
$csp = $instance->csp();
$csp->addSource('script-src', 'https://xxxxxx');
$csp->addSource('connect-src', 'https://xxxxxxx');
},
// SEO Plugin Config
'tobimori.seo' => [
'sitemap.active' => false,
'canonicalIncludesWWW' => false,
'robots' => [
'active' => true,
'content' => [
'*' => [
'Allow' => ['/'],
'Disallow' => ['/kirby', '/panel', '/content', '/site', '/media', '/assets']
]
]
]
],
// Translation Plugin Config
//'tobiaswolf.machine-translation.deepl.authKey' => '', => saved in env.php
// Lets make sure we show a nicely design error page for fatal errors not the ugly "This site is offline" OOB page
'fatal' => function($kirby, $exception) {
include $kirby->root('templates') . '/fatal.php';
}
];
/**
* This function generates dynamic opengraph images depending on the title
* of a page. It then writes the UUID into the appropriate content-file of
* each page for use in the tobimori.seo plugin
*
* @param Kirby\Cms\Page $page
* @return void
*/
function generateOgImage90210(Kirby\Cms\Page $page){
// Load the blank og image background as the image basis
$baseImgPath = './assets/images/ogimage_blank.png';
$canvas = imagecreatefrompng($baseImgPath);
// Define colors
$brandColor = imagecolorallocate($canvas, 29, 5, 26);
$textColor = imagecolorallocate($canvas, 29, 17, 1);
// Path to .ttf font file
$fontFile = './assets/css/font/Montserrat-Bold.ttf';
// Write page title to canvas
$title = $page->title()->toString();
$title = wordwrap($title, 25);
$printedText = imagefttext($canvas, 50, 0, 150, 185, $textColor, $fontFile, $title);
// Draw rectangle - relatively seen below the printed text
imagefilledrectangle($canvas, $printedText[0], $printedText[1]+30, $printedText[0]+365, $printedText[1]+40, $brandColor);
// Save image to the disk (temporarily)
$tempImageFile = './content/'.$page->diruri().'/'.$page->slug().'.'.$page->translation()->code().'.png';
$ogImageFile = $page->slug().'.'.$page->translation()->code().'.ogimage.png';
// If writing the PNG is successful
if(imagepng($canvas, $tempImageFile)){
// Check if the ogImage already exists. If yes, replace it...
if(F::exists(kirby()->root().'/content/'.$page->diruri().'/'. $ogImageFile)){
$file = $page->file($ogImageFile)->replace($tempImageFile, true);
return $file->uuid()->toString();
} else {
// ... if not, create a Kirby File object, save as ogimage and remove the original image file created via PHP
$file = File::create(['filename' => $ogImageFile, 'parent' => $page, 'source' => $tempImageFile], true);
return $file->uuid()->toString();
}
} else {
// Generation did not work - lets not save any file reference then
return '';
}
}