Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exports.resize = async (req, res, next) => {
// check if there is no new file to resize
if (!req.file) {
next(); // skip to the next middleware
return;
}
const extension = req.file.mimetype.split('/')[1];
req.body.photo = `${uuid.v4()}.${extension}`;
// now we resize
const photo = await jimp.read(req.file.buffer);
await photo.resize(800, jimp.AUTO);
await photo.write(`./public/uploads/${req.body.photo}`);
// once we have written the photo to our filesystem, keep going!
next();
};
exports.resize = async (req, res, next) => {
if (req.body.caption && req.body.caption.length > 140) {
req.flash('error', 'Upload failed, apparently you have written too much')
return res.redirect('back')
}
//rename
const extension = req.file.mimetype.split('/')[1];
req.body.url = crypto.randomBytes(10).toString('hex');
req.body.photo = `${req.body.url}.${extension}`;
// resize
const photo = await jimp.read(req.file.buffer);
await photo.resize(600, jimp.AUTO).quality(70);
await photo.write(`./public/uploads/${req.body.photo}`);
await photo.cover(290, 290, jimp.HORIZONTAL_ALIGN_CENTER | jimp.VERTICAL_ALIGN_MIDDLE);
await photo.write(`./public/uploads/gallery/${req.body.photo}`);
next();
}
readImage.then(image => {
image.clone()
.resize(width, jimp.AUTO)
.quality(options.quality)
.background(parseInt(options.background, 16) || 0xFFFFFFFF)
.getBuffer(mime, function(err, data) { // eslint-disable-line func-names
if (err) {
reject(err);
} else {
resolve({
data,
width,
height: this.bitmap.height
});
}
});
});
})
.then(image => {
var width = image.bitmap.width,
height = image.bitmap.height,
pad = Utils.computeAspectRatioPadding(width, height, ratio);
// round paddings to behave like lwip
let wDiff = parseInt((2*pad.left));
let hDiff = parseInt((2*pad.top));
image = image.contain(width + wDiff, height + hDiff);
return Q.ninvoke(image, 'getBuffer', Jimp.AUTO);
});
};
const process = async function(i) {
if(i < files.length ) {
const file = files[i]
const regex = /\.(jpe?g||png)$/
if (
regex.test(file) == false ||
file.includes('.lazy') ||
file.includes('.blur')
) return process(i + 1)
console.log('Lazying:', folder + file)
const image = await Jimp.read(folder + file).catch((err) => { return console.log(err) })
image.resize(1280, Jimp.AUTO)
image.quality(90)
const prefix = file.split(regex)[0]
const suffix = file.split(regex)[1]
const saveName = prefix + '.lazy.' + suffix
const save = await image.write(folder + saveName)
process(i + 1)
}
}
process(0)
return Jimp.read(_media, (err, image) => {
if (err) {
return CallBack(err);
}
const uu = image.bitmap;
if (uu.height > uu.width) {
image.resize(Jimp.AUTO, imageMaxHeight);
}
else {
image.resize(imageMaxWidth, Jimp.AUTO);
}
if (/\/PNG/i.test(type)) {
return image.deflateStrategy(1, () => {
return exportImage(type, image);
});
}
if (/\/(JPEG|JPG)/i.test(type)) {
return image.quality(100, () => {
return exportImage(type, image);
});
}
// BMP and all other to PNG
ret.media_type = 'image/png';
return image.deflateStrategy(4, () => {
return exportImage(ret.media_type, image);
});
Jimp.read(originalPath, function (err, image) {
if (err) {
reject(err);
}
console.log('JIMP COVER', finalWidth, ' x ', finalHeight);
if (finalWidth === Jimp.AUTO || finalHeight === Jimp.AUTO) {
image.resize(finalWidth, finalHeight)
.quality(imagesQuality)
.write(destinationPath, function() {
resolve(destinationPath);
});
} else {
image.cover(finalWidth, finalHeight)
.quality(imagesQuality)
.write(destinationPath, function() {
resolve(destinationPath);
});
}
}).catch(err => {
console.log(err);
return Jimp.read ( _media, ( err, image ) => {
if ( err ) {
return CallBack ( err )
}
const uu = image.bitmap
if ( uu.height > uu.width ) {
image.resize ( Jimp.AUTO, tweetImageMaxHeight )
} else {
image.resize ( tweetImageMaxWidth, Jimp.AUTO )
}
if ( /\/PNG/i.test ( type )) {
return image.deflateStrategy ( 1, () => {
return exportImage ( type, image )
})
}
if ( /\/(JPEG|JPG)/i.test ( type )) {
return image.quality ( 100, () => {
return exportImage ( type, image )
})
}
// BMP and all other to PNG
ret.media_type = 'image/png'
return image.deflateStrategy ( 4, () => {
const fn = (resolve, reject) => (input.img ? input.img : input).getBuffer(jimp.AUTO, (e, b) => (e ? reject(e) : resolve(b)));
return new Promise(fn).then(buffer => jimp.read(buffer));
function resizeSquared(img: Jimp, _w: number, _h: number) {
let w;
let h;
if (_h > _w) {
w = Jimp.AUTO;
h = _h;
} else {
w = _w;
h = Jimp.AUTO;
}
return img.resize(w, h);
}