From fd0d40c18ecde6bafc1636159fba93db9b5fc705 Mon Sep 17 00:00:00 2001 From: C85297 <95289555+C85297@users.noreply.github.com> Date: Thu, 5 Feb 2026 12:23:11 +0000 Subject: [PATCH] Fix operations --- src/core/operations/AddTextToImage.mjs | 26 ++++++++++++++++--- src/core/operations/ContainImage.mjs | 14 ++++++---- src/core/operations/CoverImage.mjs | 8 +++--- src/core/operations/CropImage.mjs | 2 +- src/core/operations/DitherImage.mjs | 2 +- src/core/operations/FlipImage.mjs | 10 +++++-- src/core/operations/GenerateImage.mjs | 10 +++---- .../ImageHueSaturationLightness.mjs | 6 ++--- src/core/operations/ResizeImage.mjs | 12 +++++++-- tests/browser/02_ops.js | 4 +++ 10 files changed, 68 insertions(+), 26 deletions(-) diff --git a/src/core/operations/AddTextToImage.mjs b/src/core/operations/AddTextToImage.mjs index aa3c8928..722a67fa 100644 --- a/src/core/operations/AddTextToImage.mjs +++ b/src/core/operations/AddTextToImage.mjs @@ -17,6 +17,7 @@ import { measureTextHeight, loadFont, } from "jimp"; +import log from "loglevel"; /** * Add Text To Image operation @@ -211,6 +212,10 @@ class AddTextToImage extends Operation { } }); + let outlog = ""; + outlog += `Adding text: "${text}" at (${xPos}, ${yPos}) with font: ${fontFace}, size: ${size}, colour: rgba(${red}, ${green}, ${blue}, ${alpha})\n`; + outlog += `Adding to image ${image.width}w, ${image.height}h\n`; + // Create a temporary image to hold the rendered text const textImage = new Jimp({ width: measureText(jimpFont, text), @@ -222,17 +227,25 @@ class AddTextToImage extends Operation { y: 0, text, }); + outlog += `Rendered text image size: ${textImage.width}w, ${textImage.height}h\n`; // Scale the rendered text image to the correct size const scaleFactor = size / 72; if (size !== 1) { // Use bicubic for decreasing size if (size > 1) { - textImage.scale(scaleFactor, ResizeStrategy.BICUBIC); + textImage.scale({ + f: scaleFactor, + mode: ResizeStrategy.BICUBIC, + }); } else { - textImage.scale(scaleFactor, ResizeStrategy.BILINEAR); + textImage.scale({ + f: scaleFactor, + mode: ResizeStrategy.BILINEAR, + }); } } + outlog += `Scaled text image size: ${textImage.width}w, ${textImage.height}h\n`; // If using the alignment options, calculate the pixel values AFTER the image has been scaled switch (hAlign) { @@ -246,6 +259,7 @@ class AddTextToImage extends Operation { xPos = image.width - textImage.width; break; } + outlog += `Calculated xPos: ${xPos}\n`; switch (vAlign) { case "Top": @@ -258,9 +272,15 @@ class AddTextToImage extends Operation { yPos = image.height - textImage.height; break; } + outlog += `Calculated yPos: ${yPos}\n`; + // throw new OperationError(outlog); // Blit the rendered text image onto the original source image - image.blit(textImage, xPos, yPos); + image.blit({ + src: textImage, + x: xPos, + y: yPos, + }); let imageBuffer; if (image.mime === "image/gif") { diff --git a/src/core/operations/ContainImage.mjs b/src/core/operations/ContainImage.mjs index 923a7880..e59ecd11 100644 --- a/src/core/operations/ContainImage.mjs +++ b/src/core/operations/ContainImage.mjs @@ -118,16 +118,20 @@ class ContainImage extends Operation { try { if (isWorkerEnvironment()) self.sendStatusMessage("Containing image..."); - image.contain( + image.contain({ width, height, - alignMap[hAlign] | alignMap[vAlign], - resizeMap[alg], - ); + align: alignMap[hAlign] | alignMap[vAlign], + mode: resizeMap[alg], + }); if (opaqueBg) { const newImage = await Jimp.read(width, height, 0x000000ff); - newImage.blit(image, 0, 0); + newImage.blit({ + image, + x: 0, + y: 0, + }); image = newImage; } diff --git a/src/core/operations/CoverImage.mjs b/src/core/operations/CoverImage.mjs index 9f7bd12e..5587bd41 100644 --- a/src/core/operations/CoverImage.mjs +++ b/src/core/operations/CoverImage.mjs @@ -113,12 +113,12 @@ class CoverImage extends Operation { try { if (isWorkerEnvironment()) self.sendStatusMessage("Covering image..."); - image.cover( + image.cover({ width, height, - alignMap[hAlign] | alignMap[vAlign], - resizeMap[alg], - ); + align: alignMap[hAlign] | alignMap[vAlign], + mode: resizeMap[alg], + }); let imageBuffer; if (image.mime === "image/gif") { imageBuffer = await image.getBuffer(JimpMime.png); diff --git a/src/core/operations/CropImage.mjs b/src/core/operations/CropImage.mjs index 35e22676..922dbb65 100644 --- a/src/core/operations/CropImage.mjs +++ b/src/core/operations/CropImage.mjs @@ -124,7 +124,7 @@ class CropImage extends Operation { leaveBorder: autoBorder, }); } else { - image.crop(xPos, yPos, width, height); + image.crop({ xPos, yPos, width, height }); } let imageBuffer; diff --git a/src/core/operations/DitherImage.mjs b/src/core/operations/DitherImage.mjs index 0e647edd..f21c1f88 100644 --- a/src/core/operations/DitherImage.mjs +++ b/src/core/operations/DitherImage.mjs @@ -50,7 +50,7 @@ class DitherImage extends Operation { try { if (isWorkerEnvironment()) self.sendStatusMessage("Applying dither to image..."); - image.dither565(); + image.dither(); let imageBuffer; if (image.mime === "image/gif") { diff --git a/src/core/operations/FlipImage.mjs b/src/core/operations/FlipImage.mjs index 794c4e5a..cf9c747f 100644 --- a/src/core/operations/FlipImage.mjs +++ b/src/core/operations/FlipImage.mjs @@ -59,10 +59,16 @@ class FlipImage extends Operation { self.sendStatusMessage("Flipping image..."); switch (flipAxis) { case "Horizontal": - image.flip(true, false); + image.flip({ + horizontal: true, + vertical: false, + }); break; case "Vertical": - image.flip(false, true); + image.flip({ + horizontal: false, + vertical: true, + }); break; } diff --git a/src/core/operations/GenerateImage.mjs b/src/core/operations/GenerateImage.mjs index 1e567bf5..053e4ba1 100644 --- a/src/core/operations/GenerateImage.mjs +++ b/src/core/operations/GenerateImage.mjs @@ -155,11 +155,11 @@ class GenerateImage extends Operation { if (isWorkerEnvironment()) self.sendStatusMessage("Scaling image..."); - image.scaleToFit( - width * scale, - height * scale, - ResizeStrategy.NEAREST_NEIGHBOR, - ); + image.scaleToFit({ + w: width * scale, + h: height * scale, + mode: ResizeStrategy.NEAREST_NEIGHBOR, + }); } try { diff --git a/src/core/operations/ImageHueSaturationLightness.mjs b/src/core/operations/ImageHueSaturationLightness.mjs index 141be6af..e0a1910a 100644 --- a/src/core/operations/ImageHueSaturationLightness.mjs +++ b/src/core/operations/ImageHueSaturationLightness.mjs @@ -76,7 +76,7 @@ class ImageHueSaturationLightness extends Operation { if (hue !== 0) { if (isWorkerEnvironment()) self.sendStatusMessage("Changing image hue..."); - image.colour([ + image.color([ { apply: "hue", params: [hue], @@ -86,7 +86,7 @@ class ImageHueSaturationLightness extends Operation { if (saturation !== 0) { if (isWorkerEnvironment()) self.sendStatusMessage("Changing image saturation..."); - image.colour([ + image.color([ { apply: "saturate", params: [saturation], @@ -96,7 +96,7 @@ class ImageHueSaturationLightness extends Operation { if (lightness !== 0) { if (isWorkerEnvironment()) self.sendStatusMessage("Changing image lightness..."); - image.colour([ + image.color([ { apply: "lighten", params: [lightness], diff --git a/src/core/operations/ResizeImage.mjs b/src/core/operations/ResizeImage.mjs index 5e8ba2c0..bec07c4e 100644 --- a/src/core/operations/ResizeImage.mjs +++ b/src/core/operations/ResizeImage.mjs @@ -106,9 +106,17 @@ class ResizeImage extends Operation { if (isWorkerEnvironment()) self.sendStatusMessage("Resizing image..."); if (aspect) { - image.scaleToFit(width, height, resizeMap[resizeAlg]); + image.scaleToFit({ + w: width, + h: height, + mode: resizeMap[resizeAlg], + }); } else { - image.resize(width, height, resizeMap[resizeAlg]); + image.resize({ + w: width, + h: height, + mode: resizeMap[resizeAlg], + }); } let imageBuffer; diff --git a/tests/browser/02_ops.js b/tests/browser/02_ops.js index 7fe7f538..b183f109 100644 --- a/tests/browser/02_ops.js +++ b/tests/browser/02_ops.js @@ -35,6 +35,10 @@ module.exports = { testOp(browser, "AND", "test input", "4$04 $044", [{ "option": "Hex", "string": "34" }]); testOp(browser, "Add line numbers", "test input", "1 test input"); testOp(browser, ["From Hex", "Add Text To Image", "To Base64"], Images.PNG_HEX, Images.PNG_CHEF_B64, [[], ["Chef", "Center", "Middle", 0, 0, 16], []]); + testOp(browser, ["From Hex", "Dither Image", "SHA2"], Images.PNG_HEX, "cbf587a78915cfb14546ba83080b13e5054800802488dd0cb786b8951e7dc0b48f055260917bd0ccfc075e422b9d6aff112948562653995d74e70f0b66367ac3", [[], [], []]); + testOp(browser, ["From Hex", "Generate Image", "SHA2"], Images.PNG_HEX, "2c451762a6c9192fd31dc80765eab3f447be70ea51f6fdb6911ade4d89d4a98bd0a1ff00b08d76aac472faeceb54b66092e3f3be7bbf899bf3e55ca9c96a56aa", [[], [], []]); + testOp(browser, ["From Hex", "Image Hue/Saturation/Lightness", "SHA2"], Images.PNG_HEX, "522dfc0bbef00e05c5d6861a002039fa2952e4bbb7fe8d21d0d538ef6f9d65da82065929b4150dc5b8b49460ee6c9bef7f660b86f8d4e7442a07c61c0a152a4b", [[], [], []]); + testOp(browser, ["From Hex", "Resize Image", "SHA2"], Images.PNG_HEX, "654bfbf0a0537c901459c4bc22c5fb0bacbf01af775a0733e3a1c46cda5b699bcc4ed85322d813c7bb9b245d62d64425c0766fe03d3d20bc63634e2a4df17626", [[], [64, 64], []]); testOp(browser, "Adler-32 Checksum", "test input", "16160411"); testOp(browser, "Affine Cipher Decode", "test input", "rcqr glnsr", [1, 2]); testOp(browser, "Affine Cipher Encode", "test input", "gndg zoujg", [3, 1]);