There's (potentially) a big performance win if you can use thumbnail rather than thumbnail_image.
The thumbnail operation combines load and resize in a single step, so it can exploit the various shrink-on-load hacks that many image format libraries support. These can give dramatic speedups and improve quality.
For example, with a typical 6k x 4k RGB JPG from a smartphone:
$ /usr/bin/time -f %M:%e vips thumbnail_image theo.jpg thumb.jpg 512
230976:0.19
ie. 231mb of ram, 0.2s of elapsed time, on this Ubuntu 24.10 machine with libvips 8.16. If I use thumbnail I see:
$ /usr/bin/time -f %M:%e vips thumbnail theo.jpg thumb.jpg 512
79880:0.09
Now it's 80mb of ram and 0.09s of elapsed time, three times less memory and twice as quick.
In fact it's better than that, since the vips CLI uses some ram and takes some time to start and stop:
$ /usr/bin/time -f %M:%e vips > /dev/null
30728:0.03
So 30mb of ram and 0.03s to start and stop. If we subtract this overhead and compare, thumbnail needs 4x less memory and is 4x faster.
For a possible implementation, you could:
- in
FilePathImageDecoder.php, as well as opening the image with newFromFile, you could also save the input filename in some secret member variable
- in all processing steps except resize, you could clear this secret filename member
- in
ResizeModifier.php, you could see if the filename was set and call thumbnail rather than resize
The idea being that intervention could recognise the magic open, resize sequence (surely very common) and substitute thumbnail in this case. I expect there's a better way to implement this!
You can do something similar for memory and pipe sources with thumbnail_buffer and thumbnail_source.
There's (potentially) a big performance win if you can use
thumbnailrather thanthumbnail_image.The
thumbnailoperation combines load and resize in a single step, so it can exploit the various shrink-on-load hacks that many image format libraries support. These can give dramatic speedups and improve quality.For example, with a typical 6k x 4k RGB JPG from a smartphone:
ie. 231mb of ram, 0.2s of elapsed time, on this Ubuntu 24.10 machine with libvips 8.16. If I use
thumbnailI see:Now it's 80mb of ram and 0.09s of elapsed time, three times less memory and twice as quick.
In fact it's better than that, since the
vipsCLI uses some ram and takes some time to start and stop:So 30mb of ram and 0.03s to start and stop. If we subtract this overhead and compare,
thumbnailneeds 4x less memory and is 4x faster.For a possible implementation, you could:
FilePathImageDecoder.php, as well as opening the image withnewFromFile, you could also save the input filename in some secret member variableResizeModifier.php, you could see if the filename was set and callthumbnailrather thanresizeThe idea being that intervention could recognise the magic open, resize sequence (surely very common) and substitute
thumbnailin this case. I expect there's a better way to implement this!You can do something similar for memory and pipe sources with
thumbnail_bufferandthumbnail_source.I will address this at a later date. Maybe even with a new major release of Intervention Image.
The current architecture of the parent library Intervention Image is designed to immediately load the image into memory and perform actions at each subsequent step. This is in contrast to the approach that
thumbnailrequires.I find the idea of buffering the calls to Intervention Image first and only executing them when a real image is expected as a result (encoding) or image data must be available in memory (reading colors of pixels) very exciting.
I think with this approach I can also flexibly switch internally to
thumbnailif the buffered stack only contains reading and resizing, for example.