I want to make a Google Cloud Function that will correctly set the content type of uploaded files. I know how to do this with GraphicsMagick or ImageMagick, but I'm not sure if Google Cloud Function has those native libraries. How do I find out if they have them or failing that how do I get them installed?
Asked
Active
Viewed 5,349 times
1 Answers
14
Google Cloud Functions run in a container that has ImageMagick installed. Somehow the Firebase documentation seems to have best documentation for it. From there:
Cloud Functions provides an image-processing program called
ImageMagick
that can perform manipulations on graphical image files. The following is an example of how to create a thumbnail image for an uploaded image file:// Download file from bucket. const bucket = gcs.bucket(fileBucket); const tempFilePath = `/tmp/${fileName}`; return bucket.file(filePath).download({ destination: tempFilePath }).then(() => { console.log('Image downloaded locally to', tempFilePath); // Generate a thumbnail using ImageMagick. return exec(`convert "${tempFilePath}" -thumbnail '200x200>' "${tempFilePath}"`).then(() => { console.log('Thumbnail created at', tempFilePath); // We add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail. const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, `$1thumb_$2`); // Uploading the thumbnail. return bucket.upload(tempFilePath, { destination: thumbFilePath }); }); });
This code executes the ImageMagick command line program convert to create a 200x200 thumbnail for the image saved in a temporary directory, then uploads it back to Cloud Storage.
Also see the Firebase functions-sample repo for an example of how to use it: https://github.com/firebase/functions-samples/tree/master/generate-thumbnail

Frank van Puffelen
- 565,676
- 79
- 828
- 807
-
Wild, I saw that but wasn't sure it was the "same" cloud function. Shame they don't detail how to install other binaries. – krainboltgreene Mar 27 '17 at 06:04
-
2You install other binaries by putting them in the same folder where your functions are. Either [Jason](https://cloudnext.withgoogle.com/schedule#target=building-serverless-applications-with-google-cloud-functions-c30bf9bd-c0ca-4256-b167-34a656b76703) or [Bret](https://www.youtube.com/watch?v=BybYim0HRmY) explained in a recent talk. – Frank van Puffelen Mar 27 '17 at 14:08
-
Thanks so much @frank-van-puffelen, that makes a lot of sense. I got so caught up in the world of package managers I forgot I could just ship a binary. – krainboltgreene Mar 27 '17 at 18:42
-
2Same here: I had to actually learn that from one of our Cloud folks. I just realized it wasn't just in a video, but also here: http://stackoverflow.com/questions/42773497/can-you-call-out-to-ffmpeg-in-a-firebase-cloud-function – Frank van Puffelen Mar 27 '17 at 20:56
-
1@FrankvanPuffelen do you have any links for adding binaries? I'm having some trouble getting the exec to not return a 127 error code – bryan Aug 01 '17 at 21:46
-
Remember that they give you `convert` not the full package with `magick` – alex88 Sep 03 '17 at 23:57