Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Package modules.
const sharp = require('sharp')
// 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']
}
async function loadImage(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to load ${url}`);
}
const buffer = await response.buffer();
debug(`loaded url ${url}`);
const {data, info} = await sharp(buffer)
.resize(undefined, VERTICAL_RESOLUTION, {
kernel: sharp.kernel.lanczos2
})
.ensureAlpha()
.raw()
.toBuffer({resolveWithObject: true});
console.log(info, data.length);
const resized_u8c4 = new jsfeat.matrix_t(
info.width, info.height, jsfeat.U8_t | jsfeat.C4_t,
new jsfeat.data_t(info.width * info.height, data));
const norm_u8 = normalize(grayscale(resized_u8c4));
return {
url,
original_u8c4: resized_u8c4,
norm_u8,
width: resized_u8c4.cols,
getData: function(image) {
var options = { kernel: sharp.kernel.cubic };
return image._sharp
.resize(image.width, image.height, options)
.raw()
.toBuffer()
.then(function(data) {
if (data.length === image.width * image.height * 3) {
data = rgb2rgba(data);
}
if (data.length !== image.width * image.height * 4) {
throw new Error('unexpected data length ' + data.length);
}
return new smartcrop.ImgData(image.width, image.height, data);
});
}
};
return async (input: RendererInput): Promise => {
Logger.silly('[SharpRenderer] rendering photo:' + input.mediaPath + ', size:' + input.size);
const image: Sharp = sharp(input.mediaPath, {failOnError: false});
const metadata: Metadata = await image.metadata();
/**
* newWidth * newHeight = size*size
* newHeight/newWidth = height/width
*
* newHeight = (height/width)*newWidth
* newWidth * newWidth = (size*size) / (height/width)
*
* @type {number}
*/
const ratio = metadata.height / metadata.width;
const kernel = input.qualityPriority === true ? sharp.kernel.lanczos3 : sharp.kernel.nearest;
if (input.cut) {
image.extract(input.cut);
}
if (input.makeSquare === false) {
if (metadata.height > metadata.width) {
image.resize(Math.min(input.size, metadata.width), null, {
kernel: kernel
});
} else {
image.resize(null, Math.min(input.size, metadata.height), {
kernel: kernel
});
}
async function scale(this: sharp.Sharp, ...args: any[]) {
const opts = typeof args[args.length - 1] === "object" ? args[args.length - 1] : undefined
const sharpOpts = {
kernel: sharp.kernel.lanczos3,
fastShrinkOnLoad: true
}
const fit = opts && opts.fit
if (opts) {
sharpOpts.kernel = opts.kernel
sharpOpts.fastShrinkOnLoad = opts.fastShrinkOnLoad
}
let width = typeof args[0] === "number" ? args[0] : undefined
let height = typeof args[1] === "number" ? args[1] : undefined
const ignoreAspectRatio = typeof opts === "object" && opts.ignoreAspectRatio === true
const withoutEnlargement = typeof opts === "object" && opts.allowEnlargement === false
if (!width || !height) {
const meta = await this.metadata()
const resize = (opts={}) => {
const kernelOption = {};
if (opts.kernel) {
kernelOption.kernel = sharp.kernel[opts.kernel];
}
const retinaPrefix = typeof opts.prefix !== 'undefined' ? opts.prefix : '@';
const retinaSuffix = typeof opts.suffix !== 'undefined' ? opts.suffix : 'x';
return function resize(input, fulfill) {
let finish = -input.outputs.length + 1;
const next = () => {
finish++;
if (finish > 0) {
fulfill(input);
}
};
if (Array.isArray(opts.retina)) {
for (let i = 0, l = input.outputs.length; i < l; i++) {
for (let multiple of opts.retina) {
function transform(response, next) {
sharp(response.Body).resize(MAX_WIDTH, MAX_HEIGHT, {
kernel: sharp.kernel.nearest,
fit: 'inside',
position: 'right top'
})
.toBuffer(imageType, function (err, buffer) {
if (err) {
next(err);
} else {
next(null, response.ContentType, buffer);
}
});
},