| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644 |
- /**
- * @node image
- * @name Image
- * @category media
- * @version 1.2.0
- * @description Image manipulation, filters, watermarks, and metadata extraction using ImageMagick
- * @icon image
- */
- const configSchema = {
- type: 'object',
- properties: {
- operation: {
- type: 'string',
- title: 'Operation',
- enum: ['info', 'resize', 'crop', 'rotate', 'filter', 'watermark'],
- enumLabels: ['Info', 'Resize', 'Crop', 'Rotate', 'Filter', 'Watermark'],
- default: 'info',
- description: 'Image operation to perform'
- },
- inputSource: {
- type: 'string',
- title: 'Input Source',
- enum: ['auto', 'base64', 'filePath', 'url', 'storage'],
- enumLabels: ['Auto-detect', 'Base64 Data', 'File Path', 'URL', 'Storage Reference'],
- default: 'auto',
- description: 'Source type for the input image'
- },
- base64Data: {
- type: 'string',
- title: 'Base64 Image Data',
- description: 'Base64-encoded image data. Supports {{variable}} interpolation.',
- showWhen: { field: 'inputSource', value: 'base64' }
- },
- filePath: {
- type: 'string',
- title: 'File Path',
- description: 'Path to image file on runner filesystem. Supports {{variable}} interpolation.',
- showWhen: { field: 'inputSource', value: 'filePath' }
- },
- url: {
- type: 'string',
- title: 'Image URL',
- description: 'URL to download image from. Supports {{variable}} interpolation.',
- showWhen: { field: 'inputSource', value: 'url' }
- },
- storageCollection: {
- type: 'string',
- title: 'Storage Collection',
- description: 'Collection name for stored image',
- showWhen: { field: 'inputSource', value: 'storage' }
- },
- storageId: {
- type: 'string',
- title: 'Storage Document ID',
- description: 'Document ID in storage collection. Supports {{variable}} interpolation.',
- showWhen: { field: 'inputSource', value: 'storage' }
- },
- includeExif: {
- type: 'boolean',
- title: 'Include EXIF Metadata',
- default: true,
- description: 'Extract EXIF metadata (camera info, GPS, date taken, etc.)',
- showWhen: { field: 'operation', value: 'info' }
- },
- resizeMode: {
- type: 'string',
- title: 'Resize Mode',
- enum: ['dimensions', 'percentage'],
- enumLabels: ['Dimensions (pixels)', 'Percentage (%)'],
- default: 'dimensions',
- description: 'How to specify the new size',
- showWhen: { field: 'operation', value: 'resize' }
- },
- resizeWidth: {
- type: 'number',
- title: 'Width',
- minimum: 1,
- maximum: 20000,
- description: 'Target width in pixels. Leave empty to auto-calculate from height.',
- showWhen: { field: 'operation', value: 'resize' }
- },
- resizeHeight: {
- type: 'number',
- title: 'Height',
- minimum: 1,
- maximum: 20000,
- description: 'Target height in pixels. Leave empty to auto-calculate from width.',
- showWhen: { field: 'operation', value: 'resize' }
- },
- resizePercentage: {
- type: 'number',
- title: 'Scale Percentage',
- minimum: 1,
- maximum: 1000,
- default: 100,
- description: 'Scale percentage (100 = original size, 50 = half, 200 = double)',
- showWhen: { field: 'operation', value: 'resize' }
- },
- maintainAspectRatio: {
- type: 'boolean',
- title: 'Maintain Aspect Ratio',
- default: true,
- description: 'Keep original proportions when resizing',
- showWhen: { field: 'operation', value: 'resize' }
- },
- cropX: {
- type: 'number',
- title: 'X Offset',
- minimum: 0,
- default: 0,
- description: 'Horizontal offset from left edge in pixels',
- showWhen: { field: 'operation', value: 'crop' }
- },
- cropY: {
- type: 'number',
- title: 'Y Offset',
- minimum: 0,
- default: 0,
- description: 'Vertical offset from top edge in pixels',
- showWhen: { field: 'operation', value: 'crop' }
- },
- cropWidth: {
- type: 'number',
- title: 'Crop Width',
- minimum: 1,
- description: 'Width of the crop region in pixels',
- showWhen: { field: 'operation', value: 'crop' }
- },
- cropHeight: {
- type: 'number',
- title: 'Crop Height',
- minimum: 1,
- description: 'Height of the crop region in pixels',
- showWhen: { field: 'operation', value: 'crop' }
- },
- rotateAngle: {
- type: 'number',
- title: 'Rotation Angle',
- default: 90,
- description: 'Rotation angle in degrees (positive = clockwise)',
- showWhen: { field: 'operation', value: 'rotate' }
- },
- rotatePreset: {
- type: 'string',
- title: 'Quick Rotate',
- enum: ['custom', '90', '180', '270'],
- enumLabels: ['Custom Angle', '90 (CW)', '180', '270 (CCW)'],
- default: 'custom',
- description: 'Common rotation presets or use custom angle',
- showWhen: { field: 'operation', value: 'rotate' }
- },
- backgroundColor: {
- type: 'string',
- title: 'Background Color',
- default: 'transparent',
- description: 'Fill color for areas exposed by rotation (name, hex, or transparent)',
- showWhen: { field: 'operation', value: 'rotate' }
- },
- filterBlur: {
- type: 'boolean',
- title: 'Blur',
- default: false,
- description: 'Apply blur filter',
- showWhen: { field: 'operation', value: 'filter' }
- },
- filterBlurRadius: {
- type: 'number',
- title: 'Blur Radius',
- minimum: 0,
- maximum: 100,
- default: 5,
- description: 'Blur radius in pixels (0-100)',
- showWhen: { field: 'operation', value: 'filter' }
- },
- filterSharpen: {
- type: 'boolean',
- title: 'Sharpen',
- default: false,
- description: 'Apply sharpen filter',
- showWhen: { field: 'operation', value: 'filter' }
- },
- filterSharpenAmount: {
- type: 'number',
- title: 'Sharpen Amount',
- minimum: 0,
- maximum: 10,
- default: 1,
- description: 'Sharpen intensity (0-10)',
- showWhen: { field: 'operation', value: 'filter' }
- },
- filterGrayscale: {
- type: 'boolean',
- title: 'Grayscale',
- default: false,
- description: 'Convert to grayscale',
- showWhen: { field: 'operation', value: 'filter' }
- },
- filterSepia: {
- type: 'boolean',
- title: 'Sepia',
- default: false,
- description: 'Apply sepia tone effect',
- showWhen: { field: 'operation', value: 'filter' }
- },
- filterSepiaIntensity: {
- type: 'number',
- title: 'Sepia Intensity',
- minimum: 0,
- maximum: 100,
- default: 80,
- description: 'Sepia effect intensity (0-100%)',
- showWhen: { field: 'operation', value: 'filter' }
- },
- filterBrightness: {
- type: 'boolean',
- title: 'Adjust Brightness',
- default: false,
- description: 'Adjust image brightness',
- showWhen: { field: 'operation', value: 'filter' }
- },
- filterBrightnessValue: {
- type: 'number',
- title: 'Brightness',
- minimum: -100,
- maximum: 100,
- default: 0,
- description: 'Brightness adjustment (-100 to 100, 0 = no change)',
- showWhen: { field: 'operation', value: 'filter' }
- },
- filterContrast: {
- type: 'boolean',
- title: 'Adjust Contrast',
- default: false,
- description: 'Adjust image contrast',
- showWhen: { field: 'operation', value: 'filter' }
- },
- filterContrastValue: {
- type: 'number',
- title: 'Contrast',
- minimum: -100,
- maximum: 100,
- default: 0,
- description: 'Contrast adjustment (-100 to 100, 0 = no change)',
- showWhen: { field: 'operation', value: 'filter' }
- },
- watermarkType: {
- type: 'string',
- title: 'Watermark Type',
- enum: ['text', 'image'],
- enumLabels: ['Text Watermark', 'Image Watermark'],
- default: 'text',
- description: 'Type of watermark to apply',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- watermarkText: {
- type: 'string',
- title: 'Watermark Text',
- description: 'Text to use as watermark. Supports {{variable}} interpolation.',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- watermarkFont: {
- type: 'string',
- title: 'Font',
- default: 'Arial',
- description: 'Font name for text watermark',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- watermarkFontSize: {
- type: 'number',
- title: 'Font Size',
- minimum: 8,
- maximum: 500,
- default: 48,
- description: 'Font size in points',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- watermarkColor: {
- type: 'string',
- title: 'Text Color',
- default: 'white',
- description: 'Text color (name or hex)',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- watermarkImageSource: {
- type: 'string',
- title: 'Watermark Image Source',
- enum: ['base64', 'filePath', 'url'],
- enumLabels: ['Base64 Data', 'File Path', 'URL'],
- default: 'filePath',
- description: 'Source type for watermark image',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- watermarkImageBase64: {
- type: 'string',
- title: 'Watermark Image Base64',
- description: 'Base64-encoded watermark image',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- watermarkImagePath: {
- type: 'string',
- title: 'Watermark Image Path',
- description: 'Path to watermark image file. Supports {{variable}} interpolation.',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- watermarkImageUrl: {
- type: 'string',
- title: 'Watermark Image URL',
- description: 'URL to download watermark image from. Supports {{variable}} interpolation.',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- watermarkPosition: {
- type: 'string',
- title: 'Position',
- enum: ['center', 'northwest', 'north', 'northeast', 'west', 'east', 'southwest', 'south', 'southeast'],
- enumLabels: ['Center', 'Top Left', 'Top Center', 'Top Right', 'Left', 'Right', 'Bottom Left', 'Bottom Center', 'Bottom Right'],
- default: 'center',
- description: 'Position of the watermark on the image',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- watermarkOpacity: {
- type: 'number',
- title: 'Opacity',
- minimum: 0,
- maximum: 100,
- default: 50,
- description: 'Watermark opacity (0-100%)',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- watermarkMargin: {
- type: 'number',
- title: 'Margin',
- minimum: 0,
- maximum: 500,
- default: 10,
- description: 'Margin from edge in pixels',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- watermarkScale: {
- type: 'number',
- title: 'Scale (%)',
- minimum: 1,
- maximum: 100,
- default: 20,
- description: 'Scale watermark image as percentage of main image width',
- showWhen: { field: 'operation', value: 'watermark' }
- },
- outputFormat: {
- type: 'string',
- title: 'Output Format',
- enum: ['auto', 'jpeg', 'png', 'webp', 'gif'],
- enumLabels: ['Same as Input', 'JPEG', 'PNG', 'WebP', 'GIF'],
- default: 'auto',
- description: 'Output image format'
- },
- outputQuality: {
- type: 'number',
- title: 'Quality',
- minimum: 1,
- maximum: 100,
- default: 85,
- description: 'Output quality for lossy formats (JPEG, WebP). 1-100.'
- },
- timeout: {
- type: 'number',
- title: 'Timeout (ms)',
- default: 30000,
- minimum: 1000,
- maximum: 120000,
- description: 'Maximum time to wait for image processing'
- }
- },
- required: ['operation']
- };
- const inputSchema = {
- type: 'object',
- properties: {
- data: {
- type: 'any',
- description: 'Input data. Can be base64 string, file path, URL, or binary object from another node.'
- },
- file: {
- type: 'object',
- description: 'Binary file object with type, data (base64), mimeType, filename'
- },
- storage: {
- type: 'object',
- description: 'Storage reference with collection and id'
- },
- base64: {
- type: 'string',
- description: 'Base64-encoded image data'
- },
- filePath: {
- type: 'string',
- description: 'Override file path from input'
- },
- url: {
- type: 'string',
- description: 'Override URL from input'
- }
- }
- };
- const outputSchema = {
- type: 'object',
- properties: {
- width: {
- type: 'number',
- description: 'Image width in pixels'
- },
- height: {
- type: 'number',
- description: 'Image height in pixels'
- },
- format: {
- type: 'string',
- description: 'Image format (JPEG, PNG, GIF, etc.)'
- },
- colorSpace: {
- type: 'string',
- description: 'Color space (sRGB, CMYK, Grayscale, etc.)'
- },
- depth: {
- type: 'number',
- description: 'Bit depth per channel'
- },
- fileSize: {
- type: 'number',
- description: 'File size in bytes'
- },
- dpi: {
- type: 'object',
- description: 'DPI/resolution information',
- properties: {
- x: { type: 'number' },
- y: { type: 'number' },
- unit: { type: 'string' }
- }
- },
- hasAlpha: {
- type: 'boolean',
- description: 'Whether image has alpha/transparency channel'
- },
- compression: {
- type: 'string',
- description: 'Compression type used'
- },
- exif: {
- type: 'object',
- description: 'EXIF metadata (if available)',
- properties: {
- camera: {
- type: 'object',
- properties: {
- make: { type: 'string' },
- model: { type: 'string' },
- software: { type: 'string' }
- }
- },
- datetime: {
- type: 'object',
- properties: {
- original: { type: 'string' },
- digitized: { type: 'string' },
- modified: { type: 'string' }
- }
- },
- gps: {
- type: 'object',
- properties: {
- latitude: { type: 'number' },
- longitude: { type: 'number' },
- altitude: { type: 'number' },
- latitudeRef: { type: 'string' },
- longitudeRef: { type: 'string' }
- }
- },
- exposure: {
- type: 'object',
- properties: {
- time: { type: 'string' },
- fNumber: { type: 'number' },
- iso: { type: 'number' },
- program: { type: 'string' },
- bias: { type: 'string' }
- }
- },
- lens: {
- type: 'object',
- properties: {
- focalLength: { type: 'string' },
- focalLength35mm: { type: 'number' },
- aperture: { type: 'number' },
- make: { type: 'string' },
- model: { type: 'string' }
- }
- },
- orientation: { type: 'number' },
- flash: { type: 'string' },
- whiteBalance: { type: 'string' },
- meteringMode: { type: 'string' }
- }
- },
- file: {
- type: 'object',
- description: 'Binary file output (for manipulation operations)',
- properties: {
- type: { type: 'string', const: 'binary' },
- data: { type: 'string', description: 'Base64-encoded image data' },
- mimeType: { type: 'string', description: 'MIME type of the image' },
- filename: { type: 'string', description: 'Output filename' },
- size: { type: 'number', description: 'File size in bytes' }
- }
- },
- operation: {
- type: 'string',
- description: 'Operation performed'
- },
- originalWidth: {
- type: 'number',
- description: 'Original image width before manipulation'
- },
- originalHeight: {
- type: 'number',
- description: 'Original image height before manipulation'
- },
- sourceType: {
- type: 'string',
- description: 'Source type used (base64, filePath, url, storage)'
- },
- sourcePath: {
- type: 'string',
- description: 'Temporary file path used for processing'
- },
- filtersApplied: {
- type: 'array',
- description: 'List of filters applied (for filter operation)',
- items: { type: 'string' }
- },
- filterSettings: {
- type: 'object',
- description: 'Detailed filter settings applied',
- properties: {
- blur: { type: 'object', properties: { enabled: { type: 'boolean' }, radius: { type: 'number' } } },
- sharpen: { type: 'object', properties: { enabled: { type: 'boolean' }, amount: { type: 'number' } } },
- grayscale: { type: 'boolean' },
- sepia: { type: 'object', properties: { enabled: { type: 'boolean' }, intensity: { type: 'number' } } },
- brightness: { type: 'object', properties: { enabled: { type: 'boolean' }, value: { type: 'number' } } },
- contrast: { type: 'object', properties: { enabled: { type: 'boolean' }, value: { type: 'number' } } }
- }
- },
- watermark: {
- type: 'object',
- description: 'Watermark details applied',
- properties: {
- type: { type: 'string', description: 'text or image' },
- position: { type: 'string' },
- opacity: { type: 'number' },
- text: { type: 'string', description: 'Text used (for text watermark)' },
- imagePath: { type: 'string', description: 'Path to watermark image (for image watermark)' }
- }
- }
- }
- };
- function interpolate(template, data) {
- if (typeof template !== 'string') return template;
- return template.replace(/\{\{([^}]+)\}\}/g, function(match, key) {
- var keys = key.trim().split('.');
- var value = data;
- for (var i = 0; i < keys.length; i++) {
- if (value && typeof value === 'object' && keys[i] in value) {
- value = value[keys[i]];
- } else {
- return match;
- }
- }
- return value !== undefined ? String(value) : match;
- });
- }
- function generateTempPath(extension) {
- var uuid = smartbotic.utils.uuid();
- return '/tmp/smartbotic-image-' + uuid + (extension ? '.' + extension : '');
- }
- function detectImageFormat(base64Data) {
- if (!base64Data || base64Data.length < 8) return null;
- var header = smartbotic.utils.base64Decode(base64Data.substring(0, 16));
- if (!header) return null;
- if (header.charCodeAt(0) === 0xFF && header.charCodeAt(1) === 0xD8 && header.charCodeAt(2) === 0xFF) {
- return 'jpg';
- }
- if (header.substring(0, 8) === '\x89PNG\r\n\x1a\n') {
- return 'png';
- }
- if (header.substring(0, 6) === 'GIF87a' || header.substring(0, 6) === 'GIF89a') {
- return 'gif';
- }
- if (header.substring(0, 4) === 'RIFF' && header.substring(8, 12) === 'WEBP') {
- return 'webp';
- }
- if (header.substring(0, 2) === 'BM') {
- return 'bmp';
- }
- if (header.substring(0, 4) === 'II*\x00' || header.substring(0, 4) === 'MM\x00*') {
- return 'tiff';
- }
- return null;
- }
- function resolveImageInput(config, input) {
- var inputSource = config.inputSource || 'auto';
- var result = { type: null, data: null, path: null, tempPath: null };
- if (inputSource === 'filePath' || (inputSource === 'auto' && input.filePath)) {
- var filePath = input.filePath || interpolate(config.filePath, input);
- if (filePath && smartbotic.fs.exists(filePath)) {
- result.type = 'filePath';
- result.path = filePath;
- return result;
- }
- }
- if (inputSource === 'base64' || (inputSource === 'auto' && (input.base64 || config.base64Data))) {
- var base64 = input.base64 || interpolate(config.base64Data, input);
- if (base64) {
- result.type = 'base64';
- result.data = base64;
- return result;
- }
- }
- if (inputSource === 'url' || (inputSource === 'auto' && (input.url || config.url))) {
- var url = input.url || interpolate(config.url, input);
- if (url) {
- result.type = 'url';
- result.data = url;
- return result;
- }
- }
- if (inputSource === 'storage' || (inputSource === 'auto' && input.storage)) {
- var collection = input.storage ? input.storage.collection : config.storageCollection;
- var id = input.storage ? input.storage.id : interpolate(config.storageId, input);
- if (collection && id) {
- result.type = 'storage';
- result.data = { collection: collection, id: id };
- return result;
- }
- }
- if (inputSource === 'auto') {
- if (input.file && input.file.type === 'binary' && input.file.data) {
- result.type = 'base64';
- result.data = input.file.data;
- return result;
- }
- if (typeof input.data === 'string') {
- if (input.data.match(/^[A-Za-z0-9+/]+=*$/) && input.data.length > 100) {
- result.type = 'base64';
- result.data = input.data;
- return result;
- }
- if (input.data.startsWith('http://') || input.data.startsWith('https://')) {
- result.type = 'url';
- result.data = input.data;
- return result;
- }
- if (smartbotic.fs.exists(input.data)) {
- result.type = 'filePath';
- result.path = input.data;
- return result;
- }
- }
- if (input.data && typeof input.data === 'object') {
- if (input.data.type === 'binary' && input.data.data) {
- result.type = 'base64';
- result.data = input.data.data;
- return result;
- }
- if (input.data.collection && input.data.id) {
- result.type = 'storage';
- result.data = input.data;
- return result;
- }
- }
- }
- return result;
- }
- function prepareImageFile(imageInput, timeout) {
- var tempPath = null;
- if (imageInput.type === 'filePath') {
- return { path: imageInput.path, tempPath: null };
- }
- if (imageInput.type === 'base64') {
- var ext = detectImageFormat(imageInput.data) || 'img';
- tempPath = generateTempPath(ext);
- var writeResult = smartbotic.fs.writeFile(tempPath, imageInput.data, 'base64');
- if (!writeResult.success) {
- throw new Error('Failed to write temp file: ' + (writeResult.error || 'Unknown error'));
- }
- return { path: tempPath, tempPath: tempPath };
- }
- if (imageInput.type === 'url') {
- smartbotic.log.info('Downloading image from URL: ' + imageInput.data);
- var response = smartbotic.http.request({
- method: 'GET',
- url: imageInput.data,
- timeout: timeout
- });
- if (response.status < 200 || response.status >= 300) {
- throw new Error('Failed to download image: HTTP ' + response.status);
- }
- var contentType = response.headers['content-type'] || '';
- var ext = 'img';
- if (contentType.includes('jpeg') || contentType.includes('jpg')) ext = 'jpg';
- else if (contentType.includes('png')) ext = 'png';
- else if (contentType.includes('gif')) ext = 'gif';
- else if (contentType.includes('webp')) ext = 'webp';
- tempPath = generateTempPath(ext);
- var base64Data = smartbotic.utils.base64Encode(response.data);
- var writeResult = smartbotic.fs.writeFile(tempPath, base64Data, 'base64');
- if (!writeResult.success) {
- throw new Error('Failed to write downloaded image: ' + (writeResult.error || 'Unknown error'));
- }
- return { path: tempPath, tempPath: tempPath };
- }
- if (imageInput.type === 'storage') {
- var storageResult = smartbotic.storage.get(imageInput.data.collection, imageInput.data.id);
- if (!storageResult.found) {
- throw new Error('Image not found in storage: ' + imageInput.data.collection + '/' + imageInput.data.id);
- }
- var doc = storageResult.document;
- var base64Data = doc.data || doc.content || doc.base64;
- if (!base64Data) {
- throw new Error('Storage document does not contain image data');
- }
- var ext = detectImageFormat(base64Data) || 'img';
- tempPath = generateTempPath(ext);
- var writeResult = smartbotic.fs.writeFile(tempPath, base64Data, 'base64');
- if (!writeResult.success) {
- throw new Error('Failed to write storage image: ' + (writeResult.error || 'Unknown error'));
- }
- return { path: tempPath, tempPath: tempPath };
- }
- throw new Error('No valid image input found');
- }
- function cleanupTempFile(tempPath) {
- if (tempPath) {
- try {
- smartbotic.fs.unlink(tempPath);
- } catch (e) {
- smartbotic.log.warn('Failed to cleanup temp file: ' + tempPath);
- }
- }
- }
- function parseImageMagickInfo(output) {
- var info = {};
- var lines = output.split('\n');
- for (var i = 0; i < lines.length; i++) {
- var line = lines[i].trim();
- if (!line) continue;
- var colonIdx = line.indexOf(':');
- if (colonIdx > 0) {
- var key = line.substring(0, colonIdx).trim().toLowerCase();
- var value = line.substring(colonIdx + 1).trim();
- if (key === 'image' || key === 'format') {
- var formatMatch = value.match(/^(\w+)/);
- if (formatMatch) info.format = formatMatch[1].toUpperCase();
- }
- else if (key === 'geometry' || key === 'page geometry') {
- var geomMatch = value.match(/(\d+)x(\d+)/);
- if (geomMatch) {
- info.width = parseInt(geomMatch[1], 10);
- info.height = parseInt(geomMatch[2], 10);
- }
- }
- else if (key === 'colorspace') {
- info.colorSpace = value;
- }
- else if (key === 'depth') {
- var depthMatch = value.match(/(\d+)/);
- if (depthMatch) info.depth = parseInt(depthMatch[1], 10);
- }
- else if (key === 'channel depth' || key === 'channels') {
- if (value.toLowerCase().includes('alpha')) {
- info.hasAlpha = true;
- }
- }
- else if (key === 'alpha') {
- info.hasAlpha = value.toLowerCase() !== 'undefined' && value.toLowerCase() !== 'off';
- }
- else if (key === 'compression') {
- info.compression = value;
- }
- else if (key === 'resolution' || key === 'units') {
- if (!info.dpi) info.dpi = {};
- var resMatch = value.match(/([\d.]+)x([\d.]+)/);
- if (resMatch) {
- info.dpi.x = parseFloat(resMatch[1]);
- info.dpi.y = parseFloat(resMatch[2]);
- }
- if (value.includes('PixelsPerInch')) info.dpi.unit = 'dpi';
- else if (value.includes('PixelsPerCentimeter')) info.dpi.unit = 'dpcm';
- }
- else if (key === 'filesize') {
- var sizeMatch = value.match(/([\d.]+)\s*(\w+)/);
- if (sizeMatch) {
- var size = parseFloat(sizeMatch[1]);
- var unit = sizeMatch[2].toUpperCase();
- if (unit === 'B') info.fileSize = Math.round(size);
- else if (unit === 'KB' || unit === 'KIB') info.fileSize = Math.round(size * 1024);
- else if (unit === 'MB' || unit === 'MIB') info.fileSize = Math.round(size * 1024 * 1024);
- else if (unit === 'GB' || unit === 'GIB') info.fileSize = Math.round(size * 1024 * 1024 * 1024);
- }
- }
- }
- }
- if (info.hasAlpha === undefined) info.hasAlpha = false;
- return info;
- }
- function parseExifToolOutput(output) {
- var exif = {
- camera: {},
- datetime: {},
- gps: {},
- exposure: {},
- lens: {}
- };
- var lines = output.split('\n');
- for (var i = 0; i < lines.length; i++) {
- var line = lines[i].trim();
- if (!line) continue;
- var colonIdx = line.indexOf(':');
- if (colonIdx <= 0) continue;
- var key = line.substring(0, colonIdx).trim().toLowerCase().replace(/\s+/g, '');
- var value = line.substring(colonIdx + 1).trim();
- if (!value || value === '-' || value === 'n/a') continue;
- if (key === 'make' || key === 'cameramake') exif.camera.make = value;
- else if (key === 'model' || key === 'cameramodel' || key === 'cameramodelname') exif.camera.model = value;
- else if (key === 'software') exif.camera.software = value;
- else if (key === 'datetimeoriginal' || key === 'createdate') exif.datetime.original = value;
- else if (key === 'datetimedigitized' || key === 'digitizeddate') exif.datetime.digitized = value;
- else if (key === 'modifydate' || key === 'datetime') exif.datetime.modified = value;
- else if (key === 'gpslatitude') {
- var latMatch = value.match(/([\d.]+)/);
- if (latMatch) {
- exif.gps.latitude = parseFloat(latMatch[1]);
- if (value.includes('S')) exif.gps.latitude = -exif.gps.latitude;
- exif.gps.latitudeRef = value.includes('S') ? 'S' : 'N';
- }
- }
- else if (key === 'gpslongitude') {
- var lonMatch = value.match(/([\d.]+)/);
- if (lonMatch) {
- exif.gps.longitude = parseFloat(lonMatch[1]);
- if (value.includes('W')) exif.gps.longitude = -exif.gps.longitude;
- exif.gps.longitudeRef = value.includes('W') ? 'W' : 'E';
- }
- }
- else if (key === 'gpsaltitude') {
- var altMatch = value.match(/([\d.]+)/);
- if (altMatch) exif.gps.altitude = parseFloat(altMatch[1]);
- }
- else if (key === 'exposuretime' || key === 'shutterspeed') exif.exposure.time = value;
- else if (key === 'fnumber' || key === 'aperture') {
- var fMatch = value.match(/([\d.]+)/);
- if (fMatch) exif.exposure.fNumber = parseFloat(fMatch[1]);
- }
- else if (key === 'iso' || key === 'isospeedratings') {
- var isoMatch = value.match(/(\d+)/);
- if (isoMatch) exif.exposure.iso = parseInt(isoMatch[1], 10);
- }
- else if (key === 'exposureprogram') exif.exposure.program = value;
- else if (key === 'exposurecompensation' || key === 'exposurebias') exif.exposure.bias = value;
- else if (key === 'focallength') exif.lens.focalLength = value;
- else if (key === 'focallengthin35mmformat' || key === 'focallength35efl') {
- var fl35Match = value.match(/(\d+)/);
- if (fl35Match) exif.lens.focalLength35mm = parseInt(fl35Match[1], 10);
- }
- else if (key === 'maxaperturevalue') {
- var apMatch = value.match(/([\d.]+)/);
- if (apMatch) exif.lens.aperture = parseFloat(apMatch[1]);
- }
- else if (key === 'lensmake') exif.lens.make = value;
- else if (key === 'lensmodel' || key === 'lens') exif.lens.model = value;
- else if (key === 'orientation') {
- var oriMatch = value.match(/(\d)/);
- if (oriMatch) exif.orientation = parseInt(oriMatch[1], 10);
- }
- else if (key === 'flash') exif.flash = value;
- else if (key === 'whitebalance') exif.whiteBalance = value;
- else if (key === 'meteringmode') exif.meteringMode = value;
- }
- var hasData = false;
- for (var section in exif) {
- if (typeof exif[section] === 'object') {
- for (var field in exif[section]) {
- if (exif[section][field] !== undefined) {
- hasData = true;
- break;
- }
- }
- } else if (exif[section] !== undefined) {
- hasData = true;
- }
- if (hasData) break;
- }
- return hasData ? exif : null;
- }
- function executeInfoOperation(imagePath, includeExif, timeout) {
- smartbotic.log.info('Running ImageMagick identify on: ' + imagePath);
- var identifyCmd = 'identify -verbose "' + imagePath.replace(/"/g, '\\"') + '"';
- var identifyResult = smartbotic.process.exec(identifyCmd, { timeout: timeout });
- if (!identifyResult.success) {
- throw new Error('ImageMagick identify failed: ' + (identifyResult.stderr || 'Unknown error'));
- }
- var info = parseImageMagickInfo(identifyResult.stdout);
- var stat = smartbotic.fs.stat(imagePath);
- if (stat.success && !info.fileSize) {
- info.fileSize = stat.size;
- }
- if (includeExif) {
- smartbotic.log.info('Extracting EXIF metadata');
- var exiftoolCmd = 'exiftool "' + imagePath.replace(/"/g, '\\"') + '"';
- var exifResult = smartbotic.process.exec(exiftoolCmd, { timeout: timeout });
- if (exifResult.success && exifResult.stdout) {
- var exifData = parseExifToolOutput(exifResult.stdout);
- if (exifData) {
- info.exif = exifData;
- }
- } else {
- smartbotic.log.debug('No EXIF data available or exiftool not installed');
- }
- }
- return info;
- }
- function getImageDimensions(imagePath, timeout) {
- var cmd = 'identify -format "%w %h" "' + imagePath.replace(/"/g, '\\"') + '"';
- var result = smartbotic.process.exec(cmd, { timeout: timeout });
- if (!result.success) {
- throw new Error('Failed to get image dimensions: ' + (result.stderr || 'Unknown error'));
- }
- var parts = result.stdout.trim().split(' ');
- return {
- width: parseInt(parts[0], 10),
- height: parseInt(parts[1], 10)
- };
- }
- function getOutputExtension(format, originalPath) {
- if (format === 'auto') {
- var ext = originalPath.split('.').pop().toLowerCase();
- if (['jpg', 'jpeg', 'png', 'gif', 'webp'].indexOf(ext) >= 0) {
- return ext === 'jpg' ? 'jpeg' : ext;
- }
- return 'jpeg';
- }
- return format;
- }
- function getMimeType(format) {
- var mimeTypes = {
- jpeg: 'image/jpeg',
- jpg: 'image/jpeg',
- png: 'image/png',
- gif: 'image/gif',
- webp: 'image/webp'
- };
- return mimeTypes[format] || 'image/jpeg';
- }
- function buildOutputPath(format) {
- var uuid = smartbotic.utils.uuid();
- return '/tmp/smartbotic-image-out-' + uuid + '.' + format;
- }
- function readOutputFile(outputPath) {
- var readResult = smartbotic.fs.readFile(outputPath, 'base64');
- if (!readResult.success) {
- throw new Error('Failed to read output file: ' + (readResult.error || 'Unknown error'));
- }
- return readResult.data;
- }
- function getOutputFileSize(outputPath) {
- var stat = smartbotic.fs.stat(outputPath);
- return stat.success ? stat.size : 0;
- }
- function cleanupOutputFile(outputPath) {
- if (outputPath) {
- try {
- smartbotic.fs.unlink(outputPath);
- } catch (e) {
- smartbotic.log.warn('Failed to cleanup output file: ' + outputPath);
- }
- }
- }
- function buildQualityArgs(format, quality) {
- if (format === 'jpeg' || format === 'webp') {
- return ' -quality ' + quality;
- }
- if (format === 'png') {
- var pngQuality = Math.round((100 - quality) / 10);
- return ' -quality ' + (pngQuality * 10);
- }
- return '';
- }
- function executeResizeOperation(imagePath, config, timeout) {
- var dimensions = getImageDimensions(imagePath, timeout);
- var originalWidth = dimensions.width;
- var originalHeight = dimensions.height;
- var resizeMode = config.resizeMode || 'dimensions';
- var maintainAspectRatio = config.maintainAspectRatio !== false;
- var targetWidth, targetHeight;
- if (resizeMode === 'percentage') {
- var percentage = config.resizePercentage || 100;
- targetWidth = Math.round(originalWidth * percentage / 100);
- targetHeight = Math.round(originalHeight * percentage / 100);
- smartbotic.log.info('Resizing by ' + percentage + '% to ' + targetWidth + 'x' + targetHeight);
- } else {
- targetWidth = config.resizeWidth;
- targetHeight = config.resizeHeight;
- if (!targetWidth && !targetHeight) {
- throw new Error('Resize operation requires at least width or height');
- }
- if (maintainAspectRatio) {
- if (targetWidth && !targetHeight) {
- targetHeight = Math.round(originalHeight * targetWidth / originalWidth);
- } else if (targetHeight && !targetWidth) {
- targetWidth = Math.round(originalWidth * targetHeight / originalHeight);
- }
- } else {
- targetWidth = targetWidth || originalWidth;
- targetHeight = targetHeight || originalHeight;
- }
- smartbotic.log.info('Resizing to ' + targetWidth + 'x' + targetHeight + ' (aspect ratio ' + (maintainAspectRatio ? 'maintained' : 'ignored') + ')');
- }
- var outputFormat = getOutputExtension(config.outputFormat || 'auto', imagePath);
- var quality = config.outputQuality || 85;
- var outputPath = buildOutputPath(outputFormat);
- var resizeSpec = targetWidth + 'x' + targetHeight;
- if (!maintainAspectRatio && resizeMode === 'dimensions') {
- resizeSpec += '!';
- }
- var cmd = 'convert "' + imagePath.replace(/"/g, '\\"') + '"';
- cmd += ' -resize ' + resizeSpec;
- cmd += buildQualityArgs(outputFormat, quality);
- cmd += ' "' + outputPath.replace(/"/g, '\\"') + '"';
- smartbotic.log.info('Executing: ' + cmd);
- var result = smartbotic.process.exec(cmd, { timeout: timeout });
- if (!result.success) {
- cleanupOutputFile(outputPath);
- throw new Error('Resize operation failed: ' + (result.stderr || 'Unknown error'));
- }
- var base64Data = readOutputFile(outputPath);
- var fileSize = getOutputFileSize(outputPath);
- cleanupOutputFile(outputPath);
- return {
- operation: 'resize',
- originalWidth: originalWidth,
- originalHeight: originalHeight,
- width: targetWidth,
- height: targetHeight,
- format: outputFormat.toUpperCase(),
- file: {
- type: 'binary',
- data: base64Data,
- mimeType: getMimeType(outputFormat),
- filename: 'resized.' + outputFormat,
- size: fileSize
- }
- };
- }
- function executeCropOperation(imagePath, config, timeout) {
- var dimensions = getImageDimensions(imagePath, timeout);
- var originalWidth = dimensions.width;
- var originalHeight = dimensions.height;
- var cropX = config.cropX || 0;
- var cropY = config.cropY || 0;
- var cropWidth = config.cropWidth;
- var cropHeight = config.cropHeight;
- if (!cropWidth || !cropHeight) {
- throw new Error('Crop operation requires both width and height');
- }
- if (cropX < 0 || cropY < 0) {
- throw new Error('Crop offset cannot be negative');
- }
- if (cropX + cropWidth > originalWidth || cropY + cropHeight > originalHeight) {
- throw new Error('Crop region exceeds image bounds. Image is ' + originalWidth + 'x' + originalHeight + ', crop would extend to ' + (cropX + cropWidth) + 'x' + (cropY + cropHeight));
- }
- smartbotic.log.info('Cropping ' + cropWidth + 'x' + cropHeight + ' from offset ' + cropX + ',' + cropY);
- var outputFormat = getOutputExtension(config.outputFormat || 'auto', imagePath);
- var quality = config.outputQuality || 85;
- var outputPath = buildOutputPath(outputFormat);
- var cmd = 'convert "' + imagePath.replace(/"/g, '\\"') + '"';
- cmd += ' -crop ' + cropWidth + 'x' + cropHeight + '+' + cropX + '+' + cropY;
- cmd += ' +repage';
- cmd += buildQualityArgs(outputFormat, quality);
- cmd += ' "' + outputPath.replace(/"/g, '\\"') + '"';
- smartbotic.log.info('Executing: ' + cmd);
- var result = smartbotic.process.exec(cmd, { timeout: timeout });
- if (!result.success) {
- cleanupOutputFile(outputPath);
- throw new Error('Crop operation failed: ' + (result.stderr || 'Unknown error'));
- }
- var base64Data = readOutputFile(outputPath);
- var fileSize = getOutputFileSize(outputPath);
- cleanupOutputFile(outputPath);
- return {
- operation: 'crop',
- originalWidth: originalWidth,
- originalHeight: originalHeight,
- width: cropWidth,
- height: cropHeight,
- cropRegion: {
- x: cropX,
- y: cropY,
- width: cropWidth,
- height: cropHeight
- },
- format: outputFormat.toUpperCase(),
- file: {
- type: 'binary',
- data: base64Data,
- mimeType: getMimeType(outputFormat),
- filename: 'cropped.' + outputFormat,
- size: fileSize
- }
- };
- }
- function executeRotateOperation(imagePath, config, timeout) {
- var dimensions = getImageDimensions(imagePath, timeout);
- var originalWidth = dimensions.width;
- var originalHeight = dimensions.height;
- var rotatePreset = config.rotatePreset || 'custom';
- var angle;
- if (rotatePreset === 'custom') {
- angle = config.rotateAngle;
- if (angle === undefined || angle === null) {
- angle = 90;
- }
- } else {
- angle = parseInt(rotatePreset, 10);
- }
- angle = angle % 360;
- if (angle < 0) {
- angle += 360;
- }
- var backgroundColor = config.backgroundColor || 'transparent';
- smartbotic.log.info('Rotating ' + angle + ' degrees with background: ' + backgroundColor);
- var outputFormat = getOutputExtension(config.outputFormat || 'auto', imagePath);
- var quality = config.outputQuality || 85;
- var outputPath = buildOutputPath(outputFormat);
- var supportsTransparency = (outputFormat === 'png' || outputFormat === 'gif' || outputFormat === 'webp');
- var cmd = 'convert "' + imagePath.replace(/"/g, '\\"') + '"';
- if (backgroundColor === 'transparent' && supportsTransparency) {
- cmd += ' -background none';
- } else if (backgroundColor === 'transparent') {
- cmd += ' -background white';
- } else {
- cmd += ' -background "' + backgroundColor.replace(/"/g, '\\"') + '"';
- }
- cmd += ' -rotate ' + angle;
- cmd += buildQualityArgs(outputFormat, quality);
- cmd += ' "' + outputPath.replace(/"/g, '\\"') + '"';
- smartbotic.log.info('Executing: ' + cmd);
- var result = smartbotic.process.exec(cmd, { timeout: timeout });
- if (!result.success) {
- cleanupOutputFile(outputPath);
- throw new Error('Rotate operation failed: ' + (result.stderr || 'Unknown error'));
- }
- var newDimensions = getImageDimensions(outputPath, timeout);
- var base64Data = readOutputFile(outputPath);
- var fileSize = getOutputFileSize(outputPath);
- cleanupOutputFile(outputPath);
- return {
- operation: 'rotate',
- originalWidth: originalWidth,
- originalHeight: originalHeight,
- width: newDimensions.width,
- height: newDimensions.height,
- angle: angle,
- backgroundColor: backgroundColor,
- format: outputFormat.toUpperCase(),
- file: {
- type: 'binary',
- data: base64Data,
- mimeType: getMimeType(outputFormat),
- filename: 'rotated.' + outputFormat,
- size: fileSize
- }
- };
- }
- function executeFilterOperation(imagePath, config, input, timeout) {
- var dimensions = getImageDimensions(imagePath, timeout);
- var originalWidth = dimensions.width;
- var originalHeight = dimensions.height;
- var filtersApplied = [];
- var filterSettings = {};
- var filterArgs = '';
- if (config.filterBlur) {
- var blurRadius = config.filterBlurRadius || 5;
- filterArgs += ' -blur 0x' + blurRadius;
- filtersApplied.push('blur');
- filterSettings.blur = { enabled: true, radius: blurRadius };
- smartbotic.log.info('Applying blur with radius: ' + blurRadius);
- }
- if (config.filterSharpen) {
- var sharpenAmount = config.filterSharpenAmount || 1;
- filterArgs += ' -sharpen 0x' + sharpenAmount;
- filtersApplied.push('sharpen');
- filterSettings.sharpen = { enabled: true, amount: sharpenAmount };
- smartbotic.log.info('Applying sharpen with amount: ' + sharpenAmount);
- }
- if (config.filterGrayscale) {
- filterArgs += ' -colorspace Gray';
- filtersApplied.push('grayscale');
- filterSettings.grayscale = true;
- smartbotic.log.info('Applying grayscale conversion');
- }
- if (config.filterSepia) {
- var sepiaIntensity = config.filterSepiaIntensity || 80;
- filterArgs += ' -sepia-tone ' + sepiaIntensity + '%';
- filtersApplied.push('sepia');
- filterSettings.sepia = { enabled: true, intensity: sepiaIntensity };
- smartbotic.log.info('Applying sepia with intensity: ' + sepiaIntensity + '%');
- }
- if (config.filterBrightness) {
- var brightnessValue = config.filterBrightnessValue || 0;
- var brightnessPct = 100 + brightnessValue;
- filterArgs += ' -modulate ' + brightnessPct + ',100,100';
- filtersApplied.push('brightness');
- filterSettings.brightness = { enabled: true, value: brightnessValue };
- smartbotic.log.info('Adjusting brightness by: ' + brightnessValue);
- }
- if (config.filterContrast) {
- var contrastValue = config.filterContrastValue || 0;
- if (contrastValue > 0) {
- for (var i = 0; i < Math.min(contrastValue, 10); i++) {
- filterArgs += ' -contrast';
- }
- if (contrastValue > 10) {
- filterArgs += ' -sigmoidal-contrast ' + (contrastValue / 10) + ',50%';
- }
- } else if (contrastValue < 0) {
- var absContrast = Math.abs(contrastValue);
- for (var j = 0; j < Math.min(absContrast, 10); j++) {
- filterArgs += ' +contrast';
- }
- if (absContrast > 10) {
- filterArgs += ' +sigmoidal-contrast ' + (absContrast / 10) + ',50%';
- }
- }
- filtersApplied.push('contrast');
- filterSettings.contrast = { enabled: true, value: contrastValue };
- smartbotic.log.info('Adjusting contrast by: ' + contrastValue);
- }
- if (filtersApplied.length === 0) {
- throw new Error('Filter operation requires at least one filter to be enabled');
- }
- var outputFormat = getOutputExtension(config.outputFormat || 'auto', imagePath);
- var quality = config.outputQuality || 85;
- var outputPath = buildOutputPath(outputFormat);
- var cmd = 'convert "' + imagePath.replace(/"/g, '\\"') + '"';
- cmd += filterArgs;
- cmd += buildQualityArgs(outputFormat, quality);
- cmd += ' "' + outputPath.replace(/"/g, '\\"') + '"';
- smartbotic.log.info('Executing: ' + cmd);
- var result = smartbotic.process.exec(cmd, { timeout: timeout });
- if (!result.success) {
- cleanupOutputFile(outputPath);
- throw new Error('Filter operation failed: ' + (result.stderr || 'Unknown error'));
- }
- var base64Data = readOutputFile(outputPath);
- var fileSize = getOutputFileSize(outputPath);
- cleanupOutputFile(outputPath);
- return {
- operation: 'filter',
- originalWidth: originalWidth,
- originalHeight: originalHeight,
- width: originalWidth,
- height: originalHeight,
- filtersApplied: filtersApplied,
- filterSettings: filterSettings,
- format: outputFormat.toUpperCase(),
- file: {
- type: 'binary',
- data: base64Data,
- mimeType: getMimeType(outputFormat),
- filename: 'filtered.' + outputFormat,
- size: fileSize
- }
- };
- }
- function prepareWatermarkImage(config, input, timeout) {
- var watermarkType = config.watermarkType || 'text';
- if (watermarkType !== 'image') {
- return null;
- }
- var source = config.watermarkImageSource || 'filePath';
- var tempPath = null;
- if (source === 'filePath') {
- var filePath = interpolate(config.watermarkImagePath, input);
- if (!filePath) {
- throw new Error('Watermark image path is required');
- }
- if (!smartbotic.fs.exists(filePath)) {
- throw new Error('Watermark image file not found: ' + filePath);
- }
- return { path: filePath, tempPath: null };
- }
- if (source === 'base64') {
- var base64Data = config.watermarkImageBase64;
- if (!base64Data) {
- throw new Error('Watermark image base64 data is required');
- }
- var ext = detectImageFormat(base64Data) || 'png';
- tempPath = generateTempPath('watermark-' + ext);
- var writeResult = smartbotic.fs.writeFile(tempPath, base64Data, 'base64');
- if (!writeResult.success) {
- throw new Error('Failed to write watermark temp file: ' + (writeResult.error || 'Unknown error'));
- }
- return { path: tempPath, tempPath: tempPath };
- }
- if (source === 'url') {
- var url = interpolate(config.watermarkImageUrl, input);
- if (!url) {
- throw new Error('Watermark image URL is required');
- }
- smartbotic.log.info('Downloading watermark image from URL: ' + url);
- var response = smartbotic.http.request({
- method: 'GET',
- url: url,
- timeout: timeout
- });
- if (response.status < 200 || response.status >= 300) {
- throw new Error('Failed to download watermark image: HTTP ' + response.status);
- }
- var contentType = response.headers['content-type'] || '';
- var ext = 'png';
- if (contentType.includes('jpeg') || contentType.includes('jpg')) ext = 'jpg';
- else if (contentType.includes('gif')) ext = 'gif';
- else if (contentType.includes('webp')) ext = 'webp';
- tempPath = generateTempPath('watermark-' + ext);
- var base64 = smartbotic.utils.base64Encode(response.data);
- var writeResult = smartbotic.fs.writeFile(tempPath, base64, 'base64');
- if (!writeResult.success) {
- throw new Error('Failed to write downloaded watermark: ' + (writeResult.error || 'Unknown error'));
- }
- return { path: tempPath, tempPath: tempPath };
- }
- throw new Error('Invalid watermark image source: ' + source);
- }
- function executeWatermarkOperation(imagePath, config, input, timeout) {
- var dimensions = getImageDimensions(imagePath, timeout);
- var originalWidth = dimensions.width;
- var originalHeight = dimensions.height;
- var watermarkType = config.watermarkType || 'text';
- var position = config.watermarkPosition || 'center';
- var opacity = config.watermarkOpacity !== undefined ? config.watermarkOpacity : 50;
- var margin = config.watermarkMargin || 10;
- var gravityMap = {
- center: 'Center',
- northwest: 'NorthWest',
- north: 'North',
- northeast: 'NorthEast',
- west: 'West',
- east: 'East',
- southwest: 'SouthWest',
- south: 'South',
- southeast: 'SouthEast'
- };
- var gravity = gravityMap[position] || 'Center';
- var outputFormat = getOutputExtension(config.outputFormat || 'auto', imagePath);
- var quality = config.outputQuality || 85;
- var outputPath = buildOutputPath(outputFormat);
- var watermarkInfo = {
- type: watermarkType,
- position: position,
- opacity: opacity
- };
- var watermarkImageInfo = null;
- var cmd;
- if (watermarkType === 'text') {
- var text = interpolate(config.watermarkText, input);
- if (!text) {
- throw new Error('Watermark text is required');
- }
- var font = config.watermarkFont || 'Arial';
- var fontSize = config.watermarkFontSize || 48;
- var color = config.watermarkColor || 'white';
- watermarkInfo.text = text;
- var escapedText = text.replace(/"/g, '\\"').replace(/\$/g, '\\$').replace(/`/g, '\\`');
- cmd = 'convert "' + imagePath.replace(/"/g, '\\"') + '"';
- cmd += ' -gravity ' + gravity;
- cmd += ' -font "' + font.replace(/"/g, '\\"') + '"';
- cmd += ' -pointsize ' + fontSize;
- cmd += ' -fill "' + color.replace(/"/g, '\\"') + '"';
- cmd += ' -stroke none';
- cmd += ' -annotate +' + margin + '+' + margin + ' "' + escapedText + '"';
- if (opacity < 100) {
- cmd += ' -channel A -evaluate multiply ' + (opacity / 100);
- }
- cmd += buildQualityArgs(outputFormat, quality);
- cmd += ' "' + outputPath.replace(/"/g, '\\"') + '"';
- smartbotic.log.info('Applying text watermark: "' + text + '" at ' + position + ' with opacity ' + opacity + '%');
- } else {
- watermarkImageInfo = prepareWatermarkImage(config, input, timeout);
- var watermarkPath = watermarkImageInfo.path;
- watermarkInfo.imagePath = watermarkPath;
- var scale = config.watermarkScale || 20;
- var watermarkWidth = Math.round(originalWidth * scale / 100);
- smartbotic.log.info('Applying image watermark at ' + position + ' with opacity ' + opacity + '% and scale ' + scale + '%');
- cmd = 'convert "' + imagePath.replace(/"/g, '\\"') + '"';
- cmd += ' \\( "' + watermarkPath.replace(/"/g, '\\"') + '"';
- cmd += ' -resize ' + watermarkWidth + 'x';
- if (opacity < 100) {
- cmd += ' -alpha set -channel A -evaluate multiply ' + (opacity / 100) + ' +channel';
- }
- cmd += ' \\)';
- cmd += ' -gravity ' + gravity;
- cmd += ' -geometry +' + margin + '+' + margin;
- cmd += ' -composite';
- cmd += buildQualityArgs(outputFormat, quality);
- cmd += ' "' + outputPath.replace(/"/g, '\\"') + '"';
- }
- smartbotic.log.info('Executing: ' + cmd);
- var result = smartbotic.process.exec(cmd, { timeout: timeout });
- if (watermarkImageInfo && watermarkImageInfo.tempPath) {
- cleanupTempFile(watermarkImageInfo.tempPath);
- }
- if (!result.success) {
- cleanupOutputFile(outputPath);
- throw new Error('Watermark operation failed: ' + (result.stderr || 'Unknown error'));
- }
- var base64Data = readOutputFile(outputPath);
- var fileSize = getOutputFileSize(outputPath);
- cleanupOutputFile(outputPath);
- return {
- operation: 'watermark',
- originalWidth: originalWidth,
- originalHeight: originalHeight,
- width: originalWidth,
- height: originalHeight,
- watermark: watermarkInfo,
- format: outputFormat.toUpperCase(),
- file: {
- type: 'binary',
- data: base64Data,
- mimeType: getMimeType(outputFormat),
- filename: 'watermarked.' + outputFormat,
- size: fileSize
- }
- };
- }
- async function execute(config, input) {
- var operation = config.operation || 'info';
- var timeout = config.timeout || 30000;
- var supportedOperations = ['info', 'resize', 'crop', 'rotate', 'filter', 'watermark'];
- if (supportedOperations.indexOf(operation) < 0) {
- throw new Error('Operation "' + operation + '" is not yet implemented. Supported: ' + supportedOperations.join(', '));
- }
- var imageInput = resolveImageInput(config, input);
- if (!imageInput.type) {
- throw new Error('No valid image input found. Provide base64 data, file path, URL, or storage reference.');
- }
- smartbotic.log.info('Image input source: ' + imageInput.type);
- var fileInfo = null;
- try {
- fileInfo = prepareImageFile(imageInput, timeout);
- smartbotic.log.info('Processing image: ' + fileInfo.path);
- var result;
- if (operation === 'info') {
- result = executeInfoOperation(fileInfo.path, config.includeExif !== false, timeout);
- } else if (operation === 'resize') {
- result = executeResizeOperation(fileInfo.path, config, timeout);
- } else if (operation === 'crop') {
- result = executeCropOperation(fileInfo.path, config, timeout);
- } else if (operation === 'rotate') {
- result = executeRotateOperation(fileInfo.path, config, timeout);
- } else if (operation === 'filter') {
- result = executeFilterOperation(fileInfo.path, config, input, timeout);
- } else if (operation === 'watermark') {
- result = executeWatermarkOperation(fileInfo.path, config, input, timeout);
- }
- result.sourceType = imageInput.type;
- result.sourcePath = fileInfo.path;
- return result;
- } finally {
- if (fileInfo && fileInfo.tempPath) {
- cleanupTempFile(fileInfo.tempPath);
- }
- }
- }
- module.exports = { configSchema, inputSchema, outputSchema, execute };
|