npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

text-to-image-lines

v0.0.3-development

Published

A library for generating an image data URI representing an image containing the text of your choice.

Downloads

4

Readme

Text to image

Code Coverage Npm Version License

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 produces an image data URI (data:image/png;base64,iVBORw0KGgoAAAA...).

Installation

npm i text-to-image

Note that text-to-image uses node-canvas to generate the 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.

Usage

const textToImage = require('text-to-image');

// using the asynchronous API with await
const dataUri = await textToImage.generate('Lorem ipsum dolor sit amet')

// using the asynchronous API with .then
textToImage.generate('Lorem ipsum dolor sit amet').then(function (dataUri) {
  // use the dataUri
});

// using the synchronous API
const dataUri = textToImage.generateSync('Lorem ipsum dolor sit amet')

This is an example of a full dataUri generated by the above examples:

data:image/png;base64,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qa2tRWloKIyMjuLi4dPlstKmpCcXFxejZsyccHByEm9b/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.

With the default options 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.

Configuring

The generate and generateSync functions take an optional second parameter containing configuration options for the image. All configuraion parameters are optional.

Note that the supplied configuration values are not validated. Invalid values may lead to unexpected results or the image not getting generated at all. For color value validations, consider using a library like validate-color before passing the value to this library.

The available options are as follows.

| Name | Type | Default value | Description | | ------------- | ----------------------------------------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | bgColor | string | CanvasGradient | CanvasPattern | #FFFFFF | Sets the background color of the image. See CanvasRenderingContext2D.fillStyle for valid values, or use a color value validator (see note above). | | customHeight | number | 0 | Sets the height of the generated image in pixels. If falsy, will automatically calculate the height based on the amount of text. | | debug | boolean | false | Set to true to turn on debug mode (see below). | | debugFilename | string | timestamp | Set a custom file name for the file created in debug mode. Defaults to a timestamp with a .png extension. | | fontFamily | string | Helvetica | The font family to use for the text in the image. See CSS font-family for valid values. | | fontPath | string | | The file system path to a font file to use, also specify fontFamily if you use this. | | fontSize | number | 18 | The font size to use for the text in the image. See CSS font-size for valid values. | | fontWeight | string | number | normal | The font weight to use for the text in the image. See CSS font-weight for valid values. | | lineHeight | number | 28 | The line height for the generated text. | | margin | number | 10 | The margin (all sides) between the text and the border of the image. | | maxWidth | number | 400 | Sets the width of the generated image in pixels. | | textAlign | string | left | The text alignment for the generated text. See CanvasRenderingContext2D.textAlign for valid values. | | textColor | string | #000000 | Sets the text color. See CanvasRenderingContext2D.fillStyle for valid values, or use a color value validator (see note above). | | verticalAlign | string | top | Use to set center height with customHeight (possible values: top, center). |

Example:

const textToImage = require('text-to-image');

const dataUri = await textToImage.generate('Lorem ipsum dolor sit amet', {
  debug: true,
  maxWidth: 720,
  fontSize: 18,
  fontFamily: 'Arial',
  lineHeight: 30,
  margin: 5,
  bgColor: "blue",
  textColor: "red"
});

Debugging

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.

Test

The library is tested using Jest. Run the test suit by executing

npm test

A coverage report will be generated in coverage/.

Contributing

Pull requests are welcome. Read the Contributing guidelines.

License

ISC License (ISC)