That's a really good question.
There are libraries to make images out of code, surely that's how they do it. Maybe using SSR they create the meta tags for the image and check if the image exists and if not, they create it.
The only one that comes from the top of my head is p5.js, it's for drawing. With that you could create the image but I'm not sure if you could export it to a PNG.
Yes P5.js has a method called save that takes the argument to save to jpg, png etc. However I have my own doubts on them using p5.js here though, Sandeep Panda?