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

tiny-image-uploader

v0.1.1

Published

A lightweight library for image uploading and processing

Downloads

14

Readme

tiny-image-uploader

tiny-image-uploader is a TypeScript library that allows easy selection, cropping, and resizing of images in web applications. It also includes file size limitation features for efficient image management.

Demo

[Add a link to your demo here if available]

Features

  • Select images from local device
  • Resize images with customizable options
  • Crop images with specified dimensions
  • Upload processed images using custom upload functions
  • Restore original image
  • Lightweight and dependency-free core

Installation

npm install image-processor

Usage

Example

import { ImageProcessor, ImageProcessorOptions, ResizeOptions, CropOptions, UploadFunction } from 'image-processor';

// Initialize ImageProcessor
const options: ImageProcessorOptions = {
  maxFileSize: 5 * 1024 * 1024, // 5MB
  maxWidth: 1920,
  maxHeight: 1080
};
const imageProcessor = new ImageProcessor(options);

// Define resize options
const resizeOptions: ResizeOptions = {
  width: 800,
  height: 600,
  maintainAspectRatio: true
};

// Define crop options
const cropOptions: CropOptions = {
  top: 0,
  left: 0,
  width: 400,
  height: 300
};

// Define upload function
const uploadFunction: UploadFunction = async (blob, fileName) => {
  const formData = new FormData();
  formData.append('file', blob, fileName);
  const response = await fetch('https://your-upload-url.com', {
    method: 'POST',
    body: formData
  });
  const data = await response.json();
  return data.url;
};

// Process and upload image
async function processAndUploadImage() {
  try {
    await imageProcessor.selectImage();
    const resizedImageSrc = await imageProcessor.resizeImage(resizeOptions);
    console.log('Resized image:', resizedImageSrc);

    const croppedImageSrc = imageProcessor.cropImage(cropOptions);
    console.log('Cropped image:', croppedImageSrc);

    const uploadedUrl = await imageProcessor.uploadImage(uploadFunction);
    console.log('Uploaded image URL:', uploadedUrl);

    const originalImageSrc = imageProcessor.restoreOriginalImage();
    console.log('Restored original image:', originalImageSrc);
  } catch (error) {
    console.error('Error processing image:', error);
  }
}

processAndUploadImage();

Options

You can pass these options to ImageProcessor constructor:

  • maxFileSize: The maximum file size (in bytes) allowed for image selection.
  • maxWidth: The maximum width allowed for images.
  • maxHeight: The maximum height allowed for images.

Methods and Types

Constructor

Create a new instance of ImageProcessor.

interface ImageProcessorOptions {
  maxFileSize?: number;
  maxWidth?: number;
  maxHeight?: number;
}

const imageProcessor = new ImageProcessor(options: ImageProcessorOptions);

selectImage()

Select an image file from the local device.

await imageProcessor.selectImage(): Promise<void>;

resizeImage(options)

Resize the selected image.

interface ResizeOptions {
  width?: number;
  height?: number;
  maintainAspectRatio?: boolean;
}

const resizedImageSrc: string = await imageProcessor.resizeImage(options: ResizeOptions): Promise<string>;

cropImage(options)

Crop the selected image.

interface CropOptions {
  top: number;
  left: number;
  width: number;
  height: number;
}

const croppedImageSrc: string = imageProcessor.cropImage(options: CropOptions): string;

uploadImage(uploadFunction)

Upload the processed image using a custom upload function.

type UploadFunction = (blob: Blob, fileName: string) => Promise<string>;

const uploadedUrl: string = await imageProcessor.uploadImage(uploadFunction: UploadFunction): Promise<string>;

restoreOriginalImage()

Restore the image to its original state.

const originalImageSrc: string = imageProcessor.restoreOriginalImage(): string;

License

MIT License