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-compress-utility

v1.2.5

Published

Image Compress Utility is a Node.js library for compressing images on the client-side before uploading them to a server. It utilizes the sharp library for high-performance image processing.

Downloads

3

Readme

Image Compress Utility

Image Compress Utility is a Node.js library for compressing images on the client-side before uploading them to a server. It utilizes the sharp library for high-performance image processing.

Installation

You can install the Image Compress Utility from the npm registry using npm or yarn.

Using npm:

npm install image-compress-utility

Using yarn:

bash

yarn add image-compress-utility

Usage

Here's a basic example of how to use the compressImage function:


const { compressImage } = require('image-compress-utility');

async function compressAndUploadImage(file) {
  try {
    const compressedImage = await compressImage(file, { maxWidth: 800, maxHeight: 600, quality: 70 });
    // Upload the compressedImage to your server
  } catch (error) {
    console.error('Error compressing image:', error);
  }
}

// Usage with a file buffer
const fileBuffer = ... // Read file buffer from file input
compressAndUploadImage(fileBuffer);

// Usage with a Readable stream
const fileStream = ... // Read file stream from file input
compressAndUploadImage(fileStream);

In Next JS application


// Import necessary modules
import React, { useState } from 'react';
import { compressImage } from 'image-compress-utility';

// Define your Next.js component
const ImageUpload = () => {
  // State to hold the compressed image data URL
  const [compressedImageSrc, setCompressedImageSrc] = useState(null);

  // Function to handle file upload
  const handleFileUpload = async (event) => {
    const file = event.target.files[0]; // Get the uploaded file

    try {
      // Compress the image using the compressImage function from image-compress-utility
      const compressedImageBuffer = await compressImage(file);
      const compressedImageDataURL = `data:${file.type};base64,${compressedImageBuffer.toString('base64')}`;
      
      // Set the compressed image data URL to state
      setCompressedImageSrc(compressedImageDataURL);
      
      // Now you can upload the compressed image data URL to your server or use it as needed
    } catch (error) {
      console.error('Error compressing image:', error);
    }
  };

  return (
    <div>
      {/* Input field to upload an image */}
      <input type="file" onChange={handleFileUpload} />
      
      {/* Display the compressed image */}
      {compressedImageSrc && <img src={compressedImageSrc} alt="Compressed Image" />}
    </div>
  );
};

export default ImageUpload;

The compressImage function accepts a file buffer or a Readable stream as input and returns a Promise that resolves to the compressed image buffer. Options

You can customize the compression settings by passing options to the compressImage function:


    maxWidth: Maximum width of the compressed image (default: 1024)
    maxHeight: Maximum height of the compressed image (default: 1024)
    quality: Compression quality (default: 80)
    format: Output format options (default: { id: 'jpeg' })
    progressive: Enable progressive JPEGs (default: false)

Link to npm package: Image Compress Utility