Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fpxLeft = newWidth - width
} else if (fpxLeft < 0) {
fpxLeft = 0
}
// adjust focal point y
if (fpyTop + height > newHeight) {
fpyTop = newHeight - height
} else if (fpyTop < 0) {
fpyTop = 0
}
image.resize({
width: newWidth,
height: newHeight,
withoutEnlargement: false,
fit: sharp.fit.fill
}).extract({
left: fpxLeft,
top: fpyTop,
width,
height
})
}
async transformWrite(fileRecord) {
if (!transform) return null;
const { size, fit, format, type } = transform;
// Need to update the content type and extension of the file info, too.
// The new size gets set correctly automatically by FileCollections package.
fileRecord.type(type, { store: name });
fileRecord.extension(format, { store: name });
// resizing image, adding fit, setting output format
return sharp()
.resize({ width: size, height: size, fit: sharp.fit[fit], withoutEnlargement: true })
.toFormat(format)
.on("error", (err) => {
throw new ReactionError("error-sharp-resize-internal", err);
});
}
})
exports.fill = async (image, mode, width = null, height = null, color = null) => {
const resizeParams = {
withoutEnlargement: false,
fit: sharp.fit.contain
}
if (width) {
resizeParams.width = width
}
if (height) {
resizeParams.height = height
}
// TODO: Validate color more explicitly
if (color) {
resizeParams.background = color
}
image.resize(resizeParams)
}
export async function resize (buffer, width, height) {
try {
const transformer = sharp(buffer);
if (width || height) {
const options = {
withoutEnlargement: true,
fit: sharp.fit.inside
}
transformer.resize(width, height, options)
}
return transformer.toBuffer();
} catch (err) {
console.log(err);
}
}
height: parseInt(options.height, 10) || null,
jpegProgressive: true
}
const plugins = []
let pipeline = sharp(buffer)
if (
(config.width && config.width <= width) ||
(config.height && config.height <= height)
) {
const resizeOptions = {}
if (config.height) resizeOptions.height = config.height
if (config.width) resizeOptions.width = config.width
if (options.fit) resizeOptions.fit = sharp.fit[options.fit]
if (options.position) resizeOptions.position = sharp.position[options.position]
if (options.background && colorString.get(options.background)) {
resizeOptions.background = options.background
} else if (backgroundColor) {
resizeOptions.background = backgroundColor
}
pipeline = pipeline.resize(resizeOptions)
}
if (/\.png$/.test(ext)) {
const quality = config.quality / 100
pipeline = pipeline.png({
compressionLevel: config.pngCompressionLevel,
adaptiveFiltering: false
if (!message.attachment || !message.attachment.data) {
bot.error('No image provided.');
return;
}
let processed = message;
if (removeBg) {
processed = await bot.receive(bot.copy(message).text('rembg'));
if (!processed || !processed.attachment || !processed.attachment.data) {
processed = message;
}
}
let fit = message.text === "fill" ? sharp.fit.fill : sharp.fit.cover;
let position = fit === sharp.fit.cover ? sharp.gravity.north : sharp.gravity.center;
let data = processed.attachment.data.split(',')[1];
let sharpInstance = sharp(Buffer.from(data, 'base64')).ensureAlpha();
if (trim) {
sharpInstance = sharpInstance.trim(5);
}
let stickerData = await sharpInstance
.resize(512, 512, {
fit,
position,
background: { r: 255, g: 255, b: 255, alpha: 0 }
})
.webp()
.toBuffer();
.then(svgBuffer =>
sharp(svgBuffer)
.resize({
background,
width,
height,
fit: sharp.fit.contain
})
.toBuffer()
)
const resizeAndConvertImage = (size: number) =>
sharp(originalFileBuffer)
.resize(size, size, {
withoutEnlargement: true,
fit: sharp.fit.inside,
})
.toFormat(sharp.format.webp)
.toBuffer();
function upscale(detail, ext) {
if (ext === 'png') {
return sharp().resize({
width: detail.width,
height: detail.height,
fit: sharp.fit.inside
}).png().withMetadata();
}
return sharp().resize({
width: detail.width,
height: detail.height,
fit: sharp.fit.inside
}).jpeg({
quality: 93,
chromaSubsampling: '4:4:4',
}).withMetadata();
}
exports.scaleMax = (image, width = null, height = null) => {
image.resize({
width,
height,
withoutEnlargement: true,
fit: sharp.fit.inside
})
}