Skip to content

Commit 793fadf

Browse files
authored
Added support for converting image types, gifs and svgs (#191)
refs https://github.com/TryGhost/Team/issues/1652 refs TryGhost/Ghost#13319 - Added support for animated webp and gifs optimization and resizing - Added optinal `format` option to `unsafeResizeFromBuffer` and `resizeFromBuffer`. E.g. allows you to convert a .svg file to a .png. - Added optional `animated` option to `unsafeResizeFromBuffer` and `resizeFromBuffer`. Defaults to 'maintain animation'. - Added optional `withoutEnlargement` option to `unsafeResizeFromBuffer` and `resizeFromBuffer`. Defaults to true. Required to increase SVG size. - Removed gif and svg from `canTransformFileExtension`. They are supported by sharp now. - Added `shouldResizeFileExtension` method, which returns if we should resize an image. This is required to prevent resizing SVG files (while it is supported, it is not desired), while allowing them to be converted to PNG (thats why a new method was needed). - Added `canTransformToFormat` to validate the `format` option. - Improved TS/JSDoc type inheritance when `makeSafe` is used.
1 parent e7abf0a commit 793fadf

File tree

2 files changed

+91
-21
lines changed

2 files changed

+91
-21
lines changed

packages/image-transform/lib/transform.js

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,38 @@ const canTransformFiles = () => {
1717

1818
/**
1919
* 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
2420
* @param {String} ext the extension to check, including the leading dot
2521
*/
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);
2752

2853
/**
2954
* @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 = {}) => {
5075
* Resize an image
5176
*
5277
* @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
5581
*/
56-
const unsafeResizeFromBuffer = (originalBuffer, {width, height} = {}) => {
82+
const unsafeResizeFromBuffer = async (originalBuffer, options = {}) => {
5783
const sharp = require('sharp');
5884

5985
// Disable the internal libvips cache - https://sharp.pixelplumbing.com/api-utility#cache
6086
sharp.cache(false);
6187

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, {
64100
// CASE: dont make the image bigger than it was
65-
withoutEnlargement: true
101+
withoutEnlargement: options.withoutEnlargement ?? true
66102
})
67103
// 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;
73112
};
74113

75114
/**
76115
* Internal utility to wrap all transform functions in error handling
77116
* Allows us to keep Sharp as an optional dependency
78117
*
79-
* @param {Function} fn
118+
* @param {T} fn
119+
* @return {T}
120+
* @template {Function} T
80121
*/
81122
const makeSafe = fn => (...args) => {
82123
try {
@@ -104,6 +145,8 @@ const generateOriginalImageName = (originalPath) => {
104145

105146
module.exports.canTransformFiles = canTransformFiles;
106147
module.exports.canTransformFileExtension = canTransformFileExtension;
148+
module.exports.shouldResizeFileExtension = shouldResizeFileExtension;
149+
module.exports.canTransformToFormat = canTransformToFormat;
107150
module.exports.generateOriginalImageName = generateOriginalImageName;
108151
module.exports.resizeFromPath = makeSafe(unsafeResizeFromPath);
109152
module.exports.resizeFromBuffer = makeSafe(unsafeResizeFromBuffer);

packages/image-transform/test/transform.test.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,54 @@ describe('Transform', function () {
2323
});
2424

2525
describe('canTransformFileExtension', function () {
26-
it('returns false for ".gif"', function () {
26+
it('returns true for ".gif"', function () {
2727
should.equal(
2828
transform.canTransformFileExtension('.gif'),
29+
true
30+
);
31+
});
32+
it('returns true for ".svg"', function () {
33+
should.equal(
34+
transform.canTransformFileExtension('.svg'),
35+
true
36+
);
37+
});
38+
it('returns true for ".svgz"', function () {
39+
should.equal(
40+
transform.canTransformFileExtension('.svgz'),
41+
true
42+
);
43+
});
44+
it('returns false for ".ico"', function () {
45+
should.equal(
46+
transform.canTransformFileExtension('.ico'),
2947
false
3048
);
3149
});
50+
});
51+
52+
describe('shouldResizeFileExtension', function () {
53+
it('returns true for ".gif"', function () {
54+
should.equal(
55+
transform.shouldResizeFileExtension('.gif'),
56+
true
57+
);
58+
});
3259
it('returns false for ".svg"', function () {
3360
should.equal(
34-
transform.canTransformFileExtension('.svg'),
61+
transform.shouldResizeFileExtension('.svg'),
3562
false
3663
);
3764
});
3865
it('returns false for ".svgz"', function () {
3966
should.equal(
40-
transform.canTransformFileExtension('.svgz'),
67+
transform.shouldResizeFileExtension('.svgz'),
4168
false
4269
);
4370
});
4471
it('returns false for ".ico"', function () {
4572
should.equal(
46-
transform.canTransformFileExtension('.ico'),
73+
transform.shouldResizeFileExtension('.ico'),
4774
false
4875
);
4976
});

0 commit comments

Comments
 (0)