@tryghost/image-transform 1.0.31 → 1.1.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/lib/transform.js +60 -17
- package/package.json +4 -4
package/lib/transform.js
CHANGED
|
@@ -17,13 +17,38 @@ const canTransformFiles = () => {
|
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Check if this tool can handle a particular extension
|
|
20
|
-
* NOTE: .gif optimization is currently not supported by sharp but will be soon
|
|
21
|
-
* as there has been support added in underlying libvips library https://github.com/lovell/sharp/issues/1372
|
|
22
|
-
* As for .svg files, sharp only supports conversion to png, and this does not
|
|
23
|
-
* play well with animated svg files
|
|
24
20
|
* @param {String} ext the extension to check, including the leading dot
|
|
25
21
|
*/
|
|
26
|
-
const canTransformFileExtension = ext => !['.
|
|
22
|
+
const canTransformFileExtension = ext => !['.ico'].includes(ext);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Check if this tool can handle a particular extension, only to resize (= not convert format)
|
|
26
|
+
* - In this case we don't want to resize SVG's (doesn't save file size)
|
|
27
|
+
* - We don't want to resize GIF's (because we would lose the animation)
|
|
28
|
+
* So this is a 'should' instead of a 'could'. Because Sharp can handle them, but animations are lost.
|
|
29
|
+
* This is 'resize' instead of 'transform', because for the transform we might want to convert a SVG to a PNG, which is perfectly possible.
|
|
30
|
+
* @param {String} ext the extension to check, including the leading dot
|
|
31
|
+
*/
|
|
32
|
+
const shouldResizeFileExtension = ext => !['.ico', '.svg', '.svgz'].includes(ext);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Can we output animation (prevents outputting animated JPGs that are just all the pages listed under each other)
|
|
36
|
+
* @param {keyof import('sharp').FormatEnum} format the extension to check, EXCLUDING the leading dot
|
|
37
|
+
*/
|
|
38
|
+
const doesFormatSupportAnimation = format => ['webp', 'gif'].includes(format);
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Check if this tool can convert to a particular format (used in the format option of ResizeFromBuffer)
|
|
42
|
+
* @param {String} format the format to check, EXCLUDING the leading dot
|
|
43
|
+
* @returns {ext is keyof import('sharp').FormatEnum}
|
|
44
|
+
*/
|
|
45
|
+
const canTransformToFormat = format => [
|
|
46
|
+
'gif',
|
|
47
|
+
'jpeg',
|
|
48
|
+
'jpg',
|
|
49
|
+
'png',
|
|
50
|
+
'webp'
|
|
51
|
+
].includes(format);
|
|
27
52
|
|
|
28
53
|
/**
|
|
29
54
|
* @NOTE: Sharp cannot operate on the same image path, that's why we have to use in & out paths.
|
|
@@ -50,33 +75,49 @@ const unsafeResizeFromPath = (options = {}) => {
|
|
|
50
75
|
* Resize an image
|
|
51
76
|
*
|
|
52
77
|
* @param {Buffer} originalBuffer image to resize
|
|
53
|
-
* @param {{width, height}} options
|
|
54
|
-
*
|
|
78
|
+
* @param {{width?: number, height?: number, format?: keyof import('sharp').FormatEnum, animated?: boolean, withoutEnlargement?: boolean}} [options]
|
|
79
|
+
* options.animated defaults to true for file formats where animation is supported (will always maintain animation if possible)
|
|
80
|
+
* @returns {Promise<Buffer>} the resizedBuffer
|
|
55
81
|
*/
|
|
56
|
-
const unsafeResizeFromBuffer = (originalBuffer,
|
|
82
|
+
const unsafeResizeFromBuffer = async (originalBuffer, options = {}) => {
|
|
57
83
|
const sharp = require('sharp');
|
|
58
84
|
|
|
59
85
|
// Disable the internal libvips cache - https://sharp.pixelplumbing.com/api-utility#cache
|
|
60
86
|
sharp.cache(false);
|
|
61
87
|
|
|
62
|
-
|
|
63
|
-
|
|
88
|
+
// It is safe to set animated to true for all formats, because if the input image doesn't contain animation
|
|
89
|
+
// nothing will change.
|
|
90
|
+
let animated = options.animated ?? true;
|
|
91
|
+
|
|
92
|
+
if (options.format) {
|
|
93
|
+
// Only set animated to true if the output format supports animation
|
|
94
|
+
// Else we end up with multiple images stacked on top of each other (from the source image)
|
|
95
|
+
animated = doesFormatSupportAnimation(options.format);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
let s = sharp(originalBuffer, {animated})
|
|
99
|
+
.resize(options.width, options.height, {
|
|
64
100
|
// CASE: dont make the image bigger than it was
|
|
65
|
-
withoutEnlargement: true
|
|
101
|
+
withoutEnlargement: options.withoutEnlargement ?? true
|
|
66
102
|
})
|
|
67
103
|
// CASE: Automatically remove metadata and rotate based on the orientation.
|
|
68
|
-
.rotate()
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
104
|
+
.rotate();
|
|
105
|
+
|
|
106
|
+
if (options.format) {
|
|
107
|
+
s = s.toFormat(options.format);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const resizedBuffer = await s.toBuffer();
|
|
111
|
+
return options.format || resizedBuffer.length < originalBuffer.length ? resizedBuffer : originalBuffer;
|
|
73
112
|
};
|
|
74
113
|
|
|
75
114
|
/**
|
|
76
115
|
* Internal utility to wrap all transform functions in error handling
|
|
77
116
|
* Allows us to keep Sharp as an optional dependency
|
|
78
117
|
*
|
|
79
|
-
* @param {
|
|
118
|
+
* @param {T} fn
|
|
119
|
+
* @return {T}
|
|
120
|
+
* @template {Function} T
|
|
80
121
|
*/
|
|
81
122
|
const makeSafe = fn => (...args) => {
|
|
82
123
|
try {
|
|
@@ -104,6 +145,8 @@ const generateOriginalImageName = (originalPath) => {
|
|
|
104
145
|
|
|
105
146
|
module.exports.canTransformFiles = canTransformFiles;
|
|
106
147
|
module.exports.canTransformFileExtension = canTransformFileExtension;
|
|
148
|
+
module.exports.shouldResizeFileExtension = shouldResizeFileExtension;
|
|
149
|
+
module.exports.canTransformToFormat = canTransformToFormat;
|
|
107
150
|
module.exports.generateOriginalImageName = generateOriginalImageName;
|
|
108
151
|
module.exports.resizeFromPath = makeSafe(unsafeResizeFromPath);
|
|
109
152
|
module.exports.resizeFromBuffer = makeSafe(unsafeResizeFromBuffer);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/image-transform",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"repository": "https://github.com/TryGhost/Utils/tree/main/packages/image-transform",
|
|
5
5
|
"author": "Ghost Foundation",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"c8": "7.11.
|
|
22
|
+
"c8": "7.11.3",
|
|
23
23
|
"mocha": "10.0.0",
|
|
24
24
|
"should": "13.2.3",
|
|
25
|
-
"sinon": "
|
|
25
|
+
"sinon": "14.0.0"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"@tryghost/errors": "^1.2.1",
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"optionalDependencies": {
|
|
33
33
|
"sharp": "^0.30.0"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "9936fa7ee3e45891bb0fd4aa28e1e2a2c26648aa"
|
|
36
36
|
}
|