@servable/tools 1.4.2 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"type": "module",
|
|
4
4
|
"source": "src/index.js",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
-
"version": "1.
|
|
6
|
+
"version": "1.5.0",
|
|
7
7
|
"description": "Servable tools is a utility that builds a protocol's manifest and documents it.",
|
|
8
8
|
"author": "Servable Community",
|
|
9
9
|
"license": "ISC",
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
"parse-comments": "^1.0.0",
|
|
81
81
|
"path-sanitizer": "^2.0.0",
|
|
82
82
|
"semver": "^7.5.4",
|
|
83
|
-
"
|
|
83
|
+
"jimp": "^1.6.0",
|
|
84
84
|
"underscore": "^1.13.3"
|
|
85
85
|
},
|
|
86
86
|
"devDependencies": {
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Jimp } from 'jimp'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* @description Transforms a local image file into a data payload
|
|
5
|
-
* @param
|
|
6
|
-
|
|
4
|
+
* @description Transforms a local image file into a data payload using Jimp
|
|
5
|
+
* @param props {Object} - Configuration
|
|
6
|
+
* @param props.path {string} - Path to file (used if `input` not given)
|
|
7
|
+
* @param props.input {Buffer|string} - Buffer or path
|
|
8
|
+
* @param props.width {number}
|
|
9
|
+
* @param props.height {number}
|
|
10
|
+
* @param props.maxWidth {number}
|
|
11
|
+
* @param props.maxHeight {number}
|
|
12
|
+
* @param props.quality {number}
|
|
13
|
+
* @param props.mimeType {string}
|
|
14
|
+
*/
|
|
7
15
|
export default async (props) => {
|
|
8
16
|
const {
|
|
9
17
|
path,
|
|
@@ -13,30 +21,79 @@ export default async (props) => {
|
|
|
13
21
|
maxWidth = 2048,
|
|
14
22
|
maxHeight = 1024,
|
|
15
23
|
quality = 90,
|
|
16
|
-
mimeType
|
|
24
|
+
mimeType = Jimp.MIME_PNG
|
|
17
25
|
} = props
|
|
18
26
|
|
|
19
27
|
try {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
return null //#TODO
|
|
29
|
+
const image = await Jimp.read(input || path)
|
|
30
|
+
|
|
31
|
+
// Resize while preserving aspect ratio (fit: 'inside')
|
|
32
|
+
const { width: origW, height: origH } = image.bitmap
|
|
33
|
+
|
|
34
|
+
const ratio = Math.min(maxWidth / origW, maxHeight / origH)
|
|
35
|
+
const newWidth = Math.round(origW * ratio)
|
|
36
|
+
const newHeight = Math.round(origH * ratio)
|
|
37
|
+
|
|
38
|
+
image.resize(newWidth, newHeight)
|
|
39
|
+
|
|
40
|
+
// Apply quality (JPEG or WEBP only)
|
|
41
|
+
if (mimeType === Jimp.MIME_JPEG || mimeType === Jimp.MIME_WEBP) {
|
|
42
|
+
image.quality(quality)
|
|
30
43
|
}
|
|
31
44
|
|
|
32
|
-
|
|
33
|
-
|
|
45
|
+
// Get base64
|
|
46
|
+
const base64 = await image.getBase64Async(mimeType)
|
|
47
|
+
|
|
48
|
+
return { base64 }
|
|
34
49
|
} catch (e) {
|
|
35
50
|
console.error(e)
|
|
51
|
+
return null
|
|
36
52
|
}
|
|
37
|
-
return null
|
|
38
53
|
}
|
|
39
54
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
// import sharp from 'sharp'
|
|
58
|
+
|
|
59
|
+
// /**
|
|
60
|
+
// * @description Transforms a local image file into a data payload
|
|
61
|
+
// * @param path Path to file
|
|
62
|
+
// */
|
|
63
|
+
// export default async (props) => {
|
|
64
|
+
// const {
|
|
65
|
+
// path,
|
|
66
|
+
// input,
|
|
67
|
+
// width = 600,
|
|
68
|
+
// height = 200,
|
|
69
|
+
// maxWidth = 2048,
|
|
70
|
+
// maxHeight = 1024,
|
|
71
|
+
// quality = 90,
|
|
72
|
+
// mimeType
|
|
73
|
+
// } = props
|
|
74
|
+
|
|
75
|
+
// try {
|
|
76
|
+
// const buffer = await sharp(input ? input : path)
|
|
77
|
+
// // .resize(width, height, {
|
|
78
|
+
// .resize(maxWidth, maxHeight, {
|
|
79
|
+
// fit: 'inside',
|
|
80
|
+
// })
|
|
81
|
+
// .webp({ quality })
|
|
82
|
+
// .toBuffer({ resolveWithObject: true })
|
|
83
|
+
|
|
84
|
+
// if (!buffer || !buffer.data) {
|
|
85
|
+
// return null
|
|
86
|
+
// }
|
|
87
|
+
|
|
88
|
+
// const data = { base64: `${buildPrefix(mimeType)},${buffer.data.toString("base64")}` }
|
|
89
|
+
// return data
|
|
90
|
+
// } catch (e) {
|
|
91
|
+
// console.error(e)
|
|
92
|
+
// }
|
|
93
|
+
// return null
|
|
94
|
+
// }
|
|
95
|
+
|
|
96
|
+
// const buildPrefix = mimeType => {
|
|
97
|
+
// return `data:${mimeType};base64`
|
|
98
|
+
// }
|
|
99
|
+
|