First of all, yes, you can do that with a panel.file.upload hook, the problem is the numbering of the images. Since images can be added and deleted, you cannot rely on the number of images in the folder to number your images correctly. So you would have to get the numbering string from each of the filenames, get the highest number and then add 1. To make it easier, I would probably put the number in front of the title.
Yes, but as I said above, this is unreliable and the hook will fail silently, if an image name already exists.
Example: There are three images in the folder: image-01,image-02, image-03. $count+1 will then be 4, and the next uploaded image will be renamed image-04. All is fine.
Now, someone deletes an image. $count+1 will again be 4; since image-04 already exists, renaming fails silently, the image will not be renamed. The user will not get a message and has to check in the sidebar, if the image was renamed successfully, and if not, rename the image manually.
Also, I’m not sure why, but I had to remove from the initial code $count++.
If the $page has already 2 $images, the new uploaded file was numbered 4 (instead of 3), and then next 5 etc…
Not sure why it was counting one image more then the ones I had.
So now I have the following code
// Auto rename photos
kirby()->hook('panel.file.upload', function($file) {
$page = $file->page();
$title = $page->title();
$count = 0;
$old_filename = $file->filename();
try {
$count = $page->images()->count();
// $count++;
// Add 0 before $count so that it is two digits
$count = str_pad($count, 2, '0', STR_PAD_LEFT);
$filename = $title . "-" . $count;
if (strpos($old_filename, 'box') !== false) {
$filename = $filename . "-box";
}
$file->rename($filename);
echo 'The file has been renamed';
} catch(Exception $e) {
echo 'The file has been renamed';
// optional reason: echo $e->getMessage();
}
});