@tryghost/image-transform 1.0.32 → 1.2.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.
Files changed (2) hide show
  1. package/lib/transform.js +62 -17
  2. package/package.json +3 -3
package/lib/transform.js CHANGED
@@ -17,13 +17,40 @@ 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 => !['.gif', '.svg', '.svgz', '.ico'].includes(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
+ * Sharp doesn't support AVIF image sequences yet (animation)
37
+ * @param {keyof import('sharp').FormatEnum} format the extension to check, EXCLUDING the leading dot
38
+ */
39
+ const doesFormatSupportAnimation = format => ['webp', 'gif'].includes(format);
40
+
41
+ /**
42
+ * Check if this tool can convert to a particular format (used in the format option of ResizeFromBuffer)
43
+ * @param {String} format the format to check, EXCLUDING the leading dot
44
+ * @returns {ext is keyof import('sharp').FormatEnum}
45
+ */
46
+ const canTransformToFormat = format => [
47
+ 'gif',
48
+ 'jpeg',
49
+ 'jpg',
50
+ 'png',
51
+ 'webp',
52
+ 'avif'
53
+ ].includes(format);
27
54
 
28
55
  /**
29
56
  * @NOTE: Sharp cannot operate on the same image path, that's why we have to use in & out paths.
@@ -50,33 +77,49 @@ const unsafeResizeFromPath = (options = {}) => {
50
77
  * Resize an image
51
78
  *
52
79
  * @param {Buffer} originalBuffer image to resize
53
- * @param {{width, height}} options
54
- * @returns {Buffer} the resizedBuffer
80
+ * @param {{width?: number, height?: number, format?: keyof import('sharp').FormatEnum, animated?: boolean, withoutEnlargement?: boolean}} [options]
81
+ * options.animated defaults to true for file formats where animation is supported (will always maintain animation if possible)
82
+ * @returns {Promise<Buffer>} the resizedBuffer
55
83
  */
56
- const unsafeResizeFromBuffer = (originalBuffer, {width, height} = {}) => {
84
+ const unsafeResizeFromBuffer = async (originalBuffer, options = {}) => {
57
85
  const sharp = require('sharp');
58
86
 
59
87
  // Disable the internal libvips cache - https://sharp.pixelplumbing.com/api-utility#cache
60
88
  sharp.cache(false);
61
89
 
62
- return sharp(originalBuffer)
63
- .resize(width, height, {
90
+ // It is safe to set animated to true for all formats, because if the input image doesn't contain animation
91
+ // nothing will change.
92
+ let animated = options.animated ?? true;
93
+
94
+ if (options.format) {
95
+ // Only set animated to true if the output format supports animation
96
+ // Else we end up with multiple images stacked on top of each other (from the source image)
97
+ animated = doesFormatSupportAnimation(options.format);
98
+ }
99
+
100
+ let s = sharp(originalBuffer, {animated})
101
+ .resize(options.width, options.height, {
64
102
  // CASE: dont make the image bigger than it was
65
- withoutEnlargement: true
103
+ withoutEnlargement: options.withoutEnlargement ?? true
66
104
  })
67
105
  // CASE: Automatically remove metadata and rotate based on the orientation.
68
- .rotate()
69
- .toBuffer()
70
- .then((resizedBuffer) => {
71
- return resizedBuffer.length < originalBuffer.length ? resizedBuffer : originalBuffer;
72
- });
106
+ .rotate();
107
+
108
+ if (options.format) {
109
+ s = s.toFormat(options.format);
110
+ }
111
+
112
+ const resizedBuffer = await s.toBuffer();
113
+ return options.format || resizedBuffer.length < originalBuffer.length ? resizedBuffer : originalBuffer;
73
114
  };
74
115
 
75
116
  /**
76
117
  * Internal utility to wrap all transform functions in error handling
77
118
  * Allows us to keep Sharp as an optional dependency
78
119
  *
79
- * @param {Function} fn
120
+ * @param {T} fn
121
+ * @return {T}
122
+ * @template {Function} T
80
123
  */
81
124
  const makeSafe = fn => (...args) => {
82
125
  try {
@@ -104,6 +147,8 @@ const generateOriginalImageName = (originalPath) => {
104
147
 
105
148
  module.exports.canTransformFiles = canTransformFiles;
106
149
  module.exports.canTransformFileExtension = canTransformFileExtension;
150
+ module.exports.shouldResizeFileExtension = shouldResizeFileExtension;
151
+ module.exports.canTransformToFormat = canTransformToFormat;
107
152
  module.exports.generateOriginalImageName = generateOriginalImageName;
108
153
  module.exports.resizeFromPath = makeSafe(unsafeResizeFromPath);
109
154
  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.32",
3
+ "version": "1.2.0",
4
4
  "repository": "https://github.com/TryGhost/Utils/tree/main/packages/image-transform",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -19,7 +19,7 @@
19
19
  "access": "public"
20
20
  },
21
21
  "devDependencies": {
22
- "c8": "7.11.2",
22
+ "c8": "7.11.3",
23
23
  "mocha": "10.0.0",
24
24
  "should": "13.2.3",
25
25
  "sinon": "14.0.0"
@@ -32,5 +32,5 @@
32
32
  "optionalDependencies": {
33
33
  "sharp": "^0.30.0"
34
34
  },
35
- "gitHead": "273e3f58cdce8874bd732ddc5a3fc0aad71e5a96"
35
+ "gitHead": "60f77998eee90b3396e0df06ac0cb9879e5d39c8"
36
36
  }