Good afternoon,
I am trying to dynamically generate ogImages using Content Representation as per the Cookbook Recipe using good old PHP image methods - it works great.
Now the disadvantage of this is that I am working outside of Kirby and thus not getting UUIDs for my newly created images.
This is important because the SEO plugin needs this UUID to be stored in the content file to show and work with it accordingly in the Panel.
I just want to make sure that I understand the methods Kirby provides in the File Class. Do I see it correctly, that the best way is to generate and save the file using raw PHP and then File::create() the file, providing the freshly created image as the source, create the File using this Kirby method and then delete the PHP generated file?
This sounds easily possible but not elegant to me So I am wondering if there is a better solution?
Thanks
Andreas
Nope, you only create the file once in the file system, but then use File::create() to create the file object (but you don’t delete anything).
Thanks @texnixe but File::create tries to overwrite the file. This is my code:
function generateOgImage(Kirby\Cms\Page $page, $kirby){
// Load the blank og image background as the image basis
$baseImgPath = './assets/images/ogimage_blank.png';
$canvas = imagecreatefrompng($baseImgPath);
[... do image manipulation stuff ...]
// Save image to the disk
$fileURL = './content/'.$page->diruri().'/'.$page->slug().'.png';
if(imagepng($canvas, $fileURL)){
$file = File::create(['filename' => $page->slug().'.png', 'parent' => $page, 'source' => $kirby->root().$fileURL]);
return $file->uuid()->toString();
} else {
return 'Das hat ned gefunzt';
}
Throws a Kirby\Exception\LogicException thrown with message “The file could not be created” error while trying to overwrite the file:
I tried to set File::Create($props, true) to delete the old file but that did not work either.
BUT once I rename the file so it does not have to be overwritten, it works:
$file = File::create(['filename' => $page->slug().'2.png', 'parent' => $page, 'source' => $kirby->root().$fileURL], true);
That is that then
Andreas