Skip to content

Commit

Permalink
witch pngquant
Browse files Browse the repository at this point in the history
  • Loading branch information
joehoyle committed Nov 20, 2023
1 parent 295d23b commit 5f28ee2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"@aws-sdk/client-s3": "^3.378.0",
"eslint-config-react-app": "^7.0.1",
"sharp": "^0.32.4",
"imagemin-pngquant": "^8.0.0",
"smartcrop-sharp": "^2.0.6"
},
"devDependencies": {
Expand Down
14 changes: 5 additions & 9 deletions src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { S3Client, S3ClientConfig, GetObjectCommand, GetObjectCommandOutput } from '@aws-sdk/client-s3';
import imageminPngquant from 'imagemin-pngquant';
import sharp from 'sharp';
import smartcrop from 'smartcrop-sharp';

Expand Down Expand Up @@ -318,6 +317,11 @@ export async function resizeBuffer(
image.jpeg( {
quality: Math.round( clamp( args.quality, 0, 100 ) ),
} );
} else if ( metadata.format === 'png' ) {
// Compress the PNG.
image.png( {
palette: true,
} );
}

// send image
Expand All @@ -326,14 +330,6 @@ export async function resizeBuffer(
if ( err ) {
reject( err );
}
// Pass PNG images through PNGQuant as Sharp is not good at compressing them.
// See https://github.com/lovell/sharp/issues/478
if ( info.format === 'png' && metadata.pages! > 1 ) {
data = await imageminPngquant()( data );
// Make sure we update the size in the info, to reflect the new
// size after lossless-compression.
info.size = data.length;
}

// add invalid args
resolve( {
Expand Down

0 comments on commit 5f28ee2

Please sign in to comment.