n8n-nodes-convert-image
Version:
n8n node for converting image formats between JPG, PNG, BMP, TIFF, etc. Supports both base64 and file inputs/outputs
196 lines • 7.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ImageFormatConverter = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const jimp_1 = require("jimp");
class ImageFormatConverter {
constructor() {
this.description = {
displayName: 'Image Format Converter',
name: 'imageFormatConverter',
icon: 'file:convert.svg',
group: ['transform'],
version: 1,
description: 'Image Format Converter',
defaults: {
name: 'Image Format Converter',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'Input File Type',
name: 'inputType',
type: 'options',
options: [
{
name: 'Base64',
value: 'base64',
},
{
name: 'File',
value: 'file',
},
],
default: 'file',
description: 'Whether the input is a base64 string or a file',
},
{
displayName: 'Base64 Field',
name: 'base64Field',
type: 'string',
default: 'data',
description: 'The base64 string of the image',
displayOptions: {
show: {
inputType: ['base64'],
},
},
},
{
displayName: 'Output File Format',
name: 'outputFileFormat',
type: 'options',
options: [
{
name: 'BMP',
value: 'image/bmp',
},
{
name: 'GIF',
value: 'image/gif',
},
{
name: 'JPG',
value: 'image/jpeg',
},
{
name: 'MS BMP',
value: 'image/x-ms-bmp',
},
{
name: 'PNG',
value: 'image/png',
},
{
name: 'TIFF',
value: 'image/tiff',
},
],
default: 'image/jpeg',
description: 'Whether to output as base64 string or file',
},
{
displayName: 'Output Type',
name: 'outputType',
type: 'options',
options: [
{
name: 'Base64',
value: 'base64',
},
{
name: 'File',
value: 'file',
},
],
default: 'file',
description: 'Whether to output as base64 string or file',
},
{
displayName: 'Quality',
name: 'quality',
type: 'number',
default: 80,
description: 'JPEG quality (0-100)',
typeOptions: {
minValue: 0,
maxValue: 100,
},
},
],
};
}
async execute() {
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
const inputType = this.getNodeParameter('inputType', i);
let base64Data = '';
let originalFilename = '';
if (inputType === 'base64') {
const base64Field = this.getNodeParameter('base64Field', i);
base64Data = items[i].json[base64Field];
}
else {
const item = items[i].binary;
if (item === undefined) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'No binary data exists on item!' });
}
else if (item.data === undefined) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'No binary data exists on item!' });
}
else if (item.data.data === undefined) {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'No binary data exists on item!' });
}
if (item.data.fileType !== 'image') {
throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'No image data exists on item!' });
}
originalFilename = item.data.fileName || '';
base64Data = item.data.data;
}
try {
const image = await jimp_1.Jimp.read(Buffer.from(base64Data, 'base64'));
const outputFileFormat = this.getNodeParameter('outputFileFormat', i);
const outputType = this.getNodeParameter('outputType', i);
let imageBuffer;
if (outputFileFormat === 'image/jpeg') {
const quality = this.getNodeParameter('quality', i);
imageBuffer = await image.getBuffer(outputFileFormat, { quality: quality });
}
else {
imageBuffer = await image.getBuffer(outputFileFormat);
}
const jpgBase64 = imageBuffer.toString('base64');
const newItem = {
json: {},
binary: {}
};
if (outputType === 'base64') {
newItem.json['data'] = jpgBase64;
}
else {
let extension = outputFileFormat.split('/')[1];
if (outputFileFormat === 'image/x-ms-bmp') {
extension = 'bmp';
}
let fileName = originalFilename.split('.').slice(0, -1).join('.');
if (originalFilename === '') {
originalFilename = 'image.' + extension;
}
newItem.binary = {
data: {
fileName: fileName + extension,
data: jpgBase64,
fileType: 'image',
fileSize: Math.round(imageBuffer.length / 1024 * 10) / 10 + ' kB',
fileExtension: extension,
mimeType: outputFileFormat,
},
};
}
returnData.push(newItem);
}
catch (error) {
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message } });
continue;
}
throw new n8n_workflow_1.NodeApiError(this.getNode(), error);
}
}
return [returnData];
}
}
exports.ImageFormatConverter = ImageFormatConverter;
//# sourceMappingURL=ImageFormatConverter.node.js.map