-
Notifications
You must be signed in to change notification settings - Fork 10
Description
Hello, I just found this very nice project.
libvips has a special thumbnail
operation which can load and resize an image in one step. Because the two things happen together, it can exploit a lot of tricks with the various load libraries, often giving a large speedup.
For example, here's roughly what intervention-image-vips-driver
does now:
$ vipsheader nina.jpg
nina.jpg: 6048x4032 uchar, 3 bands, srgb, jpegload
$ /usr/bin/time -f %M:%e vips resize nina.jpg x.jpg 0.05
103936:0.17
104mb and 0.17s to size down to 302 pixels across. If you use thumbnail
instead:
$ /usr/bin/time -f %M:%e vips thumbnail nina.jpg x.jpg 302
44152:0.06
44mb and 0.06s -- half the memory use and almost 3x faster. In fact it's faster still, since in both cases you're paying the same fixed startup cost for libvips:
$ /usr/bin/time -f %M:%e vips
...
31232:0.02
So the real difference is 0.15s vs 0.04, almost 4x faster.
Moreover, thumbnail
knows about things like alpha and will premultiply if necessary, so you get better quality too.
I realise load+save in one operation does not fit so well with the architecture you have to work with and might need some thought :(
I noticed some other small things, for example:
You could make this faster by putting the alpha into the brightness array. Something like:
// what about mono or cmyk images? is intervention always rgb? it might be better
// to use $core->bands to get the number of image bands rather than assuming three
$brighten = [$level, $level, $level];
if ($core->hasAlpha()) {
$brighten[] = 1;
}
$core = $core->add($brighten);
Now there's no need to add and remove the alpha.