From 06095ca0a7440987cca98afe0c940cc935ccf366 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Thu, 19 Nov 2020 10:48:46 +0100 Subject: [PATCH] :zap: Make it possible to create a new image --- packages/nodes-base/nodes/EditImage.node.ts | 108 +++++++++++++++++--- 1 file changed, 93 insertions(+), 15 deletions(-) diff --git a/packages/nodes-base/nodes/EditImage.node.ts b/packages/nodes-base/nodes/EditImage.node.ts index 382fa4097a..f11aee8737 100644 --- a/packages/nodes-base/nodes/EditImage.node.ts +++ b/packages/nodes-base/nodes/EditImage.node.ts @@ -53,6 +53,11 @@ export class EditImage implements INodeType { value: 'border', description: 'Adds a border to the image', }, + { + name: 'Create', + value: 'create', + description: 'Create a new image', + }, { name: 'Crop', value: 'crop', @@ -107,6 +112,62 @@ export class EditImage implements INodeType { }, + // ---------------------------------- + // create + // ---------------------------------- + { + displayName: 'Background Color', + name: 'backgroundColor', + type: 'color', + default: '#ffffff00', + typeOptions: { + showAlpha: true, + }, + displayOptions: { + show: { + operation: [ + 'create', + ], + }, + }, + description: 'The background color of the image to create.', + }, + { + displayName: 'Image Width', + name: 'width', + type: 'number', + default: 50, + typeOptions: { + minValue: 1, + }, + displayOptions: { + show: { + operation: [ + 'create', + ], + }, + }, + description: 'The width of the image to create.', + }, + { + displayName: 'Image Height', + name: 'height', + type: 'number', + default: 50, + typeOptions: { + minValue: 1, + }, + displayOptions: { + show: { + operation: [ + 'create', + ], + }, + }, + description: 'The height of the image to create.', + }, + + // ---------------------------------- // draw // ---------------------------------- @@ -821,19 +882,29 @@ export class EditImage implements INodeType { const options = this.getNodeParameter('options', {}) as IDataObject; - // TODO: Later should make so that it sends directly a valid buffer and the buffer.from stuff is not needed anymore - if (item.binary === undefined) { - return item; + let gmInstance: gm.State; + if (operation === 'create') { + const backgroundColor = this.getNodeParameter('backgroundColor') as string; + const width = this.getNodeParameter('width') as number; + const height = this.getNodeParameter('height') as number; + + gmInstance = gm(width, height, backgroundColor); + if (!options.format) { + options.format = 'png'; + } + } else { + if (item.binary === undefined) { + throw new Error('Item does not contain any binary data.'); + } + + if (item.binary[dataPropertyName as string] === undefined) { + throw new Error(`Item does not contain any binary data with the name "${dataPropertyName}".`); + } + + gmInstance = gm(Buffer.from(item.binary![dataPropertyName as string].data, BINARY_ENCODING)); + gmInstance = gmInstance.background('transparent'); } - if (item.binary[dataPropertyName as string] === undefined) { - return item; - } - - let gmInstance = gm(Buffer.from(item.binary![dataPropertyName as string].data, BINARY_ENCODING)); - - gmInstance = gmInstance.background('transparent'); - const cleanupFunctions: Array<() => void> = []; if (operation === 'blur') { @@ -853,15 +924,15 @@ export class EditImage implements INodeType { const geometryString = (positionX >= 0 ? '+' : '') + positionX + (positionY >= 0 ? '+' : '') + positionY; - if (item.binary[dataPropertyNameComposite as string] === undefined) { - throw new Error(''); + if (item.binary![dataPropertyNameComposite as string] === undefined) { + throw new Error(`Item does not contain any binary data with the name "${dataPropertyNameComposite}".`); } const { fd, path, cleanup } = await file(); cleanupFunctions.push(cleanup); fsWriteFileAsync(fd, Buffer.from(item.binary![dataPropertyNameComposite as string].data, BINARY_ENCODING)); - gmInstance = gmInstance.compose(path).geometry(geometryString); + gmInstance = gmInstance.composite(path).geometry(geometryString); } else if (operation === 'crop') { const width = this.getNodeParameter('width') as number; const height = this.getNodeParameter('height') as number; @@ -963,7 +1034,7 @@ export class EditImage implements INodeType { .fill(fontColor) .fontSize(fontSize) .drawText(positionX, positionY, renderText); - } else { + } else if (operation !== 'create') { throw new Error(`The operation "${operation}" is not supported!`); } @@ -979,6 +1050,13 @@ export class EditImage implements INodeType { Object.assign(newItem.binary, item.binary); // Make a deep copy of the binary data we change newItem.binary![dataPropertyName as string] = JSON.parse(JSON.stringify(newItem.binary![dataPropertyName as string])); + } else { + newItem.binary = { + [dataPropertyName as string]: { + data: '', + mimeType: '', + }, + }; } if (options.quality !== undefined) {