I have a function called dynamic_img.js with this code:
const fs = require('fs');
const canvas = require('canvas')
exports.handler = async (event, context) => {
const { createCanvas, loadImage } = require('canvas')
const canvas = createCanvas(200, 200)
const ctx = canvas.getContext('2d')
// Write "Awesome!"
ctx.font = '40px Impact'
ctx.rotate(0.1)
ctx.fillText('Awesome!', 50, 100)
// Draw line under text
var text = ctx.measureText('Awesome!')
ctx.strokeStyle = 'rgba(0,0,0,0.5)'
ctx.beginPath()
ctx.lineTo(50, 102)
ctx.lineTo(50 + text.width, 102)
ctx.stroke()
// Draw cat with lime helmet
const url = "https://www.twnel.com/wp-content/uploads/2022/12/2022-12-07_12h11_44.png"
await loadImage(url).then((image) => {
ctx.drawImage(image, 50, 0, 70, 70)
})
console.log('<img src="' + canvas.toDataURL() + '" />')
// canvas.createPNGStream().pipe(__dirname + '/test.png')
return {
statusCode: 200,
body: canvas.toDataURL()
}
}
I have not deployed this yet. I’m still working on my local machine.
This code returns this on this path: localhost:8888/api/dynamic_img/
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAABmJLR0QA/wD/AP+gvaeTAAAgAElEQVR4nOy9WaylW37Y9Vtrfeub97zPPqfOqbq37ty3Z6fdJm6nQyCQATsBWXSEiMgTEjwgHiyRgEBCFkiRIggBg4wSCQn8gBQLBRKiyIQwxNjx0O12D7fvWLduTfvMe/7mNfCwSxGvJtW6nMv5vVQ97rPX+Z21/mv9B7jllltuueWWW2655ZZbbrnlD4r4tD/ALZ9Ngk/7A7wIvvXPfO1fAP67IFDzyezOfHb3/vxs/vis3q7nw/Hh3MH84ux8PnktOvulX/ ...
I want o be able to return an image at a custom path, such as localhost:8888/api/dynamic_img/test.png
I think I need to use canvas.createPNGStream().pipe(__dirname + '/test.png')
, but I don’t know where to put it.
Any suggestions?
On the other hand, when I do a small change and I save my work it doesn’t work anymore.
I get this error and I need to run npm run netlify
again
Error: Module did not self-register: ‘\?\C:\Users\Gilbert\netify\serverless-functions-starter\node_modules\canvas\build\Release\canvas.node’.
Object.Module._extensions…node (node:internal/modules/cjs/loader:1183:18)
Module.load (node:internal/modules/cjs/loader:981:32)
Function.Module._load (node:internal/modules/cjs/loader:822:12)
Module.require (node:internal/modules/cjs/loader:1005:19)
require (node:internal/modules/cjs/helpers:102:18)
Object. (C:\Users\Gilbert\netify\serverless-functions-starter\node_modules\canvas\lib\bindings.js:3:18)
Module._compile (node:internal/modules/cjs/loader:1101:14)
Object.Module._extensions…js (node:internal/modules/cjs/loader:1153:10)
Module.load (node:internal/modules/cjs/loader:981:32)
Function.Module._load (node:internal/modules/cjs/loader:822:12)
I tried npm install -g netlify-cli
as suggested somewhere, but it didn’t solve the issue