libSquooshでWebP化
import {ImagePool} from "@squoosh/lib";
const imagePool = new ImagePool(_threads);
const image = imagePool.ingestImage(_file);
await image.preprocess(_preprocessOptions);
await image.encode(_encodeOptions);
const rawEncodedImage = image.encodedWith.webp.binary;
await imagePool.close();
ImagePool
ImagePoolimport {cpus} from "os";
const imagePool = new ImagePool(cpus().length);
Make sure to only create 1 ImagePool when performing parallel image processing
ImagePool when performing parallel image processingImagePool.ingestImage
ImagePool.ingestImageimport {readFile} from "fs/promises";
const image = imagePool.ingestImage(await readFile("path/to/image.png"));
SVG
import sharp from "sharp";
const image = imagePool.ingestImage(
await sharp("path/to/image.svg")
.resize({width: 333})
.webp({lossless: true, quality: 100})
.toBuffer()
);
sharp
sharpsharpオブジェクトが作成できます。
Sharp.resize
Sharp.resizeSharp.webp
Sharp.webpSharp.toBuffer
Sharp.toBufferImage.preprocess
Image.preprocessawait image.preprocess({resize: {
width: 333,
// height: 333,
}});
resize
resizeImage.encode
Image.encodeawait image.encode({webp: {lossless: 1}});
webp
webplossless
losslessImage.encodedWith.webp.binary
Image.encodedWith.webp.binaryimport {writeFile} from "fs/promises";
await writeFile("path/to/outputFile.webp", image.encodedWith.webp.binary);
ImagePool.close
ImagePool.close