Skip to content

Commit 9779c58

Browse files
authored
Remove ImageMagick from thumbnail samples (1st and 2nd gen) (#1079)
1 parent ca8b2bc commit 9779c58

File tree

15 files changed

+316
-580
lines changed

15 files changed

+316
-580
lines changed

2nd-gen/thumbnails/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This sample automatically generates thumbnails for images that are uploaded to C
1111

1212
See file [functions/index.js](functions/index.js) for the thumbnail generation code.
1313

14-
The thumbnail generation is performed using ImageMagick which is installed by default on all Cloud Functions instances. This is a CLI so we execute the command from node using the [child-process-promise](https://www.npmjs.com/package/child-process-promise) package. The image is first downloaded locally from the Cloud Storage bucket to the `tmp` folder using the [google-cloud](https://github.com/GoogleCloudPlatform/google-cloud-node) SDK.
14+
The thumbnail generation is performed using [sharp](https://www.npmjs.com/package/sharp).
1515

1616
The dependencies are listed in [functions/package.json](functions/package.json).
1717

2nd-gen/thumbnails/functions/index.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ const {onObjectFinalized} = require("firebase-functions/v2/storage");
2424
const {initializeApp} = require("firebase-admin/app");
2525
const {getStorage} = require("firebase-admin/storage");
2626
const logger = require("firebase-functions/logger");
27-
const spawn = require("child-process-promise").spawn;
2827
const path = require("path");
29-
const os = require("os");
30-
const fs = require("fs");
28+
29+
// library for image resizing
30+
const sharp = require("sharp");
3131

3232
initializeApp();
3333
// [END v2storageAdditionalImports]
@@ -36,7 +36,7 @@ initializeApp();
3636
// [START v2storageGenerateThumbnail]
3737
/**
3838
* When an image is uploaded in the Storage bucket,
39-
* generate a thumbnail automatically using ImageMagick.
39+
* generate a thumbnail automatically using sharp.
4040
*/
4141
// [START v2storageGenerateThumbnailTrigger]
4242
exports.generateThumbnail = onObjectFinalized({cpu: 2}, async (event) => {
@@ -61,30 +61,30 @@ exports.generateThumbnail = onObjectFinalized({cpu: 2}, async (event) => {
6161
// [END v2storageStopConditions]
6262

6363
// [START v2storageThumbnailGeneration]
64-
// Download file from bucket.
64+
// Download file into memory from bucket.
6565
const bucket = getStorage().bucket(fileBucket);
66-
const tempPath = path.join(os.tmpdir(), fileName);
67-
await bucket.file(filePath).download({destination: tempPath});
68-
logger.log("Image downloaded locally to", tempPath);
66+
const downloadResponse = await bucket.file(filePath).download();
67+
const imageBuffer = downloadResponse[0];
68+
logger.log("Image downloaded!");
6969

70-
// Generate a thumbnail using ImageMagick.
71-
await spawn("convert", [tempPath, "-thumbnail", "200x200>", tempPath]);
72-
logger.log("Thumbnail created at", tempPath);
70+
// Generate a thumbnail using sharp.
71+
const thumbnailBuffer = await sharp(imageBuffer).resize({
72+
width: 200,
73+
height: 200,
74+
withoutEnlargement: true,
75+
}).toBuffer();
76+
logger.log("Thumbnail created");
7377

7478
// Prefix 'thumb_' to file name.
7579
const thumbFileName = `thumb_${fileName}`;
7680
const thumbFilePath = path.join(path.dirname(filePath), thumbFileName);
7781

78-
// Uploading the thumbnail.
82+
// Upload the thumbnail.
7983
const metadata = {contentType: contentType};
80-
await bucket.upload(tempPath, {
81-
destination: thumbFilePath,
84+
await bucket.file(thumbFilePath).save(thumbnailBuffer, {
8285
metadata: metadata,
8386
});
84-
85-
// Once the thumbnail has been uploaded,
86-
// delete the local file to free up disk space.
87-
return fs.unlinkSync(tempPath);
87+
return logger.log("Thumbnail uploaded!");
8888
// [END v2storageThumbnailGeneration]
8989
});
9090
// [END v2storageGenerateThumbnail]

2nd-gen/thumbnails/functions/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
"name": "generate-thumbnail-functions-quickstart",
33
"description": "Generate Thumbnail Firebase Functions sample",
44
"dependencies": {
5-
"child-process-promise": "^2.2.1",
6-
"firebase-admin": "^11.7.0",
5+
"sharp": "^0.32.1",
6+
"firebase-admin": "^11.8.0",
77
"firebase-functions": "^4.3.1"
88
},
99
"devDependencies": {
1010
"eslint": "^6.8.0",
11+
"eslint-config-google": "^0.14.0",
1112
"eslint-plugin-promise": "^4.3.1"
1213
},
1314
"scripts": {
@@ -20,7 +21,7 @@
2021
"compile": "cp ../../../tsconfig.template.json ./tsconfig-compile.json && tsc --project tsconfig-compile.json"
2122
},
2223
"engines": {
23-
"node": "16"
24+
"node": "18"
2425
},
2526
"private": true
2627
}

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ Uses an HTTP trigger.
124124
Demonstrates how to automatically convert images that are uploaded to Firebase Storage to JPEG using ImageMagick.
125125
Uses a Firebase Storage trigger.
126126

127-
### [Generate image thumbnails using ImageMagick](/generate-thumbnail)
128-
129-
Demonstrates how to automatically generate a thumbnail for images that are uploaded to Firebase Storage using ImageMagick and generate a public download link for the images.
130-
Uses a Firebase Storage trigger.
131-
132127
### [Generate image thumbnails using Node.js Stream & Sharp](/image-sharp)
133128

134129
Demonstrates how to use Node.js Stream to read image from Cloud Storage, generate a thumbnail image using Sharp and upload it back to Cloud Storage.

generate-thumbnail/README.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

generate-thumbnail/firebase.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

generate-thumbnail/functions/.eslintrc.json

Lines changed: 0 additions & 122 deletions
This file was deleted.

generate-thumbnail/functions/index.js

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)