Tidied up image manipulation ops

This commit is contained in:
n1474335 2019-03-09 07:23:11 +00:00
parent 369b640408
commit e10d4bf45c
14 changed files with 143 additions and 236 deletions

View file

@ -6,7 +6,7 @@
import Operation from "../Operation";
import OperationError from "../errors/OperationError";
import Magic from "../lib/Magic";
import { isImage } from "../lib/FileType";
import { toBase64 } from "../lib/Base64";
import jimp from "jimp";
@ -30,13 +30,13 @@ class BlurImage extends Operation {
this.presentType = "html";
this.args = [
{
name: "Blur Amount",
name: "Amount",
type: "number",
value: 5,
min: 1
},
{
name: "Blur Type",
name: "Type",
type: "option",
value: ["Fast", "Gaussian"]
}
@ -50,56 +50,51 @@ class BlurImage extends Operation {
*/
async run(input, args) {
const [blurAmount, blurType] = args;
const type = Magic.magicFileType(input);
if (type && type.mime.indexOf("image") === 0){
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
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);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error blurring image. (${err})`);
}
} else {
if (!isImage(input)) {
throw new OperationError("Invalid file type.");
}
let image;
try {
image = await jimp.read(Buffer.from(input));
} catch (err) {
throw new OperationError(`Error loading image. (${err})`);
}
try {
switch (blurType){
case "Fast":
image.blur(blurAmount);
break;
case "Gaussian":
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Gaussian blurring image. This may take a while...");
image.gaussian(blurAmount);
break;
}
const imageBuffer = await image.getBufferAsync(jimp.AUTO);
return [...imageBuffer];
} catch (err) {
throw new OperationError(`Error blurring image. (${err})`);
}
}
/**
* Displays the blurred 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 {
const type = isImage(data);
if (!type) {
throw new OperationError("Invalid file type.");
}
dataURI += "base64," + toBase64(data);
return "<img src='" + dataURI + "'>";
return `<img src="data:${type};base64,${toBase64(data)}">`;
}
}