-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
example.mjs
75 lines (51 loc) · 2.2 KB
/
example.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { readFileSync, writeFileSync } from 'node:fs'
import {
losslessCompressPng,
compressJpeg,
pngQuantize,
Transformer,
ResizeFilterType,
ChromaSubsampling,
} from '@napi-rs/image'
import chalk from 'chalk'
const PNG = readFileSync('./un-optimized.png')
const JPEG = readFileSync('./un-optimized.jpg')
// https://github.com/ianare/exif-samples/blob/master/jpg/orientation/portrait_5.jpg
const WITH_EXIF = readFileSync('./with-exif.jpg')
const SVG = readFileSync('./input-debian.svg')
writeFileSync('optimized-lossless.png', await losslessCompressPng(PNG))
console.info(chalk.green('Lossless compression png done'))
writeFileSync(
'optimized-lossy.png',
await pngQuantize(PNG, {
maxQuality: 75,
}),
)
console.info(chalk.green('Lossy compression png done'))
writeFileSync('optimized-lossless.jpg', await compressJpeg(JPEG))
console.info(chalk.green('Lossless compression jpeg done'))
writeFileSync('optimized-lossy.jpg', await compressJpeg(JPEG, { quality: 75 }))
console.info(chalk.green('Lossy compression jpeg done'))
writeFileSync('optimized-lossless.webp', await new Transformer(PNG).webpLossless())
console.info(chalk.green('Lossless encoding webp from PNG done'))
writeFileSync('optimized-lossy-png.webp', await new Transformer(PNG).webp(75))
console.info(chalk.green('Encoding webp from PNG done'))
writeFileSync('optimized-lossless-png.avif', await new Transformer(PNG).avif({ quality: 100 }))
console.info(chalk.green('Lossless encoding avif from PNG done'))
writeFileSync(
'optimized-lossy-png.avif',
await new Transformer(PNG).avif({ quality: 75, chromaSubsampling: ChromaSubsampling.Yuv420 }),
)
console.info(chalk.green('Lossy encoding avif from PNG done'))
writeFileSync(
'output-exif.webp',
await new Transformer(WITH_EXIF)
.rotate()
.resize(450 / 2, null, ResizeFilterType.Lanczos3)
.webp(75),
)
console.info(chalk.green('Encoding webp from JPEG with EXIF done'))
writeFileSync('output-overlay-png.png', await new Transformer(PNG).overlay(PNG, 200, 200).png())
console.info(chalk.green('Overlay an image done'))
writeFileSync('output-debian.jpeg', await Transformer.fromSvg(SVG, 'rgba(238, 235, 230, .9)').jpeg())
console.info(chalk.green('Encoding jpeg from SVG done'))