Its thumb file is successfully created but first one (ipad.png) is deleted automatically. Also it happens every time I upload an image. Kirby deletes previous thumb file created with hook.
And I can’t find any commit that around that date that seems to reference the issue. I’d test with uncommenting that line.
Edit: But this whole thing is tricky. If you remove one single file, for example, the whole thumbs folder for that page is deleted, so that your thumbs will all be gone if that happens.
Maybe it would be a better option to generate the file sizes you need in the page folder? Or somewhere else, if you don’t want to rely on thumbs generated on page load (why don’t you want that?):
Bugs bugs everywhere
I am kidding. It is the nature of programming i guess.
Generating thumbs on page load is risky for me because speed is important.
I spend my whole day to find a workaround. Because the thumbs in the default “thumbs” folder are deleted by itself ridiculously when replacing, uploading and renaming files. I had to move thumbnails in a safe place everytime a file uploaded via panel. (content/page/thumbs). Then I won’t have to worry about the default this thumbs folder’s strange behavior.
It is little bit nasty but I came up with this hook. Thank you for the idea. I will simplify later this but luckily it works for now!
kirby()->hook(['panel.file.upload', 'panel.file.replace'], function($file) {
$width = 300;
// get file info
$filePath = $file->page()->root();
$fileName = $file->name();
$fileSafeName = $file->safeName();
$fileFullPath = $filePath . "/" . $fileSafeName;
// get thumb info
$thumbArray = $file->resize($width)->toArray();
$thumbName = $thumbArray["safeName"];
$thumbFullPath = $thumbArray["root"];
// new thumbs full path
$targetFullPath = $filePath . "/thumbs/" . $thumbName;
// when uploading
if ($this->type() == "panel.file.upload") {
// create thumb folder if it doesnt exist
if (!file_exists($filePath . "/thumbs/")) {
mkdir($filePath . "/thumbs/", 0777, true);
}
// copy default thumb file to new location
rename($thumbFullPath, $targetFullPath);
}
// when replacing
else if ($this->type() == "panel.file.replace") {
$thumbFolder = $filePath . "/thumbs/";
$thumbFiles = scandir($thumbFolder);
$thumbFiles = array_diff($thumbFiles, array('.', '..'));
$thumbFilename = "";
// find the thumbnail of the file
foreach ($thumbFiles as $key) {
// check filename
if (preg_match('/'.$fileName.'-([0-9]+)x([0-9]+).(.*)/', $key)) {
// take it
$thumbFilename = $key;
break;
}
}
// remove old one
unlink($thumbFolder . $thumbFilename);
// copy new one
rename($thumbFullPath, $targetFullPath);
}
});
It is fun to play with Kirby. Great effort. Respect.
@fhmi Thank you for your hook snippet, I’m going to try it out asap! I’ve encountered the same issue seven months ago, and recently began to port my site to Wordpress, since Kirby never fixed this bug. Maybe you just saved me from changing my CMS…
Edit: I’m unable to adapt this for multiple thumbnails, sadly.
I think I just realized that this workaround doesn’t work with Imagekit anyways, doesn’t it? You would only move the “job” files to a new location, which the plugin wouldn’t know about. And since I rely on this plugin, there’s still nothing I can do about it.
I’m not shure if I’ve done something wrong, but my thumbnails are still being deleted. I’ve downloaded the latest core and panel from the develop branch and I’ve overwritten the files on my server with their contents. If I upload one image, all of my “presets” seem to be generated fine:
But as soon as I upload another image, all of the jobs vanish:
The only thumbnails that remain are the ones the panel generates (75x75/48x48) and the one that is “hardcoded” into the template (640). Is it possible that Imagekit is part of the problem? Is there anything I can do to finally fix this problem?