1.0.3 • Published 7 months ago

@meghoshpritam/react-image-file-resizer v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

React Image File Resizer 🌄✨

@meghoshpritam/react-image-file-resizer is your go-to magic wand for enhancing your React application with image resizing capabilities. 🚀

  • Transform images effortlessly by altering their width, height, format, rotation, and quality.
  • Obtain the resized image in either base64 URI or Blob format. The URI is perfect for directly sourcing images in your <Image> component.

Quick Setup

Start by installing this enchanting package:

npm install @meghoshpritam/react-image-file-resizer

or

yarn add @meghoshpritam/react-image-file-resizer

or

pnpm install @meghoshpritam/react-image-file-resizer

Casting Spells with Magic Resizing

import Resizer from "@meghoshpritam/react-image-file-resizer";

Resizer.imageFileResizer({
  file, // The image file to be resized.
  maxWidth, // The maximum width for the new image.
  maxHeight, // The maximum height for the new image.
  compressFormat, // The image format for compression (JPEG, PNG, or WEBP).
  quality, // The quality of the resized image.
  rotation, // The degree of clockwise rotation to apply to the image.
  responseUriFunc, // The callback function for the resized image URI.
  outputType, // The output type for the resized image.
  minWidth, // The minimum width for the new image.
  minHeight, // The minimum height for the new image.
  keepAspectRatio, // Whether to maintain the aspect ratio of the resized image.
});

Captivating Examples

Example 1

Begin by invoking the resizer:

import Resizer from "@meghoshpritam/react-image-file-resizer";

const resizeFile = (file) =>
  new Promise((resolve) => {
    Resizer.imageFileResizer({
      file,
      maxWidth: 300,
      maxHeight: 300,
      compressFormat: "JPEG",
      quality: 100,
      rotation: 0,
      keepAspectRatio: true,
      responseUriFunc: (uri) => {
        resolve(uri);
      },
      outputType: "base64"
  });
});

Now, wield your powers within an async function:

const onChange = async (event) => {
  try {
    const file = event.target.files[0];
    const image = await resizeFile(file);
    console.log(image);
  } catch (err) {
    console.log(err);
  }
};

Example 2

Behold, a React sorcerer at work:

import React, { Component } from "react";
import Resizer from "@meghoshpritam/react-image-file-resizer";

class App extends Component {
  constructor(props) {
    super(props);
    this.fileChangedHandler = this.fileChangedHandler.bind(this);
    this.state = {
      newImage: "",
    };
  }

  fileChangedHandler(event) {
    var fileInput = false;
    if (event.target.files[0]) {
      fileInput = true;
    }
    if (fileInput) {
      try {
        Resizer.imageFileResizer({
          file: event.target.files[0],
          maxWidth: 300,
          maxHeight: 300,
          compressFormat: "JPEG",
          quality: 100,
          rotation: 0,
          keepAspectRatio: true,
          responseUriFunc: (uri) => {
            console.log(uri);
            this.setState({ newImage: uri });
          },
          outputType: "base64",
          minWidth: 200,
          minHeight: 200
      });
      } catch (err) {
        console.log(err);
      }
    }
  }

  render() {
    return (
      <div className="App">
        <input type="file" onChange={this.fileChangedHandler} />
        <img src={this.state.newImage} alt="" />
      </div>
    );
  }
}

export default App;

Witness the Magic! 🔮

Enchanting Options

OptionDescriptionTypeRequired
filePath of the image fileobjectYes
maxWidthThe new image's maximum width (aspect ratio is preserved)numberYes
maxHeightThe new image's maximum height (aspect ratio is preserved)numberYes
compressFormatThe compression format for the image (JPEG, PNG, or WEBP)stringYes
qualityA number between 0 and 100, used for JPEG compression (set to 100 if no compression is needed)numberYes
rotationThe degree of clockwise rotation to apply to the image (limited to multiples of 90 degrees)numberYes
responseUriFuncThe callback function for the URI of the resized image (e.g., uri => {console.log(uri)})functionYes
outputTypeThe output type for the resized image (base64, blob, or file)stringNo
minWidthThe new image's minimum width (aspect ratio is preserved, defaults to null)numberNo
minHeightThe new image's minimum height (aspect ratio is preserved, defaults to null)numberNo
keepAspectRatioWhether to maintain the aspect ratio of the image (defaults to true)booleanYes

License

MIT License 📜