@tryghost/image-transform 1.0.33 → 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.
Files changed (2) hide show
  1. package/lib/transform.js +60 -17
  2. package/package.json +2 -2
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 => !['.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
+ * @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
- * @returns {Buffer} the resizedBuffer
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, {width, height} = {}) => {
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
- return sharp(originalBuffer)
63
- .resize(width, height, {
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
- .toBuffer()
70
- .then((resizedBuffer) => {
71
- return resizedBuffer.length < originalBuffer.length ? resizedBuffer : originalBuffer;
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 {Function} fn
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.33",
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",
@@ -32,5 +32,5 @@
32
32
  "optionalDependencies": {
33
33
  "sharp": "^0.30.0"
34
34
  },
35
- "gitHead": "ee7a05a733c50a4eb7c7b972d0f52a7b3d58751e"
35
+ "gitHead": "9936fa7ee3e45891bb0fd4aa28e1e2a2c26648aa"
36
36
  }