-
-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters