diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | content/2020-03-25-create-placeholder-images-with-sharp.md | 9 | ||||
| -rw-r--r-- | content/2020-03-27-create-placeholder-images-with-sharp.md | 83 | ||||
| -rw-r--r-- | static/style.css | 4 |
4 files changed, 88 insertions, 10 deletions
| @@ -3,7 +3,7 @@ MAKEFLAGS += -j2 | |||
| 3 | dev: server watch | 3 | dev: server watch |
| 4 | 4 | ||
| 5 | watch: | 5 | watch: |
| 6 | find content/*.md | entr make generate | 6 | find content/* templates/* static/* | entr make generate |
| 7 | 7 | ||
| 8 | server: | 8 | server: |
| 9 | browser-sync start --server ./public --watch --no-open --no-notify | 9 | browser-sync start --server ./public --watch --no-open --no-notify |
diff --git a/content/2020-03-25-create-placeholder-images-with-sharp.md b/content/2020-03-25-create-placeholder-images-with-sharp.md deleted file mode 100644 index 6d35d82..0000000 --- a/content/2020-03-25-create-placeholder-images-with-sharp.md +++ /dev/null | |||
| @@ -1,9 +0,0 @@ | |||
| 1 | ~ title: Create placeholder images with sharp Node.js image processing library | ||
| 2 | ~ description: Create placeholder images with sharp Node.js image processing library | ||
| 3 | ~ slug: /create-placeholder-images-with-sharp.html | ||
| 4 | ~ date: 2020-03-25 | ||
| 5 | ~ template: post | ||
| 6 | ~ hide: true | ||
| 7 | |||
| 8 | ## Todo | ||
| 9 | |||
diff --git a/content/2020-03-27-create-placeholder-images-with-sharp.md b/content/2020-03-27-create-placeholder-images-with-sharp.md new file mode 100644 index 0000000..1386b2f --- /dev/null +++ b/content/2020-03-27-create-placeholder-images-with-sharp.md | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | ~ title: Create placeholder images with sharp Node.js image processing library | ||
| 2 | ~ description: Create placeholder images with sharp Node.js image processing library | ||
| 3 | ~ slug: /create-placeholder-images-with-sharp.html | ||
| 4 | ~ date: 2020-03-27 | ||
| 5 | ~ template: post | ||
| 6 | ~ hide: false | ||
| 7 | |||
| 8 | I have been searching for a solution to pre-generate some placeholder images for image server I needed to develop that resizes images on S3. I though this would be a 15min job and quickly found out how very mistaken I was. | ||
| 9 | |||
| 10 | Even though Node.js is not really the best way to do this kind of things (surely something written in C or Rust or even Golang would be the correct way to do this but we didn't need the speed in our case) I found an excellent library [sharp - High performance Node.js image processing](https://github.com/lovell/sharp). | ||
| 11 | |||
| 12 | Getting things running was a breeze. | ||
| 13 | |||
| 14 | ## Fetch image from S3 and save resized | ||
| 15 | |||
| 16 | ```js | ||
| 17 | const sharp = require('sharp'); | ||
| 18 | const aws = require('aws-sdk'); | ||
| 19 | |||
| 20 | const x,y = 100; | ||
| 21 | const s3 = new aws.S3({}); | ||
| 22 | |||
| 23 | aws.config.update({ | ||
| 24 | secretAccessKey: 'secretAccessKey', | ||
| 25 | accessKeyId: 'accessKeyId', | ||
| 26 | region: 'region' | ||
| 27 | }); | ||
| 28 | |||
| 29 | const originalImage = await s3.getObject({ | ||
| 30 | Bucket: 'some-bucket-name', | ||
| 31 | Key: 'image.jpg', | ||
| 32 | }).promise(); | ||
| 33 | |||
| 34 | const resizedImage = await sharp(originalImage.Body) | ||
| 35 | .resize(100, 100) | ||
| 36 | .jpeg({ progressive: true }) | ||
| 37 | .toBuffer(); | ||
| 38 | |||
| 39 | s3.putObject({ | ||
| 40 | Bucket: 'some-bucket-name', | ||
| 41 | Key: `optimized/${x}x${y}/image.jpg`, | ||
| 42 | Body: resizedImage, | ||
| 43 | ContentType: 'image/jpeg', | ||
| 44 | ACL: 'public-read' | ||
| 45 | }).promise(); | ||
| 46 | ``` | ||
| 47 | |||
| 48 | All this code was wrapped inside a web service with some additional security checks and defensive coding to detect if key is missing on S3. | ||
| 49 | |||
| 50 | And at that point I needed to return placeholder images as a response in case key is missing or x,y are not allowed by the server etc. I could have created PNG in Gimp and just serve them but I wanted to respect aspect ratio and I didn't want to return some mangled images. | ||
| 51 | |||
| 52 | **ⓘ** Main problem with finding a clean solution I could copy and paste and change a bit was a task. API is changing constantly and there weren't clear examples or I was unable to find them. | ||
| 53 | |||
| 54 | ## Generating placeholder images using SVG | ||
| 55 | |||
| 56 | What I ended up was using SVG to generate text and created image with sharp and used composition to combine both layers. Response returned by this function is a buffer you can use to either upload to S3 or save to local file. | ||
| 57 | |||
| 58 | ```js | ||
| 59 | const generatePlaceholderImageWithText = async (width, height, message) => { | ||
| 60 | const overlay = `<svg width="${width - 20}" height="${height - 20}"> | ||
| 61 | <text x="50%" y="50%" font-family="sans-serif" font-size="16" text-anchor="middle">${message}</text> | ||
| 62 | </svg>`; | ||
| 63 | |||
| 64 | return await sharp({ | ||
| 65 | create: { | ||
| 66 | width: width, | ||
| 67 | height: height, | ||
| 68 | channels: 4, | ||
| 69 | background: { r: 230, g: 230, b: 230, alpha: 1 } | ||
| 70 | } | ||
| 71 | }) | ||
| 72 | .composite([{ | ||
| 73 | input: Buffer.from(overlay), | ||
| 74 | gravity: 'center', | ||
| 75 | }]) | ||
| 76 | .jpeg() | ||
| 77 | .toBuffer(); | ||
| 78 | } | ||
| 79 | ``` | ||
| 80 | |||
| 81 | That is about it. Nothing more to it. You can change the color of the image by changing `background` and if you want to change text styling you can adapt SVG to your needs. | ||
| 82 | |||
| 83 | **ⓘ** Also be careful about the length of the text. This function positions text at the center and adds `20px` padding on all sides. If text is longer than the image it will get cut. | ||
diff --git a/static/style.css b/static/style.css index 5597f30..6c6d5e5 100644 --- a/static/style.css +++ b/static/style.css | |||
| @@ -23,6 +23,10 @@ nav a { | |||
| 23 | margin-right: 20px; | 23 | margin-right: 20px; |
| 24 | } | 24 | } |
| 25 | 25 | ||
| 26 | header { | ||
| 27 | margin-block-end: 40px; | ||
| 28 | } | ||
| 29 | |||
| 26 | h1 { | 30 | h1 { |
| 27 | font-size: 280%; | 31 | font-size: 280%; |
| 28 | line-height: initial; | 32 | line-height: initial; |
