-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
39 lines (33 loc) · 1.33 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import fs from 'fs';
import { ImageConverterFactory } from './src/ImageConverterFactory';
import { identifyImageFormat } from './src/IdentifyImageFormat';
// Function to convert an image file
export async function convertImage(inputFile: string, outputFormat: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
// Read the input image file
fs.readFile(inputFile, (err, data) => {
if (err) {
reject(err);
return;
}
// Identify the input image format
const inputFormat = identifyImageFormat(data);
// Create the appropriate image converter based on the format
const converter = ImageConverterFactory.getConverter(inputFormat);
// Decode the input image data based on its format
const decodedImageData = converter.decode(data);
// // Encode the decoded image data into the desired output format
// const encodedImageData = converter.encode(decodedImageData);
// // Write the encoded image data to a new file
// const outputFile = `output.${outputFormat}`;
// fs.writeFile(outputFile, encodedImageData, (err) => {
// if (err) {
// reject(err);
// return;
// }
// console.log(`Image converted successfully to ${outputFormat} format!`);
// resolve();
// });
});
});
}