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 🙏

© 2025 – Pkg Stats / Ryan Hefner

cca-image-resize-convert-module

v0.0.6

Published

convert and resize image

Readme

Image Processing Utility

A powerful TypeScript/JavaScript utility for processing, resizing, and optimizing images with built-in support for multiple formats and intelligent optimization presets.

Features

  • Resize images to multiple predefined sizes in a single operation
  • Support for JPEG, PNG, GIF, WebP, and SVG formats
  • Preserve animations in GIF files
  • Special handling for vector formats (SVG)
  • Intelligent WebP optimization with format-specific presets
  • Consistent API for image processing operations

Installation

npm install cca-image-resize-convert-module

Or using yarn:

yarn add cca-image-resize-convert-module

Usage

Resizing an Image to All Defined Sizes

import { resizeImageAllSizes } from 'cca-image-resize-convert-module';
import express from 'express';
import multer from 'multer';

const upload = multer({ storage: multer.memoryStorage() });

app.post('/upload', upload.single('image'), async (req, res) => {
  try {
    // Resize the uploaded image to all predefined sizes
    const resizedImages = await resizeImageAllSizes(req.file);
      
    // Process further or save to storage...
    
    res.status(200).json({ success: true });
  } catch (error) {
    res.status(400).json({ error: error.message });
  }
});

Using the ImageProcessor Service

import { imageProcessor, ImageSize } from 'image-processing-utility';
import fs from 'fs/promises';

async function processMyImage() {
  // Read an image file
  const buffer = await fs.readFile('path/to/image.jpg');
  const mimeType = 'image/jpeg';
  
  // Validate the image type
  if (!imageProcessor.validateImageType(mimeType)) {
    throw new Error('Unsupported image type');
  }
  
  // Get image metadata
  const metadata = await imageProcessor.analyzeImage(buffer);
  console.log(`Image dimensions: ${metadata.width}x${metadata.height}`);
  
  // Resize to medium size
  const resizedBuffer = await imageProcessor.resizeImage(
    buffer,
    ImageSize.MD,
    mimeType
  );
  
  // Get the content type for the output format
  const contentType = imageProcessor.getContentType(mimeType);
  
  // Save or use the resized image...
  await fs.writeFile('path/to/output.webp', resizedBuffer);
}

API Reference

resizeImageAllSizes(file: Express.Multer.File): Promise<Record<ImageSize, Buffer>>

Resizes an image to all predefined sizes and returns a record with size keys mapping to image buffers.

Parameters:

  • file: An Express.Multer.File object containing the image file buffer and metadata

Returns:

  • A Promise that resolves to a Record with ImageSize keys and Buffer values

Throws:

  • Error if the image type is not supported

imageProcessor

A singleton instance of the ImageProcessor class that provides methods for processing images.

Methods

validateImageType(mimeType: string): boolean

  • Checks if the provided MIME type is supported
  • Returns true if supported, false otherwise

getOutputFormat(mimeType: string): string

  • Determines the output format based on input MIME type
  • Returns the format string (e.g., 'webp', 'jpeg', 'png')

getContentType(mimeType: string): string

  • Gets the content type for the output format
  • Returns a content type string (e.g., 'image/webp')

analyzeImage(buffer: Buffer): Promise<any>

  • Analyzes an image and returns its metadata
  • Returns a Promise that resolves to image metadata

resizeImage(buffer: Buffer, size: ImageSize, mimeType: string): Promise<Buffer>

  • Resizes an image to the specified size
  • Parameters:
    • buffer: The input image buffer
    • size: The target size (from ImageSize enum)
    • mimeType: The MIME type of the input image
  • Returns a Promise that resolves to a buffer containing the resized image

Predefined Sizes

The library includes the following predefined sizes:

| Size Name | Dimensions | |-----------|------------| | ORIGINAL | Original dimensions | | THUMB | 100 × 100 | | SM | 300 × 300 | | MD | 600 × 600 | | LG | 900 × 900 | | XL | 1200 × 1200 |

Images are resized to fit within these dimensions while maintaining aspect ratio.

Supported Formats

  • JPEG (image/jpeg)
  • PNG (image/png)
  • GIF (image/gif) - Animations are preserved
  • WebP (image/webp)
  • SVG (image/svg+xml) - Vector format is preserved

WebP Optimization

The library uses intelligent WebP optimization with different presets for:

  • Photos: Balanced quality for photographic content
  • Graphics: Higher quality for graphic designs
  • Icons: Near-lossless mode for icon preservation
  • Text: Lossless mode for text clarity

License

MIT