Make it possible to create a new image

This commit is contained in:
Jan Oberhauser 2020-11-19 10:48:46 +01:00
parent 130f944398
commit 06095ca0a7

View file

@ -53,6 +53,11 @@ export class EditImage implements INodeType {
value: 'border', value: 'border',
description: 'Adds a border to the image', description: 'Adds a border to the image',
}, },
{
name: 'Create',
value: 'create',
description: 'Create a new image',
},
{ {
name: 'Crop', name: 'Crop',
value: '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 // draw
// ---------------------------------- // ----------------------------------
@ -821,19 +882,29 @@ export class EditImage implements INodeType {
const options = this.getNodeParameter('options', {}) as IDataObject; 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 let gmInstance: gm.State;
if (item.binary === undefined) { if (operation === 'create') {
return item; 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> = []; const cleanupFunctions: Array<() => void> = [];
if (operation === 'blur') { if (operation === 'blur') {
@ -853,15 +924,15 @@ export class EditImage implements INodeType {
const geometryString = (positionX >= 0 ? '+' : '') + positionX + (positionY >= 0 ? '+' : '') + positionY; const geometryString = (positionX >= 0 ? '+' : '') + positionX + (positionY >= 0 ? '+' : '') + positionY;
if (item.binary[dataPropertyNameComposite as string] === undefined) { if (item.binary![dataPropertyNameComposite as string] === undefined) {
throw new Error(''); throw new Error(`Item does not contain any binary data with the name "${dataPropertyNameComposite}".`);
} }
const { fd, path, cleanup } = await file(); const { fd, path, cleanup } = await file();
cleanupFunctions.push(cleanup); cleanupFunctions.push(cleanup);
fsWriteFileAsync(fd, Buffer.from(item.binary![dataPropertyNameComposite as string].data, BINARY_ENCODING)); 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') { } else if (operation === 'crop') {
const width = this.getNodeParameter('width') as number; const width = this.getNodeParameter('width') as number;
const height = this.getNodeParameter('height') as number; const height = this.getNodeParameter('height') as number;
@ -963,7 +1034,7 @@ export class EditImage implements INodeType {
.fill(fontColor) .fill(fontColor)
.fontSize(fontSize) .fontSize(fontSize)
.drawText(positionX, positionY, renderText); .drawText(positionX, positionY, renderText);
} else { } else if (operation !== 'create') {
throw new Error(`The operation "${operation}" is not supported!`); throw new Error(`The operation "${operation}" is not supported!`);
} }
@ -979,6 +1050,13 @@ export class EditImage implements INodeType {
Object.assign(newItem.binary, item.binary); Object.assign(newItem.binary, item.binary);
// Make a deep copy of the binary data we change // Make a deep copy of the binary data we change
newItem.binary![dataPropertyName as string] = JSON.parse(JSON.stringify(newItem.binary![dataPropertyName as string])); 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) { if (options.quality !== undefined) {