Add shear option, resize with alpha color and fix bug

This commit is contained in:
Jan Oberhauser
2020-11-09 11:26:46 +01:00
parent 2cc6f6c218
commit 9fee42b2a8
3 changed files with 111 additions and 5 deletions

View File

@@ -9,6 +9,12 @@ import {
INodeTypeDescription,
} from 'n8n-workflow';
import * as gm from 'gm';
import { file } from 'tmp-promise';
import {
writeFile as fsWriteFile,
} from 'fs';
import { promisify } from 'util';
const fsWriteFileAsync = promisify(fsWriteFile);
export class EditImage implements INodeType {
@@ -61,6 +67,11 @@ export class EditImage implements INodeType {
value: 'resize',
description: 'Change the size of image',
},
{
name: 'Shear',
value: 'shear',
description: 'Shear image along the X or Y axis',
},
{
name: 'Text',
value: 'text',
@@ -385,6 +396,11 @@ export class EditImage implements INodeType {
value: 'onlyIfSmaller',
description: 'Resize only if image is smaller than width or height',
},
{
name: 'Percent',
value: 'percent',
description: 'Width and height are specified in percents.',
},
],
default: 'maximumArea',
displayOptions: {
@@ -422,7 +438,10 @@ export class EditImage implements INodeType {
displayName: 'Background Color',
name: 'backgroundColor',
type: 'color',
default: '#ffffff',
default: '#ffffffff',
typeOptions: {
showAlpha: true,
},
displayOptions: {
show: {
operation: [
@@ -433,6 +452,39 @@ export class EditImage implements INodeType {
description: 'The color to use for the background when image gets rotated by anything which is not a multiple of 90..',
},
// ----------------------------------
// shear
// ----------------------------------
{
displayName: 'Degrees X',
name: 'degreesX',
type: 'number',
default: 0,
displayOptions: {
show: {
operation: [
'shear',
],
},
},
description: 'X (horizontal) shear degrees.',
},
{
displayName: 'Degrees Y',
name: 'degreesY',
type: 'number',
default: 0,
displayOptions: {
show: {
operation: [
'shear',
],
},
},
description: 'Y (vertical) shear degrees.',
},
{
displayName: 'Options',
name: 'options',
@@ -503,7 +555,6 @@ export class EditImage implements INodeType {
},
description: 'Sets the jpeg|png|tiff compression level from 0 to 100 (best).',
},
],
},
],
@@ -529,6 +580,8 @@ export class EditImage implements INodeType {
let gmInstance = gm(Buffer.from(item.binary![dataPropertyName as string].data, BINARY_ENCODING));
gmInstance = gmInstance.background('transparent');
if (operation === 'blur') {
const blur = this.getNodeParameter('blur') as number;
const sigma = this.getNodeParameter('sigma') as number;
@@ -574,6 +627,8 @@ export class EditImage implements INodeType {
option = '<';
} else if (resizeOption === 'onlyIfLarger') {
option = '>';
} else if (resizeOption === 'percent') {
option = '%';
}
gmInstance = gmInstance.resize(width, height, option);
@@ -581,6 +636,10 @@ export class EditImage implements INodeType {
const rotate = this.getNodeParameter('rotate') as number;
const backgroundColor = this.getNodeParameter('backgroundColor') as string;
gmInstance = gmInstance.rotate(backgroundColor, rotate);
} else if (operation === 'shear') {
const xDegrees = this.getNodeParameter('degreesX') as number;
const yDegress = this.getNodeParameter('degreesY') as number;
gmInstance = gmInstance.shear(xDegrees, yDegress);
} else if (operation === 'text') {
const fontColor = this.getNodeParameter('fontColor') as string;
const fontSize = this.getNodeParameter('fontSize') as number;
@@ -624,6 +683,8 @@ export class EditImage implements INodeType {
// data references which do not get changed still stay behind
// but the incoming data does not get changed.
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]));
}
if (options.quality !== undefined) {