@tryghost/image-transform 1.0.14

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2013-2021 Ghost Foundation
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Image Transform
2
+
3
+ ## Install
4
+
5
+ `npm install @tryghost/image-transform --save`
6
+
7
+ or
8
+
9
+ `yarn add @tryghost/image-transform`
10
+
11
+
12
+ ## Usage
13
+
14
+
15
+ ## Develop
16
+
17
+ This is a mono repository, managed with [lerna](https://lernajs.io/).
18
+
19
+ Follow the instructions for the top-level repo.
20
+ 1. `git clone` this repo & `cd` into it as usual
21
+ 2. Run `yarn` to install top-level dependencies.
22
+
23
+
24
+ ## Run
25
+
26
+ - `yarn dev`
27
+
28
+
29
+ ## Test
30
+
31
+ - `yarn lint` run just eslint
32
+ - `yarn test` run lint and tests
33
+
34
+
35
+
36
+
37
+ # Copyright & License
38
+
39
+ Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE).
package/index.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./lib/transform');
@@ -0,0 +1,105 @@
1
+ const Promise = require('bluebird');
2
+ const errors = require('@tryghost/errors');
3
+ const fs = require('fs-extra');
4
+ const path = require('path');
5
+
6
+ /**
7
+ * Check if this tool can handle any file transformations as Sharp is an optional dependency
8
+ */
9
+ const canTransformFiles = () => {
10
+ try {
11
+ require('sharp');
12
+ return true;
13
+ } catch (err) {
14
+ return false;
15
+ }
16
+ };
17
+
18
+ /**
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
+ * @param {String} ext the extension to check, including the leading dot
25
+ */
26
+ const canTransformFileExtension = ext => !['.gif', '.svg', '.svgz', '.ico'].includes(ext);
27
+
28
+ /**
29
+ * @NOTE: Sharp cannot operate on the same image path, that's why we have to use in & out paths.
30
+ *
31
+ * We currently can't enable compression or having more config options, because of
32
+ * https://github.com/lovell/sharp/issues/1360.
33
+ *
34
+ * Resize an image referenced by the `in` path and write it to the `out` path
35
+ * @param {{in, out, width}} options
36
+ */
37
+ const unsafeResizeFromPath = (options = {}) => {
38
+ return fs.readFile(options.in)
39
+ .then((data) => {
40
+ return unsafeResizeFromBuffer(data, {
41
+ width: options.width
42
+ });
43
+ })
44
+ .then((data) => {
45
+ return fs.writeFile(options.out, data);
46
+ });
47
+ };
48
+
49
+ /**
50
+ * Resize an image
51
+ *
52
+ * @param {Buffer} originalBuffer image to resize
53
+ * @param {{width, height}} options
54
+ * @returns {Buffer} the resizedBuffer
55
+ */
56
+ const unsafeResizeFromBuffer = (originalBuffer, {width, height} = {}) => {
57
+ const sharp = require('sharp');
58
+ return sharp(originalBuffer)
59
+ .resize(width, height, {
60
+ // CASE: dont make the image bigger than it was
61
+ withoutEnlargement: true
62
+ })
63
+ // CASE: Automatically remove metadata and rotate based on the orientation.
64
+ .rotate()
65
+ .toBuffer()
66
+ .then((resizedBuffer) => {
67
+ return resizedBuffer.length < originalBuffer.length ? resizedBuffer : originalBuffer;
68
+ });
69
+ };
70
+
71
+ /**
72
+ * Internal utility to wrap all transform functions in error handling
73
+ * Allows us to keep Sharp as an optional dependency
74
+ *
75
+ * @param {Function} fn
76
+ */
77
+ const makeSafe = fn => (...args) => {
78
+ try {
79
+ require('sharp');
80
+ } catch (err) {
81
+ return Promise.reject(new errors.InternalServerError({
82
+ message: 'Sharp wasn\'t installed',
83
+ code: 'SHARP_INSTALLATION',
84
+ err: err
85
+ }));
86
+ }
87
+ return fn(...args).catch((err) => {
88
+ throw new errors.InternalServerError({
89
+ message: 'Unable to manipulate image.',
90
+ err: err,
91
+ code: 'IMAGE_PROCESSING'
92
+ });
93
+ });
94
+ };
95
+
96
+ const generateOriginalImageName = (originalPath) => {
97
+ const parsedFileName = path.parse(originalPath);
98
+ return path.join(parsedFileName.dir, `${parsedFileName.name}_o${parsedFileName.ext}`);
99
+ };
100
+
101
+ module.exports.canTransformFiles = canTransformFiles;
102
+ module.exports.canTransformFileExtension = canTransformFileExtension;
103
+ module.exports.generateOriginalImageName = generateOriginalImageName;
104
+ module.exports.resizeFromPath = makeSafe(unsafeResizeFromPath);
105
+ module.exports.resizeFromBuffer = makeSafe(unsafeResizeFromBuffer);
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@tryghost/image-transform",
3
+ "version": "1.0.14",
4
+ "repository": "https://github.com/TryGhost/Utils/tree/master/packages/image-transform",
5
+ "author": "Ghost Foundation",
6
+ "license": "MIT",
7
+ "main": "index.js",
8
+ "scripts": {
9
+ "dev": "echo \"Implement me!\"",
10
+ "test": "NODE_ENV=testing c8 mocha './test/**/*.test.js'",
11
+ "lint": "eslint . --ext .js --cache",
12
+ "posttest": "yarn lint"
13
+ },
14
+ "files": [
15
+ "index.js",
16
+ "lib"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "devDependencies": {
22
+ "c8": "7.9.0",
23
+ "mocha": "9.0.0",
24
+ "should": "13.2.3",
25
+ "sinon": "11.0.0"
26
+ },
27
+ "dependencies": {
28
+ "@tryghost/errors": "^0.2.14",
29
+ "bluebird": "^3.7.2",
30
+ "fs-extra": "^9.1.0"
31
+ },
32
+ "optionalDependencies": {
33
+ "sharp": "^0.29.0"
34
+ },
35
+ "gitHead": "9545d13a14c1b0ac70cd80ee111f66f642eb0a4d"
36
+ }