-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
sharp.mjs
91 lines (56 loc) · 2.22 KB
/
sharp.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Test sharp behavior
import { readFileSync, writeFileSync } from 'fs'
import sharp from 'sharp'
import { ChromaSubsampling, Transformer, FastResizeFilter, ResizeFilterType } from '@napi-rs/image'
// https://github.com/ianare/exif-samples/blob/master/jpg/orientation/portrait_5.jpg
const WITH_EXIF = readFileSync('./with-exif.jpg')
const NASA = readFileSync('./nasa-4928x3279.png')
console.time('sharp webp')
const sharpOutputWebp = await sharp(WITH_EXIF)
.rotate()
.resize(450 / 2)
.webp({ quality: 75 })
.toBuffer()
console.timeEnd('sharp webp')
writeFileSync('output-exif.sharp.webp', sharpOutputWebp)
console.time('@napi-rs/image webp')
const imageOutputWebp = await new Transformer(WITH_EXIF)
.rotate()
.resize(450 / 2)
.webp(75)
console.timeEnd('@napi-rs/image webp')
writeFileSync('output-exif.image.webp', imageOutputWebp)
const imageOutputWithoutRotateWebp = await new Transformer(WITH_EXIF).resize(450 / 2).webp(75)
writeFileSync('output-exif.no-rotate.image.webp', imageOutputWithoutRotateWebp)
console.time('sharp avif')
const sharpOutputAvif = await sharp(WITH_EXIF)
.rotate()
.resize(450 / 2)
.avif({ quality: 70, chromaSubsampling: '4:2:0' })
.toBuffer()
console.timeEnd('sharp avif')
writeFileSync('output-exif.sharp.avif', sharpOutputAvif)
console.time('@napi-rs/image avif')
const imageOutputAvif = await new Transformer(WITH_EXIF)
.rotate()
.resize(450 / 2)
.avif({ quality: 70, chromaSubsampling: ChromaSubsampling.Yuv420 })
console.timeEnd('@napi-rs/image avif')
writeFileSync('output-exif.image.avif', imageOutputAvif)
console.time('sharp resize')
const outputSharp = await sharp(NASA, { sequentialRead: true }).resize(1024).png().toBuffer()
console.timeEnd('sharp resize')
writeFileSync('nasa-small.sharp.png', outputSharp)
console.time('@napi-rs/image resize')
const outputImage = await new Transformer(NASA).resize(1024, null, ResizeFilterType.Lanczos3).png()
console.timeEnd('@napi-rs/image resize')
writeFileSync('nasa-small.image.png', outputImage)
console.time('fast resize')
const output = await new Transformer(NASA)
.fastResize({
width: 1024,
filter: FastResizeFilter.Lanczos3,
})
.png()
console.timeEnd('fast resize')
writeFileSync('nasa-small.png', output)