0

So I'm just curious if this capability exists, though it may well not exist. I'm trying to take a string of text like 1 or 2323 and convert it into an image that has the text string as background. Nothing crazy about the image, just a placeholder image from a text string. Does this sort of capability exist? So far, I've only been able to find base64 generators for images that I already had, not images I wanted to generate on the fly.

(Bonus points for a solution in Javascript)

Dylan Gattey
  • 1,713
  • 13
  • 34

2 Answers2

1

You can do this pretty simply without additional libraries if your browser supports HTML5 canvas.

function getTextImage(text) {
  var c = document.getElementById("canvas");
  var ctx=c.getContext("2d");    
  ctx.font="20px Georgia";
  ctx.fillText(text,10,50);
  var img = canvas.toDataURL("image/png");
  return img;
}
    
var yourText = "I'm an image";
document.write('<img src="' + getTextImage(yourText) + '"/>');
<canvas id="canvas" style="display:none;">
</canvas>
Dave
  • 10,748
  • 3
  • 43
  • 54
0

Actually, I figured it out by combining frameworks. Placehold.it has the ability to generate a sized image with custom text, and that combined with the trick outlined in this StackOverflow post does what I need it to in Javascript.

Community
  • 1
  • 1
Dylan Gattey
  • 1,713
  • 13
  • 34