Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getCropper(name) {
// See http://sharp.dimens.io/en/stable/api-resize/#crop
//
// Possible attributes of sharp.gravity are north, northeast, east, southeast, south,
// southwest, west, northwest, center and centre.
if (sharp.gravity[name] !== undefined) {
return sharp.gravity[name];
}
// The experimental strategy-based approach resizes so one dimension is at its target
// length then repeatedly ranks edge regions, discarding the edge with the lowest
// score based on the selected strategy.
// - entropy: focus on the region with the highest Shannon entropy.
// - attention: focus on the region with the highest luminance frequency,
// colour saturation and presence of skin tones.
if (sharp.strategy[name] !== undefined) {
return sharp.strategy[name];
}
throw new UsageError(
`"${name}" is not a valid crop value. Consult http://sharp.dimens.io/en/stable/api-resize/#crop`
);
}
// Exports.
module.exports = {
BAND: ['red', 'green', 'blue'],
BLEND: Object.keys(sharp.blend),
BOOL: Object.keys(sharp.bool),
COLOURSPACE: Object.keys(sharp.colourspace),
CONTAINER: ['fs', 'zip'],
DEPTH: ['onepixel', 'onetile', 'one'],
FIT: Object.keys(sharp.fit),
FORMAT: ['heif', 'jpeg', 'jpg', 'png', 'raw', 'tiff', 'webp'],
GRAVITY: Object.keys(sharp.gravity),
HEIF_COMPRESSION: ['hevc', 'avc', 'jpeg', 'av1'],
KERNEL: Object.keys(sharp.kernel),
LAYOUT: ['dz', 'google', 'zoomify'],
POSITION: Object.keys(sharp.position),
STRATEGY: Object.keys(sharp.strategy),
TIFF_COMPRESSION: ['ccittfax4', 'deflate', 'jpeg', 'lzw', 'none'],
TIFF_PREDICTOR: ['float', 'horizontal', 'none']
}
function getCropper(name) {
// See http://sharp.dimens.io/en/stable/api-resize/#crop
//
// Possible attributes of sharp.gravity are north, northeast, east, southeast, south,
// southwest, west, northwest, center and centre.
if (sharp.gravity[name] !== undefined) {
return sharp.gravity[name];
}
// The experimental strategy-based approach resizes so one dimension is at its target
// length then repeatedly ranks edge regions, discarding the edge with the lowest
// score based on the selected strategy.
// - entropy: focus on the region with the highest Shannon entropy.
// - attention: focus on the region with the highest luminance frequency,
// colour saturation and presence of skin tones.
if (sharp.strategy[name] !== undefined) {
return sharp.strategy[name];
}
throw new UsageError(
`"${name}" is not a valid crop value. Consult http://sharp.dimens.io/en/stable/api-resize/#crop`
);
}
function processImageWithSharp(file, callback) {
const name = file.filename;
const resizeOpt = {
width: 1600,
height: 1800,
type: 'jpeg',
quality: 95
};
const thumbnailOpt = {
width: 80,
height: 60,
entropy: sharp.strategy.entropy
};
const image1 = sharp(file.path);
image1.metadata(function (error, data) {
// calculate image aspect and resize if image size is over
if (data.width > resizeOpt.width) {
if (image1.height > resizeOpt.height) {
image1.resize(null, resizeOpt.height);
} else {
image1.resize(resizeOpt.width, null);
}
} else if (data.height > resizeOpt.height * 2) {
image1.resize(null, resizeOpt.height);
}
productsApi.put('/upload', upload.single('product_image'), (req, res) => {
const productId = req.body.product_id;
const filename = productId + Date.now();
sharp(req.file.buffer)
.resize(200, 200)
.crop(sharp.strategy.entropy)
.toFile(`${__dirname}/../public/uploads/${filename}`, (err) => {
if (err) {
console.error('woops', err);
}
inMemoryProducts[productId].imageUrl = `http://localhost:3000/uploads/${filename}`;
inMemoryProducts[productId].imageName = req.file.originalname;
res.status(201);
res.send({
data: req.file.originalname,
});
});
});
presets: ImageTransformPreset[],
): Promise {
let width = +queryParams.w || undefined;
let height = +queryParams.h || undefined;
let mode = queryParams.mode || 'crop';
if (queryParams.preset) {
const matchingPreset = presets.find(p => p.name === queryParams.preset);
if (matchingPreset) {
width = matchingPreset.width;
height = matchingPreset.height;
mode = matchingPreset.mode;
}
}
const options: ResizeOptions = {};
if (mode === 'crop') {
options.position = sharp.strategy.entropy;
} else {
options.fit = 'inside';
}
return sharp(originalImage).resize(width, height, options);
}
presets: ImageTransformPreset[],
): Promise {
let width = +queryParams.w || undefined;
let height = +queryParams.h || undefined;
let mode = queryParams.mode || 'crop';
if (queryParams.preset) {
const matchingPreset = presets.find(p => p.name === queryParams.preset);
if (matchingPreset) {
width = matchingPreset.width;
height = matchingPreset.height;
mode = matchingPreset.mode;
}
}
const options: ResizeOptions = {};
if (mode === 'crop') {
options.position = sharp.strategy.entropy;
} else {
options.fit = 'inside';
}
return sharp(originalImage).resize(width, height, options);
}
return res.status(400).json({
error: {
status: res.status,
title: 'An error occured during file upload',
detail: [{
msg: errorMsg,
param: err.field
}
],
meta: req.body
}
});
}
if(req.file) {
sharp(req.file.path).resize(300, 300).crop(sharp.strategy.entropy).toFile('uploads/medium-' + req.file.filename, function (err) {
if (err) {
return next(err);
}
});
}
next();
});
}
sizes.map(async size => {
let image = sharp(fromPath).resize(size.w, size.h);
if (fill) {
image = image.background(fill).embed();
} else {
image = image.crop(sharp.strategy.entropy);
}
await image
.png()
.toFile(path.resolve(destPath, `splash-${size.w}x${size.h}.png`));
htmlSplash.push(``);
})
);