I’m using getID3 to get the width and height of videos being uploaded and
it’s working fine to extract the data but I am not sure how to update the preview details field called dimensions similar to how images shows; width x height px
Here is the code from my file.create:after hook, where I have a update for the dimensions but I’m not sure how to get it to show on the panel page.
"file.create:after" => function (Kirby\Cms\File $file) {
// if filetype is equal to image
if ($file->type() === "image") {
function containsPoster($filename)
{
return strpos($filename, "-poster") !== false;
}
if (containsPoster($file)) {
$file = $file->update([
"template" => "poster",
]);
} else {
$file = $file->update([
"template" => $file->type(),
]);
}
}
// if filetype is video
elseif ($file->type() === "video") {
require_once __DIR__ . "/../libs/getid3/getid3.php";
$getID3 = new getID3();
$fileInfo = $getID3->analyze($file->root());
if (
isset($fileInfo["video"]["resolution_x"]) &&
isset($fileInfo["video"]["resolution_y"])
) {
$width = $fileInfo["video"]["resolution_x"];
$height = $fileInfo["video"]["resolution_y"];
try {
$file->update([
"dimensions" => [
"width" => $width,
"height" => $height,
],
]);
// Update the page's metadata with the video dimensions
$parentPage = $file->page();
$parentPage->update([
"dimensions" => $width . "x" . $height,
]);
} catch (Exception $e) {
// Handle the exception, e.g., log the error
error_log(
"Error updating file dimensions and page meta: " .
$e->getMessage()
);
}
}
I plan to only have one update for the meta but I was iterating over ideas which is why I ( in the uploaded code ) had but an update for the meta and parent page in case that would make a different ( just testing
The preview details I wanted to update / show is this:
That won’t work out of the box, because only an image has dimensions in the Kirby file world, unless you create a new file model with a dimension property.
So what is shown in that view as dimensions is not something that is present in the file meta data.
You cannot register a file model like this, in fact, there is no way to register file models in Kirby.
You need to take a detour via a page model, in which you overwrite what files are, example code can be found in this virtual files recipe: Virtual files | Kirby CMS
@texnixe thank you for the link to the virtual files, had a read through and that makes good sense.
I’ll give it a go once I’ve read the recipe through properly
I’ve read through the tutorial you linked and as far as I can see I should only ‘copy’ the steps in Option 2 from the tutorial ?
So far I’m not having any luck and I was wondering if you would not mind outlining the steps needed to make it work with video - then I could work from there ?
I know it’s a big ask but I’m only looking for an outline so I can move in the right direction to get Kirby to work well with video and hopefully others can use it as well.
Instead of overwriting the files() method, I’d probably go for the $page->videos() method, because what you want to do only affects videos, so we leave the other files untouched.
As a second step, you would create a VideoFile class, which you then load in your index.php.
The thumbs part is completely irrelavant here, but since you want the dimensions, you would then add this new property in your class as in the example. Of course, to define the dimensions, you need the getid3 extension.
That should be basically it, I think.
Of course, I have not tested this, but I think it should work.