Thumbnail not loaded directly after creation

This is the same issue as in posts:

Kirby 3.5, PHP7.3-FPM, Apache 2.4.25 on Debian

$asset = new asset('file.jpg');
$thumb = $asset->thumb(array('width' => 250));

then I use $thumb->url() inside an image tag src attribute.

Now all seems ok, the generated code is correct, but the thumb isn’t loaded the first time the page loads, even though web inspector shows a HTTP200 status code for the thumb. Only after a page refresh it is loaded. On hovering over the source in web inspector, it prints ‘could not load the image’.

The thumb has an img src pointing to the media/assets/ folder, where it is put by Kirby after creation. Possibly the page is requesting the image before it is ready? This also explains why it is actually found after a reload.

Since literally thousands of crude image files need to be resized on the fly, I’m pretty much stuck.

Have you tested this with a fresh Starterkit? If not yet, you could please try if you have the same issue there as well.

Also, do you use any plugins (which ones)? Is there anything particular about these images (size?).

Does this happen with a single image on the page, or only when you have multiple images (how many?).

What about server caching?

I dropped the code in the default template of the plainkit, exact same behaviour.
No plugins, just the plainkit and these lines of code added, I also added the dir and the file, of course. Nothing else.

$asset = new asset('assets/images/imgname.jpg');
$thumb = $asset->thumb(array('width' => 200));

echo $thumb;

Image sizes are very normal, ranging somewhere under a 800px width and less in height and are around 70kb filesize. Happens both with single and multiple images on the page. No caching is active on the system.

The same problem (or bug?) also shows in the Panel. If I open an image belonging to a page, the image box initially stays empty. If I click the empty box the browser tries to open the image and shows the following error message:

The image “path/filename” cannot be displayed because it contains errors.

The path/filename is the generated path by Kirby; media/etc
But after a page refresh also here it works, now the image loads. I changed from GD to Imagick, to no avail. It has all the seeming of a bug which only rears its head in specific circumstances.

I’m upgrading from a Kirby 2 site which is running without any problems on this server, so I doubt there might be something wrong with the apache/php7.3 config, but I’ll check that too. Suggestions are very much appreciated!

Apparently this problem can occur when loading images outside Kirby. It is solved by simply adding the save() method

$asset = asset('assets/images/imgname.jpg');
$thumb = $asset->thumb(array('width' => 200))->save();

which saves the file to disk and prevents the image from getting ‘lazy loaded’, according to some expert. (thanks Bastian)