CyberChef/src/core/operations/ResizeImage.mjs

132 lines
3.5 KiB
JavaScript
Raw Normal View History

2019-02-19 17:19:34 +01:00
/**
* @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.mjs";
import jimp from "jimp";
/**
* Resize Image operation
*/
class ResizeImage extends Operation {
/**
* ResizeImage constructor
*/
constructor() {
super();
this.name = "Resize Image";
this.module = "Image";
this.description = "Resizes an image to the specified width and height values.";
2019-03-04 14:48:13 +01:00
this.infoURL = "https://wikipedia.org/wiki/Image_scaling";
2019-02-19 17:19:34 +01:00
this.inputType = "byteArray";
this.outputType = "byteArray";
this.presentType = "html";
this.args = [
{
name: "Width",
type: "number",
2019-03-04 12:52:54 +01:00
value: 100,
min: 1
2019-02-19 17:19:34 +01:00
},
{
name: "Height",
type: "number",
2019-03-04 12:52:54 +01:00
value: 100,
min: 1
2019-02-19 17:19:34 +01:00
},
{
name: "Unit type",
type: "option",
value: ["Pixels", "Percent"]
},
{
name: "Maintain aspect ratio",
type: "boolean",
value: false
2019-02-20 16:35:53 +01:00
},
{
name: "Resizing algorithm",
type: "option",
value: [
"Nearest Neighbour",
"Bilinear",
"Bicubic",
"Hermite",
"Bezier"
],
defaultIndex: 1
2019-02-19 17:19:34 +01:00
}
];
}
/**
* @param {byteArray} input
* @param {Object[]} args
* @returns {byteArray}
*/
2019-02-20 12:26:39 +01:00
async run(input, args) {
2019-02-19 17:19:34 +01:00
let width = args[0],
height = args[1];
const unit = args[2],
aspect = args[3],
2019-02-20 16:35:53 +01:00
resizeAlg = args[4],
2019-02-19 17:19:34 +01:00
type = Magic.magicFileType(input);
2019-02-20 16:35:53 +01:00
const resizeMap = {
"Nearest Neighbour": jimp.RESIZE_NEAREST_NEIGHBOR,
"Bilinear": jimp.RESIZE_BILINEAR,
"Bicubic": jimp.RESIZE_BICUBIC,
"Hermite": jimp.RESIZE_HERMITE,
"Bezier": jimp.RESIZE_BEZIER
};
2019-02-19 17:19:34 +01:00
if (!type || type.mime.indexOf("image") !== 0){
throw new OperationError("Invalid file type.");
}
2019-02-20 12:26:39 +01:00
const image = await jimp.read(Buffer.from(input));
2019-02-19 17:19:34 +01:00
2019-02-20 12:26:39 +01:00
if (unit === "Percent") {
width = image.getWidth() * (width / 100);
height = image.getHeight() * (height / 100);
}
if (aspect) {
2019-02-20 16:35:53 +01:00
image.scaleToFit(width, height, resizeMap[resizeAlg]);
2019-02-20 12:26:39 +01:00
} else {
2019-02-20 16:35:53 +01:00
image.resize(width, height, resizeMap[resizeAlg]);
2019-02-20 12:26:39 +01:00
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
2019-02-19 17:19:34 +01:00
}
/**
* Displays the resized 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 ResizeImage;