From e10d4bf45ca2edccdae9054a3888c0b50deafac3 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Sat, 9 Mar 2019 07:23:11 +0000 Subject: [PATCH] Tidied up image manipulation ops --- src/core/operations/BlurImage.mjs | 69 +++++++++---------- src/core/operations/ContainImage.mjs | 17 ++--- src/core/operations/CoverImage.mjs | 17 ++--- src/core/operations/CropImage.mjs | 20 ++---- src/core/operations/DitherImage.mjs | 49 ++++++------- src/core/operations/FlipImage.mjs | 20 ++---- .../operations/ImageBrightnessContrast.mjs | 21 +++--- src/core/operations/ImageFilter.mjs | 16 ++--- .../ImageHueSaturationLightness.mjs | 17 ++--- src/core/operations/ImageOpacity.mjs | 17 ++--- src/core/operations/InvertImage.mjs | 16 ++--- src/core/operations/NormaliseImage.mjs | 33 ++------- src/core/operations/ResizeImage.mjs | 19 ++--- src/core/operations/RotateImage.mjs | 48 ++++++------- 14 files changed, 143 insertions(+), 236 deletions(-) diff --git a/src/core/operations/BlurImage.mjs b/src/core/operations/BlurImage.mjs index fba3c927..e1a52710 100644 --- a/src/core/operations/BlurImage.mjs +++ b/src/core/operations/BlurImage.mjs @@ -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 ""; + return ``; } } diff --git a/src/core/operations/ContainImage.mjs b/src/core/operations/ContainImage.mjs index a2da5363..c6df81ef 100644 --- a/src/core/operations/ContainImage.mjs +++ b/src/core/operations/ContainImage.mjs @@ -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.mjs"; import jimp from "jimp"; @@ -83,7 +83,6 @@ class ContainImage extends Operation { */ async run(input, args) { const [width, height, hAlign, vAlign, alg] = args; - const type = Magic.magicFileType(input); const resizeMap = { "Nearest Neighbour": jimp.RESIZE_NEAREST_NEIGHBOR, @@ -102,7 +101,7 @@ class ContainImage extends Operation { "Bottom": jimp.VERTICAL_ALIGN_BOTTOM }; - if (!type || type.mime.indexOf("image") !== 0){ + if (!isImage(input)) { throw new OperationError("Invalid file type."); } @@ -131,16 +130,12 @@ class ContainImage extends Operation { 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"); + const type = isImage(data); + if (!type) { + throw new OperationError("Invalid file type."); } - dataURI += "base64," + toBase64(data); - return ""; + return ``; } } diff --git a/src/core/operations/CoverImage.mjs b/src/core/operations/CoverImage.mjs index f49e08b7..07466308 100644 --- a/src/core/operations/CoverImage.mjs +++ b/src/core/operations/CoverImage.mjs @@ -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.mjs"; import jimp from "jimp"; @@ -83,7 +83,6 @@ class CoverImage extends Operation { */ async run(input, args) { const [width, height, hAlign, vAlign, alg] = args; - const type = Magic.magicFileType(input); const resizeMap = { "Nearest Neighbour": jimp.RESIZE_NEAREST_NEIGHBOR, @@ -102,7 +101,7 @@ class CoverImage extends Operation { "Bottom": jimp.VERTICAL_ALIGN_BOTTOM }; - if (!type || type.mime.indexOf("image") !== 0){ + if (!isImage(input)) { throw new OperationError("Invalid file type."); } @@ -131,16 +130,12 @@ class CoverImage extends Operation { 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"); + const type = isImage(data); + if (!type) { + throw new OperationError("Invalid file type."); } - dataURI += "base64," + toBase64(data); - return ""; + return ``; } } diff --git a/src/core/operations/CropImage.mjs b/src/core/operations/CropImage.mjs index 7f1eabdf..efbf29f9 100644 --- a/src/core/operations/CropImage.mjs +++ b/src/core/operations/CropImage.mjs @@ -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.mjs"; import jimp from "jimp"; @@ -23,7 +23,7 @@ class CropImage extends Operation { this.name = "Crop Image"; this.module = "Image"; - this.description = "Crops an image to the specified region, or automatically crop edges.

Autocrop
Automatically crops same-colour borders from the image.

Autocrop tolerance
A percentage value for the tolerance of colour difference between pixels.

Only autocrop frames
Only crop real frames (all sides must have the same border)

Symmetric autocrop
Force autocrop to be symmetric (top/bottom and left/right are cropped by the same amount)

Autocrop keep border
The number of pixels of border to leave around the image."; + this.description = "Crops an image to the specified region, or automatically crops edges.

Autocrop
Automatically crops same-colour borders from the image.

Autocrop tolerance
A percentage value for the tolerance of colour difference between pixels.

Only autocrop frames
Only crop real frames (all sides must have the same border)

Symmetric autocrop
Force autocrop to be symmetric (top/bottom and left/right are cropped by the same amount)

Autocrop keep border
The number of pixels of border to leave around the image."; this.infoURL = "https://wikipedia.org/wiki/Cropping_(image)"; this.inputType = "byteArray"; this.outputType = "byteArray"; @@ -91,10 +91,8 @@ class CropImage extends Operation { * @returns {byteArray} */ async run(input, args) { - // const [firstArg, secondArg] = args; const [xPos, yPos, width, height, autocrop, autoTolerance, autoFrames, autoSymmetric, autoBorder] = args; - const type = Magic.magicFileType(input); - if (!type || type.mime.indexOf("image") !== 0){ + if (!isImage(input)) { throw new OperationError("Invalid file type."); } @@ -133,16 +131,12 @@ class CropImage extends Operation { 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"); + const type = isImage(data); + if (!type) { + throw new OperationError("Invalid file type."); } - dataURI += "base64," + toBase64(data); - return ""; + return ``; } } diff --git a/src/core/operations/DitherImage.mjs b/src/core/operations/DitherImage.mjs index f7ef4e33..13011837 100644 --- a/src/core/operations/DitherImage.mjs +++ b/src/core/operations/DitherImage.mjs @@ -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"; @@ -37,27 +37,25 @@ class DitherImage extends Operation { * @returns {byteArray} */ async run(input, 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 { - 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 { + 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 { + 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})`); + } } /** @@ -68,17 +66,12 @@ class DitherImage extends Operation { 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 ""; + return ``; } } diff --git a/src/core/operations/FlipImage.mjs b/src/core/operations/FlipImage.mjs index 09791ca6..593809e9 100644 --- a/src/core/operations/FlipImage.mjs +++ b/src/core/operations/FlipImage.mjs @@ -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"; @@ -27,10 +27,10 @@ class FlipImage extends Operation { this.infoURL = ""; this.inputType = "byteArray"; this.outputType = "byteArray"; - this.presentType="html"; + this.presentType = "html"; this.args = [ { - name: "Flip Axis", + name: "Axis", type: "option", value: ["Horizontal", "Vertical"] } @@ -44,8 +44,7 @@ class FlipImage extends Operation { */ async run(input, args) { const [flipAxis] = args; - const type = Magic.magicFileType(input); - if (!type || type.mime.indexOf("image") !== 0){ + if (!isImage(input)) { throw new OperationError("Invalid input file type."); } @@ -82,17 +81,12 @@ class FlipImage extends Operation { 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 ""; + return ``; } } diff --git a/src/core/operations/ImageBrightnessContrast.mjs b/src/core/operations/ImageBrightnessContrast.mjs index 2f49bab7..27a30cff 100644 --- a/src/core/operations/ImageBrightnessContrast.mjs +++ b/src/core/operations/ImageBrightnessContrast.mjs @@ -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.mjs"; import jimp from "jimp"; @@ -23,7 +23,7 @@ class ImageBrightnessContrast extends Operation { this.name = "Image Brightness / Contrast"; this.module = "Image"; - this.description = "Adjust the brightness and contrast of an image."; + this.description = "Adjust the brightness or contrast of an image."; this.infoURL = ""; this.inputType = "byteArray"; this.outputType = "byteArray"; @@ -53,8 +53,7 @@ class ImageBrightnessContrast extends Operation { */ async run(input, args) { const [brightness, contrast] = args; - const type = Magic.magicFileType(input); - if (!type || type.mime.indexOf("image") !== 0){ + if (!isImage(input)) { throw new OperationError("Invalid file type."); } @@ -79,7 +78,7 @@ class ImageBrightnessContrast extends Operation { const imageBuffer = await image.getBufferAsync(jimp.AUTO); return [...imageBuffer]; } catch (err) { - throw new OperationError(`Error adjusting image brightness / contrast. (${err})`); + throw new OperationError(`Error adjusting image brightness or contrast. (${err})`); } } @@ -91,16 +90,12 @@ class ImageBrightnessContrast extends Operation { 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"); + const type = isImage(data); + if (!type) { + throw new OperationError("Invalid file type."); } - dataURI += "base64," + toBase64(data); - return ""; + return ``; } } diff --git a/src/core/operations/ImageFilter.mjs b/src/core/operations/ImageFilter.mjs index 5d7f505d..aca34042 100644 --- a/src/core/operations/ImageFilter.mjs +++ b/src/core/operations/ImageFilter.mjs @@ -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.mjs"; import jimp from "jimp"; @@ -47,8 +47,7 @@ class ImageFilter extends Operation { */ async run(input, args) { const [filterType] = args; - const type = Magic.magicFileType(input); - if (!type || type.mime.indexOf("image") !== 0){ + if (!isImage(input)){ throw new OperationError("Invalid file type."); } @@ -82,17 +81,12 @@ class ImageFilter extends Operation { 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 ""; + return ``; } } diff --git a/src/core/operations/ImageHueSaturationLightness.mjs b/src/core/operations/ImageHueSaturationLightness.mjs index 9e63a6b3..bca73c30 100644 --- a/src/core/operations/ImageHueSaturationLightness.mjs +++ b/src/core/operations/ImageHueSaturationLightness.mjs @@ -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.mjs"; import jimp from "jimp"; @@ -60,9 +60,8 @@ class ImageHueSaturationLightness extends Operation { */ async run(input, args) { const [hue, saturation, lightness] = args; - const type = Magic.magicFileType(input); - if (!type || type.mime.indexOf("image") !== 0){ + if (!isImage(input)) { throw new OperationError("Invalid file type."); } @@ -118,16 +117,12 @@ class ImageHueSaturationLightness extends Operation { 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"); + const type = isImage(data); + if (!type) { + throw new OperationError("Invalid file type."); } - dataURI += "base64," + toBase64(data); - return ""; + return ``; } } diff --git a/src/core/operations/ImageOpacity.mjs b/src/core/operations/ImageOpacity.mjs index 5a547992..999ad176 100644 --- a/src/core/operations/ImageOpacity.mjs +++ b/src/core/operations/ImageOpacity.mjs @@ -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.mjs"; import jimp from "jimp"; @@ -46,8 +46,7 @@ class ImageOpacity extends Operation { */ async run(input, args) { const [opacity] = args; - const type = Magic.magicFileType(input); - if (!type || type.mime.indexOf("image") !== 0){ + if (!isImage(input)) { throw new OperationError("Invalid file type."); } @@ -77,16 +76,12 @@ class ImageOpacity extends Operation { 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"); + const type = isImage(data); + if (!type) { + throw new OperationError("Invalid file type."); } - dataURI += "base64," + toBase64(data); - return ""; + return ``; } } diff --git a/src/core/operations/InvertImage.mjs b/src/core/operations/InvertImage.mjs index c2625d9a..ed97523f 100644 --- a/src/core/operations/InvertImage.mjs +++ b/src/core/operations/InvertImage.mjs @@ -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"; @@ -37,8 +37,7 @@ class InvertImage extends Operation { * @returns {byteArray} */ async run(input, args) { - const type = Magic.magicFileType(input); - if (!type || type.mime.indexOf("image") !== 0) { + if (!isImage(input)) { throw new OperationError("Invalid input file format."); } @@ -67,17 +66,12 @@ class InvertImage extends Operation { 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 ""; + return ``; } } diff --git a/src/core/operations/NormaliseImage.mjs b/src/core/operations/NormaliseImage.mjs index 1815c7f1..bb5113a7 100644 --- a/src/core/operations/NormaliseImage.mjs +++ b/src/core/operations/NormaliseImage.mjs @@ -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"; @@ -28,20 +28,7 @@ class NormaliseImage extends Operation { 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 - } - */ - ]; + this.args = []; } /** @@ -50,10 +37,7 @@ class NormaliseImage extends Operation { * @returns {byteArray} */ async run(input, args) { - // const [firstArg, secondArg] = args; - const type = Magic.magicFileType(input); - - if (!type || type.mime.indexOf("image") !== 0){ + if (!isImage(input)) { throw new OperationError("Invalid file type."); } @@ -73,17 +57,12 @@ class NormaliseImage extends Operation { 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 ""; + return ``; } } diff --git a/src/core/operations/ResizeImage.mjs b/src/core/operations/ResizeImage.mjs index 36b0c805..48a5d54a 100644 --- a/src/core/operations/ResizeImage.mjs +++ b/src/core/operations/ResizeImage.mjs @@ -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.mjs"; import jimp from "jimp"; @@ -76,8 +76,7 @@ class ResizeImage extends Operation { height = args[1]; const unit = args[2], aspect = args[3], - resizeAlg = args[4], - type = Magic.magicFileType(input); + resizeAlg = args[4]; const resizeMap = { "Nearest Neighbour": jimp.RESIZE_NEAREST_NEIGHBOR, @@ -87,7 +86,7 @@ class ResizeImage extends Operation { "Bezier": jimp.RESIZE_BEZIER }; - if (!type || type.mime.indexOf("image") !== 0){ + if (!isImage(input)) { throw new OperationError("Invalid file type."); } @@ -126,16 +125,12 @@ class ResizeImage extends Operation { 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"); + const type = isImage(data); + if (!type) { + throw new OperationError("Invalid file type."); } - dataURI += "base64," + toBase64(data); - return ""; + return ``; } } diff --git a/src/core/operations/RotateImage.mjs b/src/core/operations/RotateImage.mjs index b2b1e059..34497863 100644 --- a/src/core/operations/RotateImage.mjs +++ b/src/core/operations/RotateImage.mjs @@ -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"; @@ -44,27 +44,26 @@ class RotateImage extends Operation { */ async run(input, args) { const [degrees] = 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 { - 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 { + 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 { + 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})`); + } } /** @@ -75,17 +74,12 @@ class RotateImage extends Operation { 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 ""; + return ``; } }