Initial commit with working image processing.

parent 56c40898
{
"name": "image-converter",
"version": "0.0.0",
"description": "A small lightweight jpeg or png to weboptimized file formats converter",
"main": "index.js",
"repository": "https://gitlab.white-it.net/Winschu/image-converter.git",
"author": "Marcel Dominic Bandowski <mbandowski@stud.hs-bremen.de>",
"license": "MIT",
"private": false,
"devDependencies": {
"@types/imagemin": "^6.0.0",
"colors": "latest",
"imagemin": "^6.0.0",
"imagemin-jpegtran": "^5.0.2",
"imagemin-pngquant": "^6.0.0",
"imagemin-webp": "^4.1.0",
"typescript": "^3.1.5"
},
"dependencies": {
"@types/filesize": "^3.6.0",
"@types/sharp": "^0.21.0",
"file-extension": "^4.0.5",
"filesize": "^3.6.1",
"sharp": "^0.21.0"
}
}
import {
blue as blueColor,
green as greenColor,
rainbow as rainbowColor,
red as redColor,
yellow as yellowColor
} from "colors";
export function consoleError(text: string): void {
console.error(redColor(text));
}
export function consoleErrorCode(err: NodeJS.ErrnoException) {
consoleError(`An error happened: ${err.code}`);
}
export function consoleWarning(text: string): void {
console.warn(yellowColor(text));
}
export function consoleSuccess(text: string): void {
console.log(greenColor(text));
}
export function consoleInfo(text: string): void {
console.log(blueColor(text));
}
export function consoleSpecial(text: string): void {
console.log(rainbowColor(text));
}
import * as sharp from "sharp";
import {consoleError, consoleInfo, consoleSpecial, consoleSuccess} from "./Helper";
import {OutputInfo} from "sharp";
import * as filesize from "filesize";
const fileExt = require("file-extension");
const inputPath: string = "./img/input/";
const outputPath: string = "./img/output/";
const inputFile: string = `${inputPath}lena.png`;
const image: sharp.Sharp = sharp(inputFile, {failOnError: true});
let fileSizes: { original: number, jpeg: number, webp: number } = {original: 0, jpeg: 0, webp: 0};
//Read input file metadata
const inputFileFormat: string = fileExt(inputFile);
if (inputFileFormat == ("jpeg" || "jpg")) {
readOriginalImage(image.clone()).then(() => {
consoleSuccess("Original image inspected!");
}).then(() => {
convertImage("WEBP", image.clone()).then(() => {
consoleSuccess("Operation completed!");
consoleSuccess(filesize(fileSizes.original - fileSizes.webp));
});
});
}
if (inputFileFormat == "png") {
consoleInfo("PNG file detected");
readOriginalImage(image.clone()).then(() => {
consoleSuccess("Original image inspected!");
}).then(() => {
convertImage("JPEG", image.clone()).then(() => {
consoleSuccess("Operation completed!");
consoleSuccess(filesize(fileSizes.original - fileSizes.webp));
});
}).then(() => {
convertImage("WEBP", image.clone()).then(() => {
consoleSuccess("Operation completed!");
consoleSuccess(filesize(fileSizes.original - fileSizes.webp));
});
});
}
//If PNG convert to jpeg
//Convert to WebP
async function readOriginalImage(img: sharp.Sharp): Promise<void> {
await img.toBuffer().then((): void => {
getImageBuffer().then(obj => {
fileSizes.original = obj.info.size;
consoleInfo(`Original file size: ${filesize(fileSizes.original)}`);
});
});
async function getImageBuffer(): Promise<{ data: Buffer, info: OutputInfo }> {
return await img.toBuffer({resolveWithObject: true});
}
}
async function convertImage(format: "WEBP" | "JPEG", image: sharp.Sharp): Promise<void> {
consoleSpecial("Converting started!");
switch (format) {
case "JPEG":
await convertJPEG(image);
break;
case "WEBP":
await convertWEBP(image);
break;
}
async function convertWEBP(img: sharp.Sharp): Promise<void> {
await img.metadata()
.then((): Promise<void> => {
return img
.webp({
alphaQuality: 100,
lossless: false,
nearLossless: false
})
.toFile(`${outputPath}lena.webp`).then(info => {
consoleInfo(`Image size: ${info.width}x${info.height}`);
fileSizes.webp = info.size;
consoleInfo(`File size: ${filesize(fileSizes.webp)}`);
}).catch(err => {
consoleError(err);
});
});
}
async function convertJPEG(img: sharp.Sharp): Promise<void> {
await img.metadata()
.then( (): Promise<void> => {
return img
.jpeg({
})
.toFile(`${outputPath}lena.jpeg`).then(info => {
consoleInfo(`Image size: ${info.width}x${info.height}`);
fileSizes.webp = info.size;
consoleInfo(`File size: ${filesize(fileSizes.webp)}`);
}).catch(err => {
consoleError(err);
});
});
}
}
\ No newline at end of file
{
"compilerOptions": {
"module": "commonjs",
"target": "es2018",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": ".",
"typeRoots": [
"./node_modules/@types"
],
"paths": {
}
},
"exclude": [
"node_modules"
]
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment