An issue about thumbnails

After set this hook in config.

kirby()->hook(['panel.file.upload', 'panel.file.replace'], function($file) {
   $file->resize(300);
});

The first thumbnail file successfully created in the thumbs folder when upload an image.

One for my customized size in the hook and one for panel default.

Everything is ok now but then i am trying to upload second image rt.png

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.

How can we fix this behavior?

That’s actually a bug that was supposed to be fixed: https://github.com/getkirby/panel/issues/890, but there’s also this issue: https://github.com/getkirby/panel/issues/971

Interesting. Is there anything that I can do instead of waiting?

Would there be any harm if I delete “$this->page->removeThumbs();” lines in “panel/models/page/uploader.php”?

He says “Fixed!” but I don’t know how.

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 :slight_smile:
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.

1 Like

Maybe this plugin is interesting for you: https://github.com/fabianmichael/kirby-imagekit

Does not solve the problem with the thumbs, but could probably make your page load fast enough when creating thumbsnails on the fly.

@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.

What have you got until now? Maybe we can help…

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.

Probably you are right. I did not consider Imagekit when writing the hook.

Thumbs are no longer being deleted now. Check out the dev branch.

Sorry but where is the dev branch? I guess I did not find it. Is this the correct address?

Yep, exactly (and some more characters).

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?

Yes, you are right, I tested this a couple of days ago and reopened this issue.

1 Like