mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-21 15:26:16 -04:00
Add error handling
This commit is contained in:
parent
4a7ea469d4
commit
1031429550
14 changed files with 348 additions and 144 deletions
|
@ -53,21 +53,29 @@ class BlurImage extends Operation {
|
||||||
const type = Magic.magicFileType(input);
|
const type = Magic.magicFileType(input);
|
||||||
|
|
||||||
if (type && type.mime.indexOf("image") === 0){
|
if (type && type.mime.indexOf("image") === 0){
|
||||||
const image = await jimp.read(Buffer.from(input));
|
let image;
|
||||||
|
try {
|
||||||
switch (blurType){
|
image = await jimp.read(Buffer.from(input));
|
||||||
case "Fast":
|
} catch (err) {
|
||||||
image.blur(blurAmount);
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
break;
|
|
||||||
case "Gaussian":
|
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
|
||||||
self.sendStatusMessage("Gaussian blurring image. This will take a while...");
|
|
||||||
image.gaussian(blurAmount);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
switch (blurType){
|
||||||
|
case "Fast":
|
||||||
|
image.blur(blurAmount);
|
||||||
|
break;
|
||||||
|
case "Gaussian":
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Gaussian blurring image. This will take a while...");
|
||||||
|
image.gaussian(blurAmount);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
return [...imageBuffer];
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error blurring image. (${err})`);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,13 +106,21 @@ class ContainImage extends Operation {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const image = await jimp.read(Buffer.from(input));
|
let image;
|
||||||
|
try {
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
image = await jimp.read(Buffer.from(input));
|
||||||
self.sendStatusMessage("Containing image...");
|
} catch (err) {
|
||||||
image.contain(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
}
|
||||||
return [...imageBuffer];
|
try {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Containing image...");
|
||||||
|
image.contain(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
|
||||||
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error containing image. (${err})`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -106,12 +106,21 @@ class CoverImage extends Operation {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const image = await jimp.read(Buffer.from(input));
|
let image;
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
try {
|
||||||
self.sendStatusMessage("Covering image...");
|
image = await jimp.read(Buffer.from(input));
|
||||||
image.cover(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
|
} catch (err) {
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
return [...imageBuffer];
|
}
|
||||||
|
try {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Covering image...");
|
||||||
|
image.cover(width, height, alignMap[hAlign] | alignMap[vAlign], resizeMap[alg]);
|
||||||
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error covering image. (${err})`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -98,22 +98,31 @@ class CropImage extends Operation {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const image = await jimp.read(Buffer.from(input));
|
let image;
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
try {
|
||||||
self.sendStatusMessage("Cropping image...");
|
image = await jimp.read(Buffer.from(input));
|
||||||
if (autocrop) {
|
} catch (err) {
|
||||||
image.autocrop({
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
tolerance: (autoTolerance / 100),
|
|
||||||
cropOnlyFrames: autoFrames,
|
|
||||||
cropSymmetric: autoSymmetric,
|
|
||||||
leaveBorder: autoBorder
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
image.crop(xPos, yPos, width, height);
|
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Cropping image...");
|
||||||
|
if (autocrop) {
|
||||||
|
image.autocrop({
|
||||||
|
tolerance: (autoTolerance / 100),
|
||||||
|
cropOnlyFrames: autoFrames,
|
||||||
|
cropSymmetric: autoSymmetric,
|
||||||
|
leaveBorder: autoBorder
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
image.crop(xPos, yPos, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
return [...imageBuffer];
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error cropping image. (${err})`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -40,12 +40,21 @@ class DitherImage extends Operation {
|
||||||
const type = Magic.magicFileType(input);
|
const type = Magic.magicFileType(input);
|
||||||
|
|
||||||
if (type && type.mime.indexOf("image") === 0){
|
if (type && type.mime.indexOf("image") === 0){
|
||||||
const image = await jimp.read(Buffer.from(input));
|
let image;
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
try {
|
||||||
self.sendStatusMessage("Applying dither to image...");
|
image = await jimp.read(Buffer.from(input));
|
||||||
image.dither565();
|
} catch (err) {
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
return [...imageBuffer];
|
}
|
||||||
|
try {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Applying dither to image...");
|
||||||
|
image.dither565();
|
||||||
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error applying dither to image. (${err})`);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,21 +49,29 @@ class FlipImage extends Operation {
|
||||||
throw new OperationError("Invalid input file type.");
|
throw new OperationError("Invalid input file type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const image = await jimp.read(Buffer.from(input));
|
let image;
|
||||||
|
try {
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
image = await jimp.read(Buffer.from(input));
|
||||||
self.sendStatusMessage("Flipping image...");
|
} catch (err) {
|
||||||
switch (flipAxis){
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
case "Horizontal":
|
|
||||||
image.flip(true, false);
|
|
||||||
break;
|
|
||||||
case "Vertical":
|
|
||||||
image.flip(false, true);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Flipping image...");
|
||||||
|
switch (flipAxis){
|
||||||
|
case "Horizontal":
|
||||||
|
image.flip(true, false);
|
||||||
|
break;
|
||||||
|
case "Vertical":
|
||||||
|
image.flip(false, true);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
return [...imageBuffer];
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error flipping image. (${err})`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -58,20 +58,29 @@ class ImageBrightnessContrast extends Operation {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const image = await jimp.read(Buffer.from(input));
|
let image;
|
||||||
if (brightness !== 0) {
|
try {
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
image = await jimp.read(Buffer.from(input));
|
||||||
self.sendStatusMessage("Changing image brightness...");
|
} catch (err) {
|
||||||
image.brightness(brightness / 100);
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
}
|
|
||||||
if (contrast !== 0) {
|
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
|
||||||
self.sendStatusMessage("Changing image contrast...");
|
|
||||||
image.contrast(contrast / 100);
|
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
if (brightness !== 0) {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Changing image brightness...");
|
||||||
|
image.brightness(brightness / 100);
|
||||||
|
}
|
||||||
|
if (contrast !== 0) {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Changing image contrast...");
|
||||||
|
image.contrast(contrast / 100);
|
||||||
|
}
|
||||||
|
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
return [...imageBuffer];
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error adjusting image brightness / contrast. (${err})`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -52,17 +52,26 @@ class ImageFilter extends Operation {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const image = await jimp.read(Buffer.from(input));
|
let image;
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
try {
|
||||||
self.sendStatusMessage("Applying " + filterType.toLowerCase() + " filter to image...");
|
image = await jimp.read(Buffer.from(input));
|
||||||
if (filterType === "Greyscale") {
|
} catch (err) {
|
||||||
image.greyscale();
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
} else {
|
|
||||||
image.sepia();
|
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Applying " + filterType.toLowerCase() + " filter to image...");
|
||||||
|
if (filterType === "Greyscale") {
|
||||||
|
image.greyscale();
|
||||||
|
} else {
|
||||||
|
image.sepia();
|
||||||
|
}
|
||||||
|
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
return [...imageBuffer];
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error applying filter to image. (${err})`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -66,40 +66,48 @@ class ImageHueSaturationLightness extends Operation {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const image = await jimp.read(Buffer.from(input));
|
let image;
|
||||||
|
try {
|
||||||
if (hue !== 0) {
|
image = await jimp.read(Buffer.from(input));
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
} catch (err) {
|
||||||
self.sendStatusMessage("Changing image hue...");
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
image.colour([
|
|
||||||
{
|
|
||||||
apply: "hue",
|
|
||||||
params: [hue]
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
if (saturation !== 0) {
|
try {
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
if (hue !== 0) {
|
||||||
self.sendStatusMessage("Changing image saturation...");
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
image.colour([
|
self.sendStatusMessage("Changing image hue...");
|
||||||
{
|
image.colour([
|
||||||
apply: "saturate",
|
{
|
||||||
params: [saturation]
|
apply: "hue",
|
||||||
}
|
params: [hue]
|
||||||
]);
|
}
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
if (saturation !== 0) {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Changing image saturation...");
|
||||||
|
image.colour([
|
||||||
|
{
|
||||||
|
apply: "saturate",
|
||||||
|
params: [saturation]
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
if (lightness !== 0) {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Changing image lightness...");
|
||||||
|
image.colour([
|
||||||
|
{
|
||||||
|
apply: "lighten",
|
||||||
|
params: [lightness]
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error adjusting image hue / saturation / lightness. (${err})`);
|
||||||
}
|
}
|
||||||
if (lightness !== 0) {
|
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
|
||||||
self.sendStatusMessage("Changing image lightness...");
|
|
||||||
image.colour([
|
|
||||||
{
|
|
||||||
apply: "lighten",
|
|
||||||
params: [lightness]
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
|
||||||
return [...imageBuffer];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -51,13 +51,22 @@ class ImageOpacity extends Operation {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const image = await jimp.read(Buffer.from(input));
|
let image;
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
try {
|
||||||
self.sendStatusMessage("Changing image opacity...");
|
image = await jimp.read(Buffer.from(input));
|
||||||
image.opacity(opacity / 100);
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Changing image opacity...");
|
||||||
|
image.opacity(opacity / 100);
|
||||||
|
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
|
const imageBuffer = await image.getBufferAsync(jimp.MIME_PNG);
|
||||||
return [...imageBuffer];
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperateionError(`Error changing image opacity. (${err})`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -41,12 +41,22 @@ class InvertImage extends Operation {
|
||||||
if (!type || type.mime.indexOf("image") !== 0) {
|
if (!type || type.mime.indexOf("image") !== 0) {
|
||||||
throw new OperationError("Invalid input file format.");
|
throw new OperationError("Invalid input file format.");
|
||||||
}
|
}
|
||||||
const image = await jimp.read(Buffer.from(input));
|
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
let image;
|
||||||
self.sendStatusMessage("Inverting image...");
|
try {
|
||||||
image.invert();
|
image = await jimp.read(Buffer.from(input));
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
} catch (err) {
|
||||||
return [...imageBuffer];
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Inverting image...");
|
||||||
|
image.invert();
|
||||||
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error inverting image. (${err})`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
91
src/core/operations/NormaliseImage.mjs
Normal file
91
src/core/operations/NormaliseImage.mjs
Normal file
|
@ -0,0 +1,91 @@
|
||||||
|
/**
|
||||||
|
* @author j433866 [j433866@gmail.com]
|
||||||
|
* @copyright Crown Copyright 2019
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Operation from "../Operation";
|
||||||
|
import OperationError from "../errors/OperationError";
|
||||||
|
import Magic from "../lib/Magic";
|
||||||
|
import { toBase64 } from "../lib/Base64";
|
||||||
|
import jimp from "jimp";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalise Image operation
|
||||||
|
*/
|
||||||
|
class NormaliseImage extends Operation {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NormaliseImage constructor
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.name = "Normalise Image";
|
||||||
|
this.module = "Image";
|
||||||
|
this.description = "Normalise the image colours.";
|
||||||
|
this.infoURL = "";
|
||||||
|
this.inputType = "byteArray";
|
||||||
|
this.outputType = "byteArray";
|
||||||
|
this.presentType= "html";
|
||||||
|
this.args = [
|
||||||
|
/* Example arguments. See the project wiki for full details.
|
||||||
|
{
|
||||||
|
name: "First arg",
|
||||||
|
type: "string",
|
||||||
|
value: "Don't Panic"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Second arg",
|
||||||
|
type: "number",
|
||||||
|
value: 42
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {byteArray} input
|
||||||
|
* @param {Object[]} args
|
||||||
|
* @returns {byteArray}
|
||||||
|
*/
|
||||||
|
async run(input, args) {
|
||||||
|
// const [firstArg, secondArg] = args;
|
||||||
|
const type = Magic.magicFileType(input);
|
||||||
|
|
||||||
|
if (!type || type.mime.indexOf("image") !== 0){
|
||||||
|
throw new OperationError("Invalid file type.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const image = await jimp.read(Buffer.from(input));
|
||||||
|
|
||||||
|
image.normalize();
|
||||||
|
|
||||||
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
|
return [...imageBuffer];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays the normalised image using HTML for web apps
|
||||||
|
* @param {byteArray} data
|
||||||
|
* @returns {html}
|
||||||
|
*/
|
||||||
|
present(data) {
|
||||||
|
if (!data.length) return "";
|
||||||
|
|
||||||
|
let dataURI = "data:";
|
||||||
|
const type = Magic.magicFileType(data);
|
||||||
|
if (type && type.mime.indexOf("image") === 0){
|
||||||
|
dataURI += type.mime + ";";
|
||||||
|
} else {
|
||||||
|
throw new OperationError("Invalid file type.");
|
||||||
|
}
|
||||||
|
dataURI += "base64," + toBase64(data);
|
||||||
|
|
||||||
|
return "<img src='" + dataURI + "'>";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default NormaliseImage;
|
|
@ -91,23 +91,31 @@ class ResizeImage extends Operation {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const image = await jimp.read(Buffer.from(input));
|
let image;
|
||||||
|
try {
|
||||||
if (unit === "Percent") {
|
image = await jimp.read(Buffer.from(input));
|
||||||
width = image.getWidth() * (width / 100);
|
} catch (err) {
|
||||||
height = image.getHeight() * (height / 100);
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
if (unit === "Percent") {
|
||||||
|
width = image.getWidth() * (width / 100);
|
||||||
|
height = image.getHeight() * (height / 100);
|
||||||
|
}
|
||||||
|
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
self.sendStatusMessage("Resizing image...");
|
self.sendStatusMessage("Resizing image...");
|
||||||
if (aspect) {
|
if (aspect) {
|
||||||
image.scaleToFit(width, height, resizeMap[resizeAlg]);
|
image.scaleToFit(width, height, resizeMap[resizeAlg]);
|
||||||
} else {
|
} else {
|
||||||
image.resize(width, height, resizeMap[resizeAlg]);
|
image.resize(width, height, resizeMap[resizeAlg]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error resizing image. (${err})`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
|
||||||
return [...imageBuffer];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -47,12 +47,21 @@ class RotateImage extends Operation {
|
||||||
const type = Magic.magicFileType(input);
|
const type = Magic.magicFileType(input);
|
||||||
|
|
||||||
if (type && type.mime.indexOf("image") === 0){
|
if (type && type.mime.indexOf("image") === 0){
|
||||||
const image = await jimp.read(Buffer.from(input));
|
let image;
|
||||||
if (ENVIRONMENT_IS_WORKER())
|
try {
|
||||||
self.sendStatusMessage("Rotating image...");
|
image = await jimp.read(Buffer.from(input));
|
||||||
image.rotate(degrees);
|
} catch (err) {
|
||||||
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
throw new OperationError(`Error loading image. (${err})`);
|
||||||
return [...imageBuffer];
|
}
|
||||||
|
try {
|
||||||
|
if (ENVIRONMENT_IS_WORKER())
|
||||||
|
self.sendStatusMessage("Rotating image...");
|
||||||
|
image.rotate(degrees);
|
||||||
|
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
|
||||||
|
return [...imageBuffer];
|
||||||
|
} catch (err) {
|
||||||
|
throw new OperationError(`Error rotating image. (${err})`);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new OperationError("Invalid file type.");
|
throw new OperationError("Invalid file type.");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue