Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-image-file-resizer

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-image-file-resizer

React module that can rescaled local images. You can change image's width, height, format, rotation and quality. It returns resized image's new base64 URI or Blob. The URI can be used as the source of an component.

  • 0.4.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

React Image File Resizer

All Contributors

Build Status npm version

react-image-file-resizer is a react module that can rescaled local images.

  • You can change image's width, height, format, rotation and quality.
  • It returns resized image's new base64 URI or Blob. The URI can be used as the source of an <Image> component.

Setup

Install the package:

npm i react-image-file-resizer

or

yarn add react-image-file-resizer

Usage

import Resizer from "react-image-file-resizer";

Resizer.imageFileResizer(
  file, // Is the file of the image which will resized.
  maxWidth, // Is the maxWidth of the resized new image.
  maxHeight, // Is the maxHeight of the resized new image.
  compressFormat, // Is the compressFormat of the resized new image.
  quality, // Is the quality of the resized new image.
  rotation, // Is the degree of clockwise rotation to apply to uploaded image.
  responseUriFunc, // Is the callBack function of the resized new image URI.
  outputType, // Is the output type of the resized new image.
  minWidth, // Is the minWidth of the resized new image.
  minHeight // Is the minHeight of the resized new image.
);

Example 1

First, wrap the resizer:

import Resizer from "react-image-file-resizer";

const resizeFile = (file) =>
  new Promise((resolve) => {
    Resizer.imageFileResizer(
      file,
      300,
      300,
      "JPEG",
      100,
      0,
      (uri) => {
        resolve(uri);
      },
      "base64"
    );
  });

And then use it in your 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

import React, { Component } from "react";
import Resizer from "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(
          event.target.files[0],
          300,
          300,
          "JPEG",
          100,
          0,
          (uri) => {
            console.log(uri);
            this.setState({ newImage: uri });
          },
          "base64",
          200,
          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;
OptionDescriptionTypeRequired
filePath of image fileobjectYes
maxWidthNew image max width (ratio is preserved)numberYes
maxHeightNew image max height (ratio is preserved)numberYes
compressFormatCan be either JPEG, PNG or WEBP.stringYes
qualityA number between 0 and 100. Used for the JPEG compression.(if no compress is needed, just set it to 100)numberYes
rotationDegree of clockwise rotation to apply to the image. Rotation is limited to multiples of 90 degrees.(if no rotation is needed, just set it to 0) (0, 90, 180, 270, 360)numberYes
responseUriFuncCallback function of URI. Returns URI of resized image's base64 format. ex: uri => {console.log(uri)});functionYes
outputTypeCan be either base64, blob or file.(Default type is base64)stringNo
minWidthNew image min width (ratio is preserved, defaults to null)numberNo
minHeightNew image min height (ratio is preserved, defaults to null)numberNo

License

MIT

Contributors

Thanks goes to these wonderful people (emoji key):


Ahmad Maleki

💻 🚧

Martin Vyšňovský

💻 🚧

Nadun Chamikara

💻 🚧

Shubham Zanwar

📖

Onur Önder

💻 🚧

Yunus Emre

💻 🚧

Juan

💻 🚧

Sreang Rathanak

💻 🚧

Andres Rivera

💻 🚧

mmmulani

💻 🚧

This project follows the all-contributors specification. Contributions of any kind welcome!

Keywords

FAQs

Package last updated on 07 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc