Trigger Firebase Storage Image Resize with Cloud Functions
source link: https://www.tuicool.com/articles/iIzyIzA
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Firebase is awesome. As written in the previous JSMonday article , it allows us to write serverless applications, authenticate users and many other things.
Firebase also offers a service called Firebase Storage , which is somewhat similar to AWS S3 . You can create a bucket and then insert any kind of file inside of it. It is commonly used for storing images, but here comes the problem: what if an user is uploading an incredible heavy image? You may want to resize it!
Let’s see how you can do it using Firebase Functions .
First of all, let’s initialize a new Firebase Functions project.
Then, let’s install a couple of npm packages:
As you can see, we’re installing these three packages:
-
@google-cloud/storage
allows you to get images from Firebase Storage . -
fs-extra
, which wraps the Node.js defaultfs
module and exposes its functions with promises. -
sharp
, an amazing and high performances image manipulation library for Node.js
We’re now ready to write our image resizer!
Let’s navigate to our functions/index.js
file and begin to import our needed dependencies:
Great! Now we can start to write our function handler:
As you can see, we’re telling our Firebase Function that we need at least 2GB of memory and a maximum of 120 seconds of time for our function to be executed. We also declare that we need to trigger this function once an image has been uploaded to our bucket ( storage.object().onFinalize
).
Let’s write the handler
function:
We need to get a bunch of informations from our object
argument (which is actually an object describing where our image has been uploaded):
- Bucket : the bucket where we uploaded the image to.
- File Path : the file path inside our Bucket.
- File Name : the uploaded file name.
- Bucket Directory : the name of the directory where we uploaded our image to.
Now we can create a temporary directory where we’ll execute our resizing code. We’ll also create a temporary file which we’ll run our manipulations on.
Here comes a problem now: our function gets triggered everytime a new file is created inside our Bucket… but we’re actually creating a new resized image, so how can we avoid an infinite resizing-loop?
We’ll set a name like myImage@s_1920.jpg
for our resized image (where s_
stands for “size”), so we’ll be able to check if the newly created image is the product of a resizing or not.
If the image is itself a resized image, we’ll quit our function.
Now we just need to download the newly created image. We’ll download it in the previously created temporary file path:
Now we’re ready to start the resizing! Let’s say we need to create three different sizes: 1920px, 720px, 100px. Let’s wrap these values into an array:
Now we need to run the resizer, so we’ll create a promise for each size inside our array:
As you can see, the process is pretty easy:
sharp
Now we just need to run these three promises… but we don’t want to run them sequentially, it could take too long! So we’ll use Promise.all
in order to run them concurrently:
And we’re done!
Let’s deploy the function and test it uploading a file to Firebase Storage:
We’re now ready to run our resizer!
At JSMonday , we’re actually using the code above for resizing our images:
Feel free to reuse the code above for your image resizer!
Recommend
-
7
Google Cloud Functions Tutorial : Using the Cloud Scheduler to trigger your functionsThis is part of a Google Cloud Functions...
-
12
How to resize a SwiftUI Image and keep its aspect ratio 17 Mar 2021 ⋅ 3 min read ⋅ SwiftUI
-
27
How to resize an image view to fit a container view in SwiftUI 14 Apr 2021 ⋅ 3 min read ⋅ SwiftUI
-
12
How to resize and position an image in UIImageView using contentMode 29 Mar 2021 ⋅ 3 min read ⋅ UIKit
-
9
Upload and resize an image natively with ASP.NET Core 🖼️ Written by Thomas Ardal, June 22, 2021 I have blogged about upload and resizing with ASP.NET Core...
-
4
How to Resize and Crop an Image Using Microsoft Paint By Nolen Jonker Published 1 day ago Microsoft Paint may be incredibly basic...
-
5
XDA Basics: How to resize an image on your Windows PC or MacWe’ve all been there: Trying to upload an image, sometimes as part of a big form, and the website where you need to upload it to has an annoying limit on file size. It can b...
-
5
It is very easy to show resized or cropped version of an image on a website using CSS. However, this doesn't change the original image data. What if you want to create an actual cropped or resized version of image for your visitors or clients...
-
2
Laravel 8 - Image Resize Before Upload 1076 views 6 months ago Laravel I am able to explain one by one article...
-
5
[Golang] Resize Image From Web Updated: March 15, 2017 This post gives an example t...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK