Skip to content

Commit

Permalink
sharp
Browse files Browse the repository at this point in the history
  • Loading branch information
orthagonal committed Oct 16, 2020
1 parent 2afb18e commit 6e10823
Show file tree
Hide file tree
Showing 4 changed files with 479 additions and 3,527 deletions.
23 changes: 3 additions & 20 deletions lib/optimize.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
const imagemin = require('imagemin');
const imageminJpegtran = require('imagemin-jpegtran');
const imageminPngquant = require('imagemin-pngquant');
const imageminSvgo = require('imagemin-svgo');
const imageminWebp = require('imagemin-webp');
const sharp = require('sharp');

module.exports = (options, buffer) => {
const plugins = [];
if (options.svgo !== false) {
plugins.push(imageminSvgo(options));
}
if (options.png !== false) {
plugins.push(imageminPngquant(options));
}
if (options.jpg !== false) {
plugins.push(imageminJpegtran());
}
if (options.webp !== false) {
plugins.push(imageminWebp());
}
module.exports = async(buffer) => {
try {
return imagemin.buffer(buffer, { plugins });
return sharp(buffer).toBuffer();
} catch (err) {
// anything other than error 99 is considered an unrecoverable error:
if (err.code !== 99) {
Expand Down
26 changes: 15 additions & 11 deletions lib/resize.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
const Jimp = require('jimp');
const TinyColor = require('tinycolor2');
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
if (width && !height) {
height = type === 'resize' ? Jimp.AUTO : width;
const resizeOptions = { fit: type };
if (width) {
resizeOptions.width = width;
}
if (height && !width) {
width = type === 'resize' ? Jimp.AUTO : height;
if (height) {
resizeOptions.height = height;
}
const jimpImage = await Jimp.read(buffer);
jimpImage[type](width, height);
if (background) {
const color = new TinyColor(background);
jimpImage.background(parseInt(color.toHex8(), 16));
resizeOptions.background = background;
}
try {
return sharp(buffer)
.resize(resizeOptions)
.toBuffer();
} catch (e) {
console.log(e);
}
return jimpImage.getBufferAsync(Jimp.AUTO);
};
Loading

0 comments on commit 6e10823

Please sign in to comment.