Skip to content

Conversation

@sam365724
Copy link
Contributor

No description provided.

@bpatrik
Copy link
Owner

bpatrik commented Nov 21, 2022

I spent some time with this:
doing something like this:


const Logger_1 = require("./src/backend/Logger");
const run = async () => {
    const sharp = require('sharp');
    const input = {
        mediaPath: '<some path>\\img.jpg',
        size: 50,
    };
    sharp.cache(false);
    const image = sharp(input.mediaPath, { failOnError: false });
    const metadata = await image.metadata();
    const kernel = sharp.kernel.lanczos3;
    image.resize(input.size, input.size, {
        kernel,
        position: sharp.gravity.centre,
        fit: 'cover',
    });
    await image.withMetadata().jpeg({ mozjpeg: true }).toFile('test_mozjpeg.jpg');
    await image.withMetadata().jpeg().withMetadata({ exif: {
            IFD0: {
                Copyright: 'Wernham Hogg'
            }
        }
    }).toFile('test_withMetadata.jpg');
  await image.withMetadata().webp({ effort: 6, quality: 60 }).toFile('test_60.webp');
  await image.withMetadata().webp({ effort: 6, quality: 100 }).toFile('test_100.webp');
  await image.withMetadata().png().toFile('test.png');
};
run();

Interistingly:
test.png - 13kb
test_60.webp - 36kb
test_100.webp - 38kb
test_mozjpeg.jpg - 49,3 kb
test_withMetadata - 49,4 kb

To my understanding png won because that does not contain any metadata which can be quite big

So I think switching to webp only helped because sharp removed some metadata, but not all. But looking at the webp file it still has some. If I manually delete the XMP data from webp it reduces to 24kb.

@bpatrik
Copy link
Owner

bpatrik commented Nov 21, 2022

I totally missed on my previous comment that I'm calling withMetadata() in each line....
Removing it, the filesize drops to 1-2KB for each format.

output:
png - 7,13KB
web 100 2,20kb
jpg 1,06kb
webp 80 786
webp 60 546kb

So changing thumbnails to webp sound good to me

@bpatrik bpatrik merged commit 70d1a04 into bpatrik:master Nov 21, 2022
bpatrik added a commit that referenced this pull request Nov 21, 2022
bpatrik added a commit that referenced this pull request Nov 21, 2022
@sam365724
Copy link
Contributor Author

@bpatrik
OK, cool. Do we need metadata for the thumbnails, correct? Because in my code I use withMetadata() as before.

@bpatrik
Copy link
Owner

bpatrik commented Nov 22, 2022

Metadata is not needed for the thumbnails. I already removed that after merging this PR.

@bpatrik bpatrik added this to the v1.9.5 milestone Nov 22, 2022
bpatrik added a commit that referenced this pull request Nov 25, 2022
@4gra
Copy link

4gra commented Jan 30, 2023

This is a fantastic change and contributes to a performance boost that's frankly staggering to see in action -- thank you 👍

I've just added a compatibility note as an issue in #603 as it's broken an old client of ours. Not a huge problem.

bpatrik added a commit that referenced this pull request Mar 28, 2023
bpatrik added a commit that referenced this pull request Mar 29, 2023
bpatrik added a commit that referenced this pull request Mar 30, 2023
bpatrik added a commit that referenced this pull request Mar 30, 2023
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

Successfully merging this pull request may close these issues.

3 participants