Fix operations
This commit is contained in:
parent
b2a0e7960f
commit
fd0d40c18e
@ -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") {
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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") {
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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],
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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]);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user