Skip to content

Commit

Permalink
added image action
Browse files Browse the repository at this point in the history
  • Loading branch information
inlife committed Jun 25, 2024
1 parent bb64a9c commit 7ec9357
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/nexrender-action-image/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const jimp = require('jimp');

module.exports = async (job, settings, { create, input, output, filters }) => {
if (!filters || !filters.length) {
throw new Error('No filters provided');
}

let image;

if (!input && !create) {
throw new Error('No input or create provided');
}

if (!output) {
throw new Error('No output provided');
}

if (!input && create) {
const [width, height] = create;
image = new jimp(width, height);
} else {
image = await jimp.read(input);
}

for (const filter of filters) {
const { name, args } = filter;

if (!image[name]) {
throw new Error(`Filter "${name}" does not exist`);
}

image[name](...args);
}

await image.writeAsync(output);
}
13 changes: 13 additions & 0 deletions packages/nexrender-action-image/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@nexrender/action-image",
"homepage": "https://www.nexrender.com",
"author": "inlife",
"version": "1.49.4",
"main": "index.js",
"publishConfig": {
"access": "public"
},
"dependencies": {
"jimp": "^0.22.12"
}
}
91 changes: 91 additions & 0 deletions packages/nexrender-action-image/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const action = require("./index.js");
const assert = require("assert");
const fs = require("fs");
const path = require("path");

describe("action/image", () => {
it("should throw an error if no filters are provided", async () => {
const job = {};
const settings = {};
const params = {
input: 'foobar',
output: 'foobar',
};

try {
await action(job, settings, params);
} catch (err) {
assert.equal(err.message, "No filters provided");
}
});

it("should throw an error if no input or create is provided", async () => {
const job = {};
const settings = {};
const { create, input, output, filters } = {filters: [{}]};

try {
await action(job, settings, { create, input, output, filters });
} catch (err) {
assert.equal(err.message, "No input or create provided");
}
});

it("should throw an error if no output is provided", async () => {
const job = {};
const settings = {};
const { create, input, output, filters } = {input: 'foobar', filters: [{}]};

try {
await action(job, settings, { create, input, output, filters });
} catch (err) {
assert.equal(err.message, "No output provided");
}
});

const imagePath = path.join(__dirname, "test.png");

it("should create a new image with the provided dimensions", async () => {
const job = {};
const settings = {};
const create = [21, 21];
const output = imagePath;
const filters = [
{ name: "background", args: [0xFFFFFF] },
{ name: "setPixelColor", args: [0xFF0000FF, 10, 10] },
{ name: "setPixelColor", args: [0xFF0000FF, 9, 10] },
{ name: "setPixelColor", args: [0xFF0000FF, 11, 10] },
{ name: "setPixelColor", args: [0xFF0000FF, 10, 9] },
{ name: "setPixelColor", args: [0xFF0000FF, 10, 10] },
{ name: "setPixelColor", args: [0xFF0000FF, 10, 11] },
];

await action(job, settings, { create, output, filters });

// assert that the image was created
assert.ok(fs.existsSync(output));
});

it("should apply filters to the input image", async () => {
const job = {};
const settings = {};

const input = imagePath;
const output = imagePath + ".jpg";

const filters = [
{ name: "greyscale", args: [] },
{ name: "invert", args: [] },
];

await action(job, settings, { input, output, filters });

// assert that the image was created
assert.ok(fs.existsSync(output));
});

after(() => {
fs.unlinkSync(imagePath);
fs.unlinkSync(imagePath + ".jpg");
});
})
1 change: 1 addition & 0 deletions packages/nexrender-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@nexrender/action-decompress": "^1.48.2",
"@nexrender/action-encode": "^1.1.4",
"@nexrender/action-upload": "^1.0.0",
"@nexrender/action-image": "^1.49.4",
"@nexrender/provider-ftp": "^1.17.2",
"@nexrender/provider-gs": "^1.21.3",
"@nexrender/provider-nx": "^1.47.0",
Expand Down
1 change: 1 addition & 0 deletions packages/nexrender-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ if (process.env.NEXRENDER_REQUIRE_PLUGINS) {
require('@nexrender/action-encode');
require('@nexrender/action-upload');
require('@nexrender/action-decompress');
require('@nexrender/action-image');

require('@nexrender/provider-s3');
require('@nexrender/provider-ftp');
Expand Down

0 comments on commit 7ec9357

Please sign in to comment.