Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use thumbnail when possible #27

Open
jcupitt opened this issue Dec 8, 2023 · 0 comments
Open

Use thumbnail when possible #27

jcupitt opened this issue Dec 8, 2023 · 0 comments

Comments

@jcupitt
Copy link

jcupitt commented Dec 8, 2023

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:

https://github.com/osiemsiedem/intervention-image-vips-driver/blob/master/src/Commands/BrightnessCommand.php#L25-L33

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant