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

cached-image-optimizer

v1.0.3

Published

A React library for optimizing images with IndexedDB caching

Downloads

253

Readme


Image Compression and Caching Library

This library provides an easy way to compress images and cache them in IndexedDB, with options to control compression settings. Once an image is cached, future loads retrieve it directly from IndexedDB, improving load times.

Installation

Install the library with:

npm install cached-image-optimizer

Usage

Import and Configure the Component

Use the CompressCacheImage component to add optimized images to your project.

Basic Example

The image and name props are required for this component. If you omit either of these, the library will not function as expected. The name prop is used as a unique key to store and retrieve the image from IndexedDB. Without it, the component will not be able to properly cache and load images from IndexedDB.

import React from 'react';
import CompressCacheImage from 'cached-image-optimizer';
import SampleImage from '../assets/sample-image.png';

const App = () => (
  <div>
  {/* Replace <img /> tag with <CompressCacheImage /> to optimise images*/}
    <CompressCacheImage
      image={SampleImage} // Required: The image file to optimize and cache
      name="sampleImage"  // Required: Unique key for storing this image in IndexedDB
    />
  </div>
);

export default App;

Props

  • image (required): Image file imported from assets.
  • name (required): Unique key for storing the image in IndexedDB. The name prop is critical because it allows the library to store and retrieve the image efficiently from IndexedDB. Without it, caching and retrieval will not function.
  • ImageCompression (optional, default: true): Set to false to store the original image without compression.
  • width and height (optional): Set the width and height of the displayed image.
  • style (optional): Pass a custom style object to apply additional CSS styling directly to the image.
  • alt (optional, default: name): Alt text for the image, which will be used for accessibility and SEO purposes. If not provided, it defaults to the name value.
  • onClick (optional): A click handler function that can be applied to the image.
  • className (optional): CSS class name that can be added to the image element for styling or additional functionality.
  • placeholder (optional): URL for a placeholder image that displays while the main image is loading. Use this to provide a low-resolution image or a loading indicator.
  • compressionSettings (optional): Object with properties maxWidthOrHeight and quality to control image compression settings. The default values are { maxWidthOrHeight: 1920, quality: 0.8 }.

Example with Options

import React from 'react';
import CompressCacheImage from 'cached-image-optimizer';

import SampleImage from '../assets/sample-image.png';
import SampleImage2 from '../assets/sample-image2.png';

const App = () => (

  const handleImageClick = () => {
    alert("Image clicked!");
  };

  <div>
  {/* Replace <img /> tag with <CompressCacheImage /> to optimise images*/}
    <CompressCacheImage
      image={SampleImage}
      name="sampleImage"
      ImageCompression={true}
      width="200px"
      height="200px"
      alt= "alt"
      onClick={handleImageClick} // Adding onClick handler to image
      className="custom-image-class" // Applying a custom class for styling
      placeholder="/path/to/placeholder.jpg" // Displayed while loading the main image
      compressionSettings={{ maxWidthOrHeight: 1024, quality: 0.7 }} // Custom compression settings
    />
    <CompressCacheImage
      image={SampleImage2}
      name="sampleImage2"
      ImageCompression={false}
      width="200px"
      height="200px"
      alt= "alt"
      style={{ boxShadow: '0px 4px 8px rgba(0,0,0,0.2)' }}
      placeholder="/path/to/placeholder.jpg" // Displayed while loading the main image
      compressionSettings={{ maxWidthOrHeight: 1024, quality: 0.7 }} // Custom compression settings
    />
  </div>
);

export default App;

License

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

Author

Contributors

Feel free to contribute to this project!