Skip to content

Commit

Permalink
feat(core): optimize compression
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Sep 7, 2022
1 parent 79693fd commit d27e96b
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions packages/core/src/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@ import tmp from "tmp";
const tmpFile = promisify<string>(tmp.file);

export const optimizeScreenshot = async (filepath: string) => {
const resultFilePath = await tmpFile();
await sharp(filepath)
.resize(1024, 1024, { fit: "inside", withoutEnlargement: true })
.jpeg()
.toFile(resultFilePath);
const [resultFilePath, metadata] = await Promise.all([
tmpFile(),
sharp(filepath).metadata(),
]);
const optimization = sharp(filepath).resize(2048, 20480, {
fit: "inside",
withoutEnlargement: true,
});
switch (metadata.format) {
case "jpeg":
case "jpg": {
optimization.jpeg();
break;
}
case "png": {
optimization.png();
break;
}
}
await optimization.toFile(resultFilePath);
return resultFilePath;
};

0 comments on commit d27e96b

Please sign in to comment.