Last active
July 19, 2022 23:26
-
-
Save tatsuyasusukida/928d7a6f655d07f49551e53dd3861da8 to your computer and use it in GitHub Desktop.
♯ How to compress jpeg images with sharp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/img/ | |
/img-original/ | |
/node_modules/ | |
/package-lock.json | |
# Do not ignore package-lock.json other than gist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import path from 'path' | |
import fsPromises from 'fs/promises' | |
import sharp from 'sharp' | |
export async function compressImages (srcdir, dstdir) { | |
const dirents = await fsPromises.readdir(srcdir, { | |
withFileTypes: true, | |
}) | |
await fsPromises.mkdir(dstdir, {recursive: true}) | |
for (const dirent of dirents) { | |
if (dirent.isDirectory()) { | |
const srcdirJoin = path.join(srcdir, dirent.name) | |
const dstdirJoin = path.join(dstdir, dirent.name) | |
await compressImages(srcdirJoin, dstdirJoin) | |
} else { | |
const extname = path.extname(dirent.name) | |
if (extname === '.jpg') { | |
const src = path.join(srcdir, dirent.name) | |
const dst = path.join(dstdir, dirent.name) | |
const options = {mozjpet: true, quality: 80} | |
await sharp(src).jpeg(options).toFile(dst) | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import path from 'path' | |
import {compressImages} from './compress-images.mjs' | |
main() | |
async function main () { | |
try { | |
const {pathname} = new URL(import.meta.url) | |
const srcdir = path.join(pathname, '../img-original') | |
const dstdir = path.join(pathname, '../img') | |
await compressImages(srcdir, dstdir) | |
} catch (err) { | |
console.error(err) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "compress-jpeg-image", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"postinstall": "mkdir -p img-original && cd img-original && curl -OJ 'https://images.pexels.com/photos/2662116/pexels-photo-2662116.jpeg?cs=srgb&dl=pexels-jaime-reimer-2662116.jpg&fm=jpg'", | |
"start": "node main.mjs" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"sharp": "^0.30.7" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment