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
ImagePool
import {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.ingestImage
import {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
sharp
sharp
オブジェクトが作成できます。
Sharp.resize
Sharp.resize
Sharp.webp
Sharp.webp
Sharp.toBuffer
Sharp.toBuffer
Image.preprocess
Image.preprocess
await image.preprocess({resize: {
width: 333,
// height: 333,
}});
resize
resize
Image.encode
Image.encode
await image.encode({webp: {lossless: 1}});
webp
webp
lossless
lossless
Image.encodedWith.webp.binary
Image.encodedWith.webp.binary
import {writeFile} from "fs/promises";
await writeFile("path/to/outputFile.webp", image.encodedWith.webp.binary);
ImagePool.close
ImagePool.close