A library for generating an image data URI representing an image containing the text of your choice.
Originally part of a Twitter bot for publishing tweets longer than 140 characters, the generator takes a string and an optional configuration object as parameters and returns a promise that will resolve to a image data URI (data:image/png;base64,iVBORw0KGgoAAAA...
)
npm i text-to-image
Note that text-to-image uses node-canvas to generate images. For text-to-image to install, you might have to fulfill the installation requirements for node-canvas. Please refer to their documentation for instructions.
const textToImage = require('text-to-image');
textToImage.generate('Lorem ipsum dolor sit amet').then(function (dataUri) {
console.log(dataUri);
});
The above script will print the full generated data URI to the console.
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAAAwCAYAAAA/1CyWAAAABmJLR0QA/wD/AP+gvaeTAAARHElEQVR4nO3dZ1BUVxsH8D/sIsUBWXoVWJSi6KKg4kbKgAUHJ3RUojEIohQxmhjRRI3GYBRXBR0sY4k4MZNYEj9Ek4wlmujYEcWSQLCCneYAUpbn/cBwX6/gvrDRoPM+vxlmuA/n3D3nnut97rll1SEiAmOMMdZFut3dAMYYY28nTiCMMca0wgmEMcaYVjiBMMYY0wonEMYYY1rhBMIYY0wrnEAYY4xphRMIY4wxrXACYYwxppUuJZDa2lpUVlaipaXldbWnW9XU1ODp06fd3Yx/RW1tLaqrq7tc7+nTp6ipqXkNLXo728HY/7MuJZC0tDSYmZnhzp07r6s93SogIAAhISHd3Yx/RXJyMgYNGtTlepGRkRg2bNhraFHXjBkzBkqlUli+f/8+zp8/j4aGhm5sVdc8ePAA58+fx7Nnz7q7KYxphS9hPWfYsGEYOnRodzeDdcKQIUPg5+cnLOfn58PX1/etOrn55ptv4Ovri5s3b3Z3UxjTirS7G/Am2bRpU3c3gXVSTk5OdzeBsf97ry2BlJaWQqVS4cKFCwAApVKJOXPmwN7eXiizYcMG9OjRA+Hh4Vi6dCmam5uRl5cHoPUad15eHo4fPw6gdXaQmpoKCwsLoX5OTg4MDAwQERGBFStWoKioCHK5HJmZmbC1tUVubi6OHj0KiUSCmJgYTJkyRWObc3JyIJFIkJ6eDgD4/PPPoVAo4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GRmkp6dHAMjAwIDmzZtHy5Yto4CAAKFcc3MzzZ07lwwNDYU+WFtb09atW0XrmzRpEo0aNUpUb968eaJ6VlZW7d5Pef/99yk4OFhjW9ucOXNGeEei7cfFxYV+++03Ud/HjBkjLD9+/JiGDh1KEomEZsyY8dJ1Hzt2THgvCAANGTKETp48SVlZWaSrq0sxMTFERBQXF0dhYWGiuqdPnya5XE4//PCDKB4fHy96jJeIaMuWLWRpaSl8joWFBalUKuFxaCKiiooK8vPzI4lEQklJSZ3aNoy9SXSItHjOtJNqa2tRWloKIyMjuLi4dPlstKmpCcXFxejZsyccHByEm9b/FlNTU7z77rvIz89HVVUVKisr4ejo+NKbwY8ePUJ5eTlsbW2F+wMvqq+vx7Nnz4SnyP4tVVVVKCsrQ+/evTU+GFBXV4fS0lIYGBhALpd3esy0rafJ48ePUVZWBhsbG9Hjxf9US0sL7ty5A0NDQ9E41dfXQ19f/5XNcNVqNYqLi6Gvr69xv2HsbfVaE8jb7vkEwhhjTIy/jZcxxphWeE6twQcffCD60kTGGGP/xZewGGOMaYUvYTHGGNMKJxDGGGNa4QTCGGNMK5xAGGOMaYUTCGOMMa1wAmGMMaYVTiCMMca0wgmEMcaYVjiBMMYY0wonEMYYY1rhBMIYY0wrnEAYY4xphRMIY4wxrXACYYwxphVOIIwxxrTCCYQxxphWOIEwxhjTCicQxhhjWuEEwhhjTCv/ATE1nc1ocQ7AAAAAAElFTkSuQmCC
Copy that line into the address field of your browser and you should see the generated image.
The image generator will adjust the height of the image automatically to fit all text, while the width of the image is fixed to the specified width.
Line breaks can be forced with \n
.
The generate
function takes an optional second parameter containing configuration options for the image. All configuraion parameters are optional. The available options are as follows.
Name | Default value | Description |
---|---|---|
maxWidth | 400 | Sets the width of the generated image in pixels. |
fontSize | 18 | The font size to use for the text in the image. |
fontFamily | Helvetica | The font family to use for the text in the image. |
fontWeight | normal | The font weight to use for the text in the image. |
lineHeight | 28 | The line height for the generated text. |
margin | 10 | The margin (all sides) between the text and the border of the image. |
debug | false | Set to true to turn on debug mode (see below). |
debugFilename | timestamp | Set a custom file name for the file created in debug mode. Defaults to a timestamp with a .png extension. |
bgColor | #FFFFFF | Sets the background color of the image. |
textColor | #000000 | Sets the text color. |
customHeight | 0 | Sets the height of the generated image in pixels. If falsy, will automatically calculate the height based on the amount of text. |
Example:
const textToImage = require('text-to-image');
textToImage.generate('Lorem ipsum dolor sit amet', {
debug: true,
maxWidth: 720,
fontSize: 18,
fontFamily: 'Arial',
lineHeight: 30,
margin: 5,
bgColor: "blue",
textColor: "red"
}).then(function (dataUri) {
console.log(dataUri);
});
Turn on debug mode to have the library store the generated image data URI as a PNG in the current working directory (where the process was started). The image name will be the current date and time.
The library is tested using Jest. Run the test suit by executing
npm test
A coverage report will be generated in coverage/
.
Pull requests are welcome. Read the Contributing guidelines.