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

react-image-crop

Package Overview
Dependencies
Maintainers
1
Versions
167
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-image-crop

A responsive image cropping tool for React

  • 11.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
392K
decreased by-13.49%
Maintainers
1
Weekly downloads
 
Created

What is react-image-crop?

The react-image-crop package is a React component for cropping images with a user-friendly interface. It allows users to select and crop images within a defined area, providing a variety of customization options for the cropping process.

What are react-image-crop's main functionalities?

Basic Image Cropping

This code demonstrates the basic usage of the react-image-crop package. It allows users to upload an image and then crop it using a resizable and draggable crop area.

import React, { useState } from 'react';
import ReactCrop from 'react-image-crop';
import 'react-image-crop/dist/ReactCrop.css';

function ImageCropper() {
  const [crop, setCrop] = useState({ aspect: 16 / 9 });
  const [src, setSrc] = useState(null);

  const onImageLoaded = (image) => {
    // Perform actions when the image is loaded
  };

  const onCropComplete = (crop) => {
    // Perform actions when the crop is complete
  };

  const onCropChange = (crop) => {
    setCrop(crop);
  };

  const handleFileChange = (e) => {
    const file = e.target.files[0];
    const reader = new FileReader();
    reader.onload = () => setSrc(reader.result);
    reader.readAsDataURL(file);
  };

  return (
    <div>
      <input type="file" accept="image/*" onChange={handleFileChange} />
      {src && (
        <ReactCrop
          src={src}
          crop={crop}
          onImageLoaded={onImageLoaded}
          onComplete={onCropComplete}
          onChange={onCropChange}
        />
      )}
    </div>
  );
}

export default ImageCropper;

Aspect Ratio Locking

This code demonstrates how to lock the aspect ratio of the crop area. In this example, the aspect ratio is set to 1:1, meaning the crop area will always be a square.

import React, { useState } from 'react';
import ReactCrop from 'react-image-crop';
import 'react-image-crop/dist/ReactCrop.css';

function AspectRatioCropper() {
  const [crop, setCrop] = useState({ aspect: 1 / 1 });
  const [src, setSrc] = useState(null);

  const onImageLoaded = (image) => {
    // Perform actions when the image is loaded
  };

  const onCropComplete = (crop) => {
    // Perform actions when the crop is complete
  };

  const onCropChange = (crop) => {
    setCrop(crop);
  };

  const handleFileChange = (e) => {
    const file = e.target.files[0];
    const reader = new FileReader();
    reader.onload = () => setSrc(reader.result);
    reader.readAsDataURL(file);
  };

  return (
    <div>
      <input type="file" accept="image/*" onChange={handleFileChange} />
      {src && (
        <ReactCrop
          src={src}
          crop={crop}
          onImageLoaded={onImageLoaded}
          onComplete={onCropComplete}
          onChange={onCropChange}
        />
      )}
    </div>
  );
}

export default AspectRatioCropper;

Custom Crop Area Styling

This code demonstrates how to apply custom styling to the crop area. By importing a custom CSS file, you can style the crop area to fit your application's design requirements.

import React, { useState } from 'react';
import ReactCrop from 'react-image-crop';
import 'react-image-crop/dist/ReactCrop.css';
import './CustomCrop.css'; // Custom CSS for crop area

function CustomStyledCropper() {
  const [crop, setCrop] = useState({ aspect: 16 / 9 });
  const [src, setSrc] = useState(null);

  const onImageLoaded = (image) => {
    // Perform actions when the image is loaded
  };

  const onCropComplete = (crop) => {
    // Perform actions when the crop is complete
  };

  const onCropChange = (crop) => {
    setCrop(crop);
  };

  const handleFileChange = (e) => {
    const file = e.target.files[0];
    const reader = new FileReader();
    reader.onload = () => setSrc(reader.result);
    reader.readAsDataURL(file);
  };

  return (
    <div>
      <input type="file" accept="image/*" onChange={handleFileChange} />
      {src && (
        <ReactCrop
          src={src}
          crop={crop}
          onImageLoaded={onImageLoaded}
          onComplete={onCropComplete}
          onChange={onCropChange}
          className="custom-crop"
        />
      )}
    </div>
  );
}

export default CustomStyledCropper;

Other packages similar to react-image-crop

Keywords

FAQs

Package last updated on 09 Sep 2024

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