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

image-text-utils

v0.1.1

Published

A utility package to write text at multiple positions and resize images.

Downloads

9

Readme

image-text-utils

image-text-utils is a Node.js package for adding text to images at single or multiple positions and resizing images. It is built with TypeScript for compatibility with both JavaScript and TypeScript. This package uses the Jimp library for image processing, enabling powerful manipulation capabilities.

Features

  • Write text at a single position on an image
  • Write text at multiple positions on an image
  • Resize images to specified dimensions
  • Compatible with JavaScript (CommonJS) and TypeScript (ES Module)

Installation

To install the package via npm, use:

npm install image-text-utils

Note: This package uses Jimp as its underlying image processing library. Ensure Jimp is compatible with your Node.js version.

Usage

The package provides two main utilities:

  1. writeText: An object with single and multiple methods for writing text at single or multiple positions.
  2. resizeImage: A function to resize images to specified dimensions.

Importing the Package

You can use the package with either require or import syntax.

CommonJS (JavaScript)

const { writeText, resizeImage } = require('image-text-utils');

ES Module (TypeScript)

import { writeText, resizeImage } from 'image-text-utils';

API Reference

writeText

The writeText object provides two methods:

  • writeText.single – Write text at a single position on an image.
  • writeText.multiple – Write text at multiple positions on an image.

Both methods require an input image path, an output image path, and specific text options.

writeText.single(imagePath: string, outputPath: string, textOptions: TextOptions): Promise<void>

Adds a single text at a specified position.

  • imagePath (string): Path to the input image file.
  • outputPath (string): Path to save the output image.
  • textOptions (object):
    • text (string): The text to add.
    • position (object): { x: number, y: number } - Position of the text on the image.
    • fontPath (optional, string): Path to a custom font. If not provided, Jimp's default font (FONT_SANS_32_BLACK) will be used.
Example
writeText.single('input.jpg', 'output.jpg', {
  text: 'Hello, World!',
  position: { x: 50, y: 50 }
});

writeText.multiple(imagePath: string, outputPath: string, textOptionsArray: TextOptions[]): Promise<void>

Adds multiple texts at specified positions on the image.

  • imagePath (string): Path to the input image file.
  • outputPath (string): Path to save the output image.
  • textOptionsArray (array of objects): Each object contains:
    • text (string): The text to add.
    • position (object): { x: number, y: number } - Position of the text on the image.
    • fontPath (optional, string): Path to a custom font. If not provided, Jimp's default font (FONT_SANS_32_BLACK) will be used.
Example
writeText.multiple('input.jpg', 'output.jpg', [
  { text: 'Top Left', position: { x: 10, y: 10 } },
  { text: 'Bottom Right', position: { x: 200, y: 200 } }
]);

resizeImage

Resizes an image to specified dimensions.

resizeImage(imagePath: string, outputPath: string, resizeOptions: ResizeOptions): Promise<void>

  • imagePath (string): Path to the input image file.
  • outputPath (string): Path to save the resized image.
  • resizeOptions (object):
    • width (number): New width of the image.
    • height (number): New height of the image.
Example
resizeImage('input.jpg', 'resized.jpg', { width: 300, height: 300 });

Types

The package provides the following TypeScript types:

  • Position: Defines the x and y coordinates for text placement.

    interface Position {
      x: number;
      y: number;
    }
  • TextOptions: Defines options for the text to be written.

    interface TextOptions {
      text: string;
      position: Position;
      fontPath?: string; // Optional custom font path
    }
  • ResizeOptions: Defines options for resizing an image.

    interface ResizeOptions {
      width: number;
      height: number;
    }

Example

Here's a complete example demonstrating how to use writeText and resizeImage in a Node.js application:

import { writeText, resizeImage } from 'image-text-utils';

// Single text on image
writeText.single('input.jpg', 'output.jpg', {
  text: 'Hello, World!',
  position: { x: 50, y: 50 }
});

// Multiple texts on image
writeText.multiple('input.jpg', 'output_with_multiple_texts.jpg', [
  { text: 'Top Left', position: { x: 10, y: 10 } },
  { text: 'Bottom Right', position: { x: 200, y: 200 } }
]);

// Resize image
resizeImage('input.jpg', 'resized_image.jpg', { width: 400, height: 400 });

Notes

  • This package uses the Jimp library for image processing. Jimp’s default fonts and methods are used for text placement and image resizing.
  • Make sure the image paths provided have read/write permissions as required by Jimp.

License

This project is licensed under the MIT License - see the LICENSE file for details.