diff --git a/src/core/lib/ImageManipulation.mjs b/src/core/lib/ImageManipulation.mjs index 1f1b85ae..54c1bafc 100644 --- a/src/core/lib/ImageManipulation.mjs +++ b/src/core/lib/ImageManipulation.mjs @@ -12,7 +12,7 @@ import OperationError from "../errors/OperationError"; * Gaussian blurs an image. * * @param {jimp} input - * @param {int} radius + * @param {number} radius * @param {boolean} fast * @returns {jimp} */ @@ -24,7 +24,6 @@ export function gaussianBlur (input, radius) { input = boxBlur(input, (boxes[i] - 1) / 2); } } catch (err) { - log.error(err); throw new OperationError(`Error blurring image. (${err})`); } @@ -33,8 +32,8 @@ export function gaussianBlur (input, radius) { /** * - * @param {int} radius - * @param {int} numBoxes + * @param {number} radius + * @param {number} numBoxes * @returns {Array} */ function boxesForGauss(radius, numBoxes) { @@ -167,9 +166,9 @@ function boxBlurH (source, output, width, height, radius) { * * @param {jimp} source * @param {jimp} output - * @param {int} width - * @param {int} height - * @param {int} radius + * @param {number} width + * @param {number} height + * @param {number} radius * @returns {jimp} */ function boxBlurV (source, output, width, height, radius) { diff --git a/src/core/operations/AddTextToImage.mjs b/src/core/operations/AddTextToImage.mjs index f8ee3485..68a28c8f 100644 --- a/src/core/operations/AddTextToImage.mjs +++ b/src/core/operations/AddTextToImage.mjs @@ -228,7 +228,12 @@ class AddTextToImage extends Operation { } } - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error adding text to image. (${err})`); diff --git a/src/core/operations/BlurImage.mjs b/src/core/operations/BlurImage.mjs index e0b5c919..68c8dbd6 100644 --- a/src/core/operations/BlurImage.mjs +++ b/src/core/operations/BlurImage.mjs @@ -76,7 +76,12 @@ class BlurImage extends Operation { break; } - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error blurring image. (${err})`); diff --git a/src/core/operations/ContainImage.mjs b/src/core/operations/ContainImage.mjs index 4cb7cfdf..e16d3d58 100644 --- a/src/core/operations/ContainImage.mjs +++ b/src/core/operations/ContainImage.mjs @@ -127,7 +127,12 @@ class ContainImage extends Operation { image = newImage; } - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error containing image. (${err})`); diff --git a/src/core/operations/CoverImage.mjs b/src/core/operations/CoverImage.mjs index 07466308..492938ff 100644 --- a/src/core/operations/CoverImage.mjs +++ b/src/core/operations/CoverImage.mjs @@ -115,7 +115,12 @@ class CoverImage extends Operation { 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); + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error covering image. (${err})`); diff --git a/src/core/operations/CropImage.mjs b/src/core/operations/CropImage.mjs index efbf29f9..fd04a907 100644 --- a/src/core/operations/CropImage.mjs +++ b/src/core/operations/CropImage.mjs @@ -116,7 +116,12 @@ class CropImage extends Operation { image.crop(xPos, yPos, width, height); } - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error cropping image. (${err})`); diff --git a/src/core/operations/DitherImage.mjs b/src/core/operations/DitherImage.mjs index 13011837..3e603016 100644 --- a/src/core/operations/DitherImage.mjs +++ b/src/core/operations/DitherImage.mjs @@ -51,7 +51,13 @@ class DitherImage extends Operation { if (ENVIRONMENT_IS_WORKER()) self.sendStatusMessage("Applying dither to image..."); image.dither565(); - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error applying dither to image. (${err})`); diff --git a/src/core/operations/FlipImage.mjs b/src/core/operations/FlipImage.mjs index 593809e9..cebba0e0 100644 --- a/src/core/operations/FlipImage.mjs +++ b/src/core/operations/FlipImage.mjs @@ -66,7 +66,12 @@ class FlipImage extends Operation { break; } - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error flipping image. (${err})`); diff --git a/src/core/operations/ImageBrightnessContrast.mjs b/src/core/operations/ImageBrightnessContrast.mjs index 27a30cff..f47134a2 100644 --- a/src/core/operations/ImageBrightnessContrast.mjs +++ b/src/core/operations/ImageBrightnessContrast.mjs @@ -75,7 +75,12 @@ class ImageBrightnessContrast extends Operation { image.contrast(contrast / 100); } - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error adjusting image brightness or contrast. (${err})`); diff --git a/src/core/operations/ImageFilter.mjs b/src/core/operations/ImageFilter.mjs index aca34042..6c1a5a65 100644 --- a/src/core/operations/ImageFilter.mjs +++ b/src/core/operations/ImageFilter.mjs @@ -66,7 +66,12 @@ class ImageFilter extends Operation { image.sepia(); } - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error applying filter to image. (${err})`); diff --git a/src/core/operations/ImageHueSaturationLightness.mjs b/src/core/operations/ImageHueSaturationLightness.mjs index bca73c30..4bdd9fa7 100644 --- a/src/core/operations/ImageHueSaturationLightness.mjs +++ b/src/core/operations/ImageHueSaturationLightness.mjs @@ -102,7 +102,13 @@ class ImageHueSaturationLightness extends Operation { } ]); } - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error adjusting image hue / saturation / lightness. (${err})`); diff --git a/src/core/operations/ImageOpacity.mjs b/src/core/operations/ImageOpacity.mjs index 999ad176..afa370ff 100644 --- a/src/core/operations/ImageOpacity.mjs +++ b/src/core/operations/ImageOpacity.mjs @@ -61,7 +61,12 @@ class ImageOpacity extends Operation { self.sendStatusMessage("Changing image opacity..."); image.opacity(opacity / 100); - const imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error changing image opacity. (${err})`); diff --git a/src/core/operations/InvertImage.mjs b/src/core/operations/InvertImage.mjs index ed97523f..7c7a4d09 100644 --- a/src/core/operations/InvertImage.mjs +++ b/src/core/operations/InvertImage.mjs @@ -51,7 +51,13 @@ class InvertImage extends Operation { if (ENVIRONMENT_IS_WORKER()) self.sendStatusMessage("Inverting image..."); image.invert(); - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error inverting image. (${err})`); diff --git a/src/core/operations/NormaliseImage.mjs b/src/core/operations/NormaliseImage.mjs index bb5113a7..c49cc977 100644 --- a/src/core/operations/NormaliseImage.mjs +++ b/src/core/operations/NormaliseImage.mjs @@ -41,12 +41,26 @@ class NormaliseImage extends Operation { throw new OperationError("Invalid file type."); } - const image = await jimp.read(Buffer.from(input)); + let image; + try { + image = await jimp.read(Buffer.from(input)); + } catch (err) { + throw new OperationError(`Error opening image file. (${err})`); + } - image.normalize(); + try { + image.normalize(); - const imageBuffer = await image.getBufferAsync(jimp.AUTO); - return [...imageBuffer]; + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } + return [...imageBuffer]; + } catch (err) { + throw new OperationError(`Error normalising image. (${err})`); + } } /** diff --git a/src/core/operations/ResizeImage.mjs b/src/core/operations/ResizeImage.mjs index 48a5d54a..11266ba8 100644 --- a/src/core/operations/ResizeImage.mjs +++ b/src/core/operations/ResizeImage.mjs @@ -110,7 +110,12 @@ class ResizeImage extends Operation { image.resize(width, height, resizeMap[resizeAlg]); } - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error resizing image. (${err})`); diff --git a/src/core/operations/RotateImage.mjs b/src/core/operations/RotateImage.mjs index 34497863..16330c44 100644 --- a/src/core/operations/RotateImage.mjs +++ b/src/core/operations/RotateImage.mjs @@ -59,7 +59,13 @@ class RotateImage extends Operation { if (ENVIRONMENT_IS_WORKER()) self.sendStatusMessage("Rotating image..."); image.rotate(degrees); - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error rotating image. (${err})`); diff --git a/src/core/operations/SharpenImage.mjs b/src/core/operations/SharpenImage.mjs index 3ef1912e..de2f3603 100644 --- a/src/core/operations/SharpenImage.mjs +++ b/src/core/operations/SharpenImage.mjs @@ -134,7 +134,12 @@ class SharpenImage extends Operation { } }); - const imageBuffer = await image.getBufferAsync(jimp.AUTO); + let imageBuffer; + if (image.getMIME() === "image/gif") { + imageBuffer = await image.getBufferAsync(jimp.MIME_PNG); + } else { + imageBuffer = await image.getBufferAsync(jimp.AUTO); + } return [...imageBuffer]; } catch (err) { throw new OperationError(`Error sharpening image. (${err})`);