Image (Resize, Crop, ...) doesn't use previous generated files

Because the execution of the commands crop or resize takes some 1000-10000 ms. When the thumbs are loaded from thumbs-folder, the execution lasts only a view ms (3-4).

As I mentioned before, the files in the thumbs-folder are staying unchanged (size, timestamp).

I guess you could just put in a dump() command in the source code or log something to file to see if the isThere() check is true and the thumb newly created. I canā€™t really believe that the thumb is just overwritten without the time stamp being modified. Even if I set the overwrite option to true, so that the thumb is always recreated, the modified timestamp gets changed.

When I try to call isThere() an error is returned:

Call to undefined method Asset::isThere()

My code looks like that:

$newimage = $page->image()->crop($width, $height, 15);
$isThere = $newimage->isThere();

I donā€™t think it works like that, the thumb method does not return an object of type Thumb, but of type Media. What I meant was to dump something directly in the thumb constructor in the source code (in kirby/vendor/getkirby/toolkit/lib/thumb.php)

OK, I put an echo into the constructor for type Thumb, and it reports that the thumbs already exists and they were not created again. Thatā€™s fine.

But the problem still exists that it lasts sometimes multiple seconds to get the thumb after a resize or crop. Any idea?

Edit: To ensure that the server does not respond too slow for checking the existence of the thumb, I included a stopwatch. The execution time of isThere is 0 ms.

I think I can relate to that issue and posted a similar topic some time ago: Very slow server respond times when creating thumbnails it seems they could be related to each other.

But the problem still exists that it lasts sometimes multiple seconds to get the thumb after a resize or crop. Any idea?

Got the same very very slow first respond times (~10 seconds) on localhost when I have a page with lots of images where thumbnails get generated.

Do you have the same behavior like this?

Yes, it seems to be a similar problem. The difference is, that my website is online and runs with Linux/Apache. For image processing I use imagick.

As I mentioned before, this effect occurs randomly. When the website is not used (for a few hours), this effect occurs. But when a page was loaded, a reload is fast as it should be.

Does this only happen on localhost? If so, in what way does it differ from your remote server? Caching enabled? PHP version? Other?

After a little bit more investigation, the code which causes this behaviour is in kirby/component/thumb.php in the function public function create($file, $params):

$thumb = new Generator($file, $params);

My experience in PHP is two weak to further follow the problem. Has anyone a suggestion?

With analysing the runtime of commands, I was able to identify the position where the starting-point of this problem is. The call-sequence to the starting-point is:

  • ...->image()->crop(...)
  • traits/image.php:
  • public function crop() ->
  • public function thumb(): return $this->kirby->component('thumb')->create($this, $params);
  • component/thumb.php:
  • public function create(): $thumb = new Generator($file, $params);
  • toolkit/lib/thumb.php:
  • public function __construct(): $this->destination = $this->destination(); ->
  • public function destination(): $result = call($this->options['destination'], $this);
  • toolkit/helpers.php:
  • function call(): call_user_func_array($function, $arguments)

And exactly the call of call_user_func_array($function, $arguments) causes this problem.

Iā€™ve created an issue on GitHub.

Is there a workaround possible, till this issue is solved?

call_user_func_array seems to be slow when called often or with too many arguments. one could refactor the function toolkit/helper/call() to use argument unpacking (since php 5.6). so instead of call_user_func_array use something like that:

$object->{$method}(...$args);

or when no object available

$method(...$args);

I tried this before, but it changed nothing. It stays slow as it was before.

then refactor panel/models/page/removeThumbs() to not clean folder but check for timestamp of all thumb-files if page panel/models/page/replace() and upload() where called. if move() was called it needs to remove all.

Unfortunately I canā€™t reproduce this issue. I have tried generating several thumbs of an image. First page load takes around 3 seconds, every following page load around 3 milliseconds.

@robert @Andi-Lo Do you have a simple test-case based on the starterkit that is able to reproduce this issue?

@lukasbestle drop me a pm and we discuss things further.

Feel free to send me one with more information. You can also drop me an email at lukas@getkirby. Thanks. :slight_smile:

@lukasbestle As you mentioned, the first load took ā€œ3 secondsā€, the following page load around ā€œ3 millisecondsā€. When you wait a while (a longer period), the next page load again lasts ā€œ3 secondsā€ - then the next page load ā€œ3 millisecondsā€ - and so on. (The ā€œsecondsā€ depends on the host, which is running the scripts)

After a hint in another post (Super slow frontend and panel not loading at all), I changed von PHP 7 back to 5.6 and activated fast-cgi.

This helped in that way, that the (first) load of the pictures lasts only a tenth of the time required before. But the first load of one (!) picture (of a dozen on this page) requires still approx. 500 milliseconds.

@robert:
Are you sure, that the PHP process runs 24 h a day?

On some cheap installations, the PHP process dies after some time of not running. The PHP process then needs to start for the ā€œfirstā€ webpage, what needs some time. The next webpage is very much fasterā€¦