Skip to content

Commit

Permalink
same input/output
Browse files Browse the repository at this point in the history
  • Loading branch information
orthagonal committed Oct 18, 2020
1 parent 6e10823 commit 18492f8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
12 changes: 10 additions & 2 deletions lib/optimize.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
const sharp = require('sharp');

module.exports = async(buffer) => {
module.exports = async(options, buffer) => {
try {
return sharp(buffer).toBuffer();
const result = await sharp(buffer);
if (options.png) {
return result.png(options).toBuffer();
}
if (options.webp) {
return result.webp(options).toBuffer();
}
// default is jpeg:
return result.jpeg(options).toBuffer();
} catch (err) {
// anything other than error 99 is considered an unrecoverable error:
if (err.code !== 99) {
Expand Down
1 change: 0 additions & 1 deletion lib/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const sharp = require('sharp');

module.exports = async ({ background, type = 'resize', width, height }, buffer) => {
type = type === 'resize' ? 'cover' : type;
// if only one of width/height was provided, make it a square or let Jimp decide if it is able
const resizeOptions = { fit: type };
if (width) {
resizeOptions.width = width;
Expand Down
23 changes: 6 additions & 17 deletions test/test.image.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ const fs = require('fs');
const path = require('path');
const testImageBase = 'snoopy.jpg';
const testImage = path.join(__dirname, testImageBase);
const testSVGBase = 'snoopy-pilot.svg';
const testSVG = path.join(__dirname, testSVGBase);
const testWEBPBase = 'sample.webp';
const testWEBP = path.join(__dirname, testWEBPBase);

const os = require('os');

let testImageSize = 0;
let testSVGSize = 0;
let testWEBPSize = 0;

testImageSize = fs.statSync(testImage).size;
testSVGSize = fs.statSync(testSVG).size;
testWEBPSize = fs.statSync(testWEBP).size;

// copy an image to temp
Expand All @@ -38,7 +34,7 @@ tap.test('should be able to resize an image', async t => {
background: 'green'
};
const outputFile = await optimiz.resize(options, fs.readFileSync(fileBuffer));
// fs.writeFileSync('theoutput.jpg', outputFile);
fs.writeFileSync('theoutput.jpg', outputFile);
t.ok(outputFile.length < testImageSize);
t.end();
});
Expand All @@ -54,17 +50,6 @@ tap.test('it should be able to compress an image', async t => {
t.end();
});

tap.test('it should be able to compress an svg image', async t => {
const fileBuffer = await makeTempFile(testSVG);
const options = {
quality: 50
};
const outputFile = await optimiz.optimize(options, fs.readFileSync(fileBuffer));
// fs.writeFileSync('theoutput2.svg', outputFile);
t.ok(outputFile.length < testSVGSize);
t.end();
});

tap.test('it should be able to compress a webp image', async t => {
const fileBuffer = await makeTempFile(testWEBP);
const options = {
Expand All @@ -82,8 +67,12 @@ tap.test('it should be able to thumbnail an image', async t => {
height: 10
};
const outputFile = await optimiz.resize(options, fs.readFileSync(fileBuffer));
// fs.writeFileSync('theoutput3.jpg', outputFile);
fs.writeFileSync('theoutput3.jpg', outputFile);
t.ok(outputFile.length < testImageSize);
const fileBufferWebP = await makeTempFile(testWEBP);
const outputFileWebP = await optimiz.resize(options, fs.readFileSync(fileBufferWebP));
fs.writeFileSync('theoutput3.webp', outputFileWebP);
t.ok(outputFileWebP.length < testWEBPSize);
t.end();
});

Expand Down

0 comments on commit 18492f8

Please sign in to comment.