1.0.2-a • Published 1 year ago

react-optimizer-image-cropper v1.0.2-a

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

react-optimizer-image-cropper

A React component for optimizing and cropping images with ease. This library provides a user-friendly interface for image cropping and optimization, making it simple to integrate into your React applications.

Getting started

You can install the package using npm or yarn:

npm install react-optimizer-image-cropper

or

yarn add react-optimizer-image-cropper

Example

Here is an example of how to use the react-optimizer-image-cropper in your React project:

import React, { useRef, useState } from 'react';
import { ImageCropper, ImageCropperRef } from "react-optimizer-image-cropper";

const App = () => {
  const [file, setFile] = useState<File | null>(null);
  const handleChangeInputFile = (e: React.ChangeEvent<HTMLInputElement>) => {
    if (e.target.files && e.target.files.length > 0) {
      setFile(e.target.files[0]);
    }
  };

  const ImageCropperRef = useRef<ImageCropperRef>(null);

  const handleFileSourceDownload = async () => {
    if (ImageCropperRef.current) {
      const file = await ImageCropperRef.current.returnOriginal();

      if (file) {
        const url = URL.createObjectURL(file);
        const a = document.createElement('a');
        a.href = url;
        a.download = `${file.name}`;
        a.click();
        URL.revokeObjectURL(url);
      }
    }
  };

  const handleFileOptimizedDownload = async () => {
    if (ImageCropperRef.current) {
      const blob = await ImageCropperRef.current.returnOptimized();

      if (blob) {
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = `${file?.name.split('.', 1)}_optimized.png`
        a.click();
        URL.revokeObjectURL(url);
      }
    }
  };

  const handleFileCroppedDownload = async (type: 'webp' | 'png') => {
    if (ImageCropperRef.current) {
      const blob = await ImageCropperRef.current.returnResult(type);

      if (blob) {
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = `${file?.name.split('.', 1)}_cropped.${type}`;
        a.click();
        URL.revokeObjectURL(url);
      }
    }
  };
  
  return (
    <div>
      <input
        type='file'
        accept='image/*'
        onChange={handleChangeInputFile}
      />

      <ImageCropper
        ref={ImageCropperRef}
        src={file}
      />

    </div>
  );
};

export default App;

Functions

ImageCropperRef.current.returnOriginal();

return original src file

ImageCropperRef.current.returnOptimized();

return optimized blob

ImageCropperRef.current.returnResult(type);

return cropped blob with format webp/png (default: webp)

Props

The ImageCropper component accepts the following props:

NameDescriptionDefault Value
refRef for the loaded imagenull
srcThe source of the image to be croppednull
optimizedWhen false, the src image will be croppedtrue
maxDimensionProportional reduction of the image by the larger side to the specified value2000
aspectAspect ratio for the crop area (e.g., 16/9, 4/3)undefined
scaleScale of the imageundefined
rotateRotation of the imageundefined
classNameWrapperStyle for the wrappernull
classNameStyle for the componentnull
classNameImageStyle for the image inside the componentnull
loaderCustom loader for the loading statenull
refPreviewBoxRef for the preview drawn on canvasnull
circularCropShow the crop area as a circle. If the aspect is not 1 (a square), the circle will be warped into an oval shapefalse
ruleOfThirdsShow rule of thirds lines in the cropped areafalse
disabledIf true, the user cannot resize or draw a new crop. A class of ReactCrop--disabled is also added to the container for user stylingfalse

See the demo

License

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

1.0.2-a

1 year ago

1.0.1-b

1 year ago

1.0.1-a

1 year ago

1.0.0-b

1 year ago

1.0.0-a

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago