Phú Nguyễn

27 bundles
1 file3 months ago
1

Convert PSD to PNG & JPG respectively

index.js

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111const PSD = require("psd");
const JIMP = require("jimp");
const fs = require("fs");

// const imgFolder = "../../../Downloads/Kimio";
const imgFolder = process.argv[2];

function getFileNameFromUrl(resourceUrl) {
    const parsedUrl = require("url").parse(resourceUrl);
    const fileName = /[^/]*$/.exec(parsedUrl.pathname)[0];

    let name = fileName.slice(0, fileName.indexOf("."));
    let ext = fileName.substr(fileName.indexOf(".") + 1);

    return { name, ext };
}

function getFilePaths(folder) {
    const imgList = fs.readdirSync(folder);
    const imgName = imgList.map(img => {
        const name = getFileNameFromUrl(img).name;
        return name;
    });
    const imgPaths = imgList.map(img => `${folder}/${img}`);

    return { paths: imgPaths, names: imgName };
}

async function PSDtoPNG(pathsAndNames) {
    fs.existsSync("./png") ? null : fs.mkdirSync("./png");
    let PNGList = { paths: [], names: [] };

    let index = 0;
    for (const file of pathsAndNames.paths) {
        await PSD.open(file)
            .then(async psd => {
                const PNGpath = `./png/${pathsAndNames.names[index]}.png`;
                const PNGname = `${pathsAndNames.names[index]}`;

                const fileExist = fs.existsSync(PNGpath);
                fileExist ? fs.unlinkSync(PNGpath) : null;

                await psd.image
                    .saveAsPng(PNGpath)
                    .then(res => {
                        console.log("saved", PNGpath);
                        PNGList.paths.push(PNGpath);
                        PNGList.names.push(PNGname);
                    })
                    .catch(err => {
                        console.log("save png", err);
                    });
            })
            .catch(err => {
                console.log("convert psd to png error ", err);
            });

        index++;
    }

    return PNGList;
}

async function PNGtoJPG(pathsAndNames) {
    fs.existsSync("./jpg") ? null : fs.mkdirSync("./jpg");
    console.log(pathsAndNames);
    let JPGList = { paths: [], names: [] };

    let index = 0;
    for (const file of pathsAndNames.paths) {
        await JIMP.read(file)
            .then(PNGfile => {
                let JPGPath = `./jpg/${pathsAndNames.names[index]}.jpg`;
                let JPGName = `${pathsAndNames.names[index]}`;

                const fileExist = fs.existsSync(JPGPath);
                fileExist ? fs.unlinkSync(JPGPath) : null;

                PNGfile.writeAsync(JPGPath)
                    .then(res => {
                        console.log("saved", JPGPath);
                        JPGList.paths.push(JPGPath);
                        JPGList.names.push(JPGName);
                    })
                    .catch(err => {
                        console.log(err);
                    });
            })
            .catch(err => {
                console.log(err);
            });

        index++;
    }

    return JPGList;
}

async function excute() {
    const filePathsAndNames = getFilePaths(imgFolder);
    await PSDtoPNG(filePathsAndNames)
        .then(async data => {
            await PNGtoJPG(data);
        })
        .catch(err => {
            console.log("catch psd to png", err);
        });
}

excute();