Fireship · @fireship

Image Resize Cloud Function

Uploaded August 9, 2018 · Length 8:42

Image Resize Cloud Function

Summary

Build a cloud function with Firebase that resizes an image into multiple thumbnails after upload, optimizing performance and avoiding infinite loops.

  • firebase cloud functions
  • image resizing
  • thumbnail generation
  • cloud storage
  • node js
  • typescript
  • sharp library
  • google cloud storage

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    [Music]

  2. 0:06

    today we're going to build a cloud

  3. 0:08

    function that's capable of resizing an

  4. 0:10

    image into multiple thumbnails after its

  5. 0:13

    initially uploaded this is one of the

  6. 0:14

    most commonly needed features for a

  7. 0:16

    cloud storage bucket but doing it

  8. 0:18

    requires some pretty advanced no js'

  9. 0:20

    code in today's video we'll build this

  10. 0:22

    cloud function from scratch and do

  11. 0:23

    everything we can to optimize

  12. 0:25

    performance for the fastest possible

  13. 0:26

    execution if you're new the channel like

  14. 0:29

    and subscribe and if you want to get

  15. 0:30

    really good at cloud functions consider

  16. 0:32

    enrolling in my cloud functions master

  17. 0:34

    course which was just released yesterday

  18. 0:36

    the of course is free if you're already

  19. 0:38

    a pro member but if not I'll go ahead

  20. 0:40

    and leave a discount code in the video

  21. 0:41

    description as well to get started I am

  22. 0:44

    assuming that you have the firebase

  23. 0:45

    command line tools installed then we can

  24. 0:47

    run firebase and net functions in any

  25. 0:49

    existing project I'm going to select

  26. 0:52

    typescript here but you could also

  27. 0:53

    select JavaScript but I would recommend

  28. 0:55

    that you use the new node js8 runtime

  29. 0:57

    because we'll be using a lot of

  30. 0:59

    async/await syntax here and that's not

  31. 1:02

    available in a default node 6 runtime

  32. 1:04

    once that's done we can see the end of

  33. 1:06

    the functions directory and then we have

  34. 1:08

    a couple of dependencies to install here

  35. 1:10

    the cloud functions environment by

  36. 1:12

    default doesn't have tools set up for

  37. 1:15

    you to download files from a storage

  38. 1:17

    bucket but we can install the Google

  39. 1:19

    Cloud Storage nodejs client to take care

  40. 1:21

    of that for us when a storage cloud

  41. 1:24

    function is triggered it doesn't give

  42. 1:26

    you access directly to the file but

  43. 1:28

    instead it just gives you some

  44. 1:29

    information about the file that was

  45. 1:30

    uploaded so we'll need the Google Cloud

  46. 1:32

    Storage library to actually download it

  47. 1:35

    and resize the image and then re-upload

  48. 1:37

    those back to the bucket we're also

  49. 1:40

    going to install a library called sharp

  50. 1:42

    because it's four to five times faster

  51. 1:44

    than the image magic library that's

  52. 1:45

    installed in the cloud functions

  53. 1:47

    environment and if you've ever used

  54. 1:49

    image magic in a cloud function before

  55. 1:51

    you'll know that it's pretty cumbersome

  56. 1:52

    sharp is just way more

  57. 1:53

    developer-friendly it's promise based

  58. 1:55

    and we'll be able to do all of our

  59. 1:57

    resizing with a single line of code

  60. 1:59

    the last dependency I'll install is node