diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2023-06-27 14:50:20 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2023-06-27 14:50:20 +0200 |
| commit | 8697555125c57ae64a0c9b78514b4aac4fd523de (patch) | |
| tree | a699df53a7c35a4425f30bca86982c4341f6de40 /content/posts/2020-03-27-create-placeholder-images-with-sharp.md | |
| parent | 33b2615a5038bc85036081e8b5e0da8584d88097 (diff) | |
| download | mitjafelicijan.com-8697555125c57ae64a0c9b78514b4aac4fd523de.tar.gz | |
Massive formatting and added figcaption
Diffstat (limited to 'content/posts/2020-03-27-create-placeholder-images-with-sharp.md')
| -rw-r--r-- | content/posts/2020-03-27-create-placeholder-images-with-sharp.md | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/content/posts/2020-03-27-create-placeholder-images-with-sharp.md b/content/posts/2020-03-27-create-placeholder-images-with-sharp.md index f7dd362..ac4f053 100644 --- a/content/posts/2020-03-27-create-placeholder-images-with-sharp.md +++ b/content/posts/2020-03-27-create-placeholder-images-with-sharp.md | |||
| @@ -5,14 +5,15 @@ date: 2020-03-27T12:00:00+02:00 | |||
| 5 | draft: false | 5 | draft: false |
| 6 | --- | 6 | --- |
| 7 | 7 | ||
| 8 | I have been searching for a solution to pre-generate some placeholder images | 8 | I have been searching for a solution to pre-generate some placeholder images for |
| 9 | for image server I needed to develop that resizes images on S3. I though this | 9 | image server I needed to develop that resizes images on S3. I though this would |
| 10 | would be a 15min job and quickly found out how very mistaken I was. | 10 | be a 15min job and quickly found out how very mistaken I was. |
| 11 | 11 | ||
| 12 | Even though Node.js is not really the best way to do this kind of things | 12 | Even though Node.js is not really the best way to do this kind of things (surely |
| 13 | (surely something written in C or Rust or even Golang would be the correct way | 13 | something written in C or Rust or even Golang would be the correct way to do |
| 14 | to do this but we didn't need the speed in our case) I found an excellent | 14 | this but we didn't need the speed in our case) I found an excellent library |
| 15 | library [sharp - High performance Node.js image processing](https://github.com/lovell/sharp). | 15 | [sharp - High performance Node.js image |
| 16 | processing](https://github.com/lovell/sharp). | ||
| 16 | 17 | ||
| 17 | Getting things running was a breeze. | 18 | Getting things running was a breeze. |
| 18 | 19 | ||
| @@ -53,20 +54,20 @@ s3.putObject({ | |||
| 53 | All this code was wrapped inside a web service with some additional security | 54 | All this code was wrapped inside a web service with some additional security |
| 54 | checks and defensive coding to detect if key is missing on S3. | 55 | checks and defensive coding to detect if key is missing on S3. |
| 55 | 56 | ||
| 56 | And at that point I needed to return placeholder images as a response in case | 57 | And at that point I needed to return placeholder images as a response in case |
| 57 | key is missing or x,y are not allowed by the server etc. I could have created | 58 | key is missing or x,y are not allowed by the server etc. I could have created |
| 58 | PNG in Gimp and just serve them but I wanted to respect aspect ratio and I | 59 | PNG in Gimp and just serve them but I wanted to respect aspect ratio and I |
| 59 | didn't want to return some mangled images. | 60 | didn't want to return some mangled images. |
| 60 | 61 | ||
| 61 | > Main problem with finding a clean solution I could copy and paste and change | 62 | > Main problem with finding a clean solution I could copy and paste and change a |
| 62 | a bit was a task. API is changing constantly and there weren't clear examples | 63 | > bit was a task. API is changing constantly and there weren't clear examples or |
| 63 | or I was unable to find them. | 64 | > I was unable to find them. |
| 64 | 65 | ||
| 65 | ## Generating placeholder images using SVG | 66 | ## Generating placeholder images using SVG |
| 66 | 67 | ||
| 67 | What I ended up was using SVG to generate text and created image with sharp and | 68 | What I ended up was using SVG to generate text and created image with sharp and |
| 68 | used composition to combine both layers. Response returned by this function is | 69 | used composition to combine both layers. Response returned by this function is a |
| 69 | a buffer you can use to either upload to S3 or save to local file. | 70 | buffer you can use to either upload to S3 or save to local file. |
| 70 | 71 | ||
| 71 | ```js | 72 | ```js |
| 72 | const generatePlaceholderImageWithText = async (width, height, message) => { | 73 | const generatePlaceholderImageWithText = async (width, height, message) => { |
| @@ -91,11 +92,10 @@ const generatePlaceholderImageWithText = async (width, height, message) => { | |||
| 91 | } | 92 | } |
| 92 | ``` | 93 | ``` |
| 93 | 94 | ||
| 94 | That is about it. Nothing more to it. You can change the color of the image by | 95 | That is about it. Nothing more to it. You can change the color of the image by |
| 95 | changing `background` and if you want to change text styling you can adapt | 96 | changing `background` and if you want to change text styling you can adapt SVG |
| 96 | SVG to your needs. | 97 | to your needs. |
| 97 | |||
| 98 | > Also be careful about the length of the text. This function positions text | ||
| 99 | > at the center and adds `20px` padding on all sides. If text is longer than | ||
| 100 | > the image it will get cut. | ||
| 101 | 98 | ||
| 99 | > Also be careful about the length of the text. This function positions text at | ||
| 100 | > the center and adds `20px` padding on all sides. If text is longer than the | ||
| 101 | > image it will get cut. | ||
