Is there a way to create a File in Kirby based on a base64 String? I thought this should work?
$page->createFile([
'source' => base64_decode($base64),
'parent' => $page,
'filename' => 'image.jpg'
]);
Is there a way to create a File in Kirby based on a base64 String? I thought this should work?
$page->createFile([
'source' => base64_decode($base64),
'parent' => $page,
'filename' => 'image.jpg'
]);
I believe you’d have to first create a temporary file, as createFile
expects a path (it’s essentially a copy operation).
I do something similar with screenshots retrieved from an API:
$tmpfilename = $page->root() . '/temp.png';
$imagefile = Remote::get($imageurl);
F::write($tmpfilename , $imagefile->content());
…and then proceed to use $tmpfilename
as source
.
Ah ok, I don’t know that createFile
needs a path. Would be great if this also works with strings. I have already found another solution for me. But your solution is also very nice!
$base64 = explode(',',get('createimage'));
$filename = 'image.jpg';
file_put_contents($page->root() . '/' . $filename, base64_decode($base64[1]));