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

sdx-resize-image

v1.0.0

Published

A lightweight and efficient module for resizing images in TypeScript using the Canvas browser API.

Downloads

6

Readme

TypeScript Image Resizer

✨🖼️ TypeScript Image Resizer: Zero-dependency module for resizing images using DOM File object. Returns base64 encoded string in Rust-like result object. Utilizes Canvas browser API. Effortless image magic! 🌟🎉


This powerful module, written entirely in TypeScript, empowers you to seamlessly resize images using the DOM File object as input. With its sleek implementation, it harnesses the mighty Canvas browser API to perform image resizing magic behind the scenes. 🌟

🎁 Experience the simplicity and elegance as this module gracefully transforms your images, leaving you with a base64 encoded string wrapped in a Rust-like result object. It's all about efficiency and ease of use, without the hassle of additional dependencies. 📦

Whether you're building a web application, mobile app, or a creative project, this lightweight and versatile tool will be your go-to companion. 🎨💪

Get started today and unlock the full potential of image resizing in TypeScript! Let your creativity flow effortlessly with the TypeScript Image Resizer. 🖌️💡

Installation

To use the TypeScript Image Resizer in your project, simply install it via npm:

npm install sdx-resize-image

Usage

The TypeScript Image Resizer provides several functions for image resizing:

export function resizeImg(
    img: File,
    opt: Options
): Promise<Result<string, string>> { /* ... */ }
export function resizeImage(
    img: File,
    width: number,
    height: number,
    quality?: number,
    type?: DataType
): Promise<Result<string, string>> { /* ... */ }
export function resizeImageWidth(
    img: File,
    width: number,
    quality?: number,
    type?: DataType
): Promise<Result<string, string>> { /* ... */ }
export function resizeImageHeight(
    img: File,
    height: number,
    quality?: number,
    type?: DataType
): Promise<Result<string, string>> { /* ... */ }

You can decide if you prefer the concrete functions for image resizing or the flexible approach using an options object to control the outcome. Invalid usage results in an error result describing the wrong usage.
Refer to the API documentation for detailed information on each function and how to use them.

API

resizeImg(img: File, opt: Options): Promise<Result<string, string>>

A function that allows you to resize an image file with flexible options.

Parameters

  • img (required): The image file to be resized.
  • opt (required): An object containing options for image resizing. The options include:
    • type (optional): The desired data type of the resized image. Supported values are 'image/png', 'image/jpeg', and ' image/webp'. If not provided, the default data type is 'image/png'.
    • width (optional): The desired width of the resized image in pixels.
    • height (optional): The desired height of the resized image in pixels.
    • quality (optional): The desired quality of the resized image. This value should be a number between 0 and 1, where 1 represents the highest quality. If not provided, the default quality is used.

At least width or height must be set in the options. If none is present, the validation fails and returns an error result.

Return Value

A promise that resolves to a Result object. The Result object represents the outcome of the image resizing operation and contains either the resized image as a base64-encoded string (ok property) or an error message (err property).

Example

import {resizeImg} from 'sdx-resize-image';

const opt = {
    width: 800,
    height: 600,
    quality: 0.8,
    type: 'image/webp'
}
const resizedImageResult = await resizeImg(img, opt);
if (resizedImageResult.ok) {
    const resizedImageBase64 = resizedImageResult.value;
    // Process the resized image
} else {
    const error = resizedImageResult.error;
    // Handle the error
}

resizeImage(img: File, width: number, height: number, quality?: number, type?: DataType): Promise<Result<string, string>>

Resizes the image specified by the img file object to the desired width and height dimensions. You can also optionally specify the quality and type of the resized image. The function returns a promise that resolves to a result object containing the base64 encoded string of the resized image on success, or an error message on failure.

Example

import {resizeImage} from 'sdx-resize-image';

const resizedImageResult = await resizeImage(img, 800, 600, 0.8);
if (resizedImageResult.ok) {
    const resizedImageBase64 = resizedImageResult.value;
    // Process the resized image
} else {
    const error = resizedImageResult.error;
    // Handle the error
}

resizeImageWidth(img: File, width: number, quality?: number, type?: DataType): Promise<Result<string, string>>

Resizes the image specified by the img file object to the desired width while maintaining the aspect ratio. You can also optionally specify the quality and type of the resized image. The function returns a promise that resolves to a result object containing the base64 encoded string of the resized image on success, or an error message on failure.

Example

import {resizeImageWidth} from 'sdx-resize-image';

const resizedImageResult = await resizeImageWidth(img, 800, 0.8);
if (resizedImageResult.ok) {
    const resizedImageBase64 = resizedImageResult.value;
    // Process the resized image
} else {
    const error = resizedImageResult.error;
    // Handle the error
}

resizeImageHeight(img: File, height: number, quality?: number, type?: DataType): Promise<Result<string, string>>

Resizes the image specified by the img file object to the desired height while maintaining the aspect ratio. You can also optionally specify the quality and type of the resized image. The function returns a promise that resolves to a result object containing the base64 encoded string of the resized image on success, or an error message on failure.

Example

import {resizeImageHeight} from 'sdx-resize-image';

const resizedImageResult = await resizeImageHeight(img, 600, 0.8);
if (resizedImageResult.ok) {
    const resizedImageBase64 = resizedImageResult.value;
    // Process the resized image
} else {
    const error = resizedImageResult.error;
    // Handle the error
}

License

This project is licensed under the MIT License.


Enjoy the power and simplicity of image resizing with the TypeScript Image Resizer! If you encounter any issues or have any suggestions, feel free to open an issue on GitHub. Contributions are also welcome! 🎉🤝