Git Product home page Git Product logo

react-crop's Introduction

#react-crop# An accessible image cropper where the image is stationary and a resizable, draggable box represents the cropped image

For example usage check out the docs folder. Demo: http://instructure-react.github.io/react-crop/

###Basic usage###

import React, { Component } from 'react';

import Cropper from 'react-crop';
import 'react-crop/css';

// You'll need to use async functions
import "babel-core/register";
import "babel-polyfill";

export default class MyComponent extends Component {
    constructor() {
        super();

        this.state = {
            image: null,
            previewImage: null
        };
    }

    onChange(evt) {
        this.setState({
            image: evt.target.files[0]
        })
    }

    async crop() {
        let image = await this.refs.crop.cropImage()
        this.setState({
            previewUrl: window.URL.createObjectURL(image)
        })
    }

    clear() {
        this.refs.file.value = null
        this.setState({
            previewUrl: null,
            image: null
        })
    }

    imageLoaded(img) {
        if (img.naturalWidth && img.naturalWidth < 262 &&
            img.naturalHeight && img.naturalHeight < 147) {
            this.crop()
        }
    }

    render() {
        return (
            <div>
                <input ref='file' type='file' onChange={this.onChange} />

                {

                    this.state.image &&

                    <div>
                        <Cropper
                            ref='crop'
                            image={this.state.image}
                            width={100}
                            height={80}
                            onImageLoaded={this.imageLoaded}
                        />

                        <button onClick={this.crop}>Crop</button>
                        <button onClick={this.clear}>Clear</button>
                    </div>

                }

                {
                    this.state.previewUrl &&

                    <img src={this.state.previewUrl} />
                }

            </div>
        );
    }
}

###Props###

####width#### This is the desired width that you would like the image to be cropped to. The width of the cropper will be scaled to fit this size. This prop also helps determine the minimum width that the cropper can be.

####height#### This is the desired height that you would like the image to be cropped to. The height of the cropper will be scaled to fit this size. This prop also helps determine the minimum height that the cropper can be. The width and height aspect ratio will be preserved while resizing the cropper.

####image#### A blob of the original image that you wish to crop.

####widthLabel#### The label to use next to the width input used for keyboard users. This is especially useful if you need to localize the text. The default is "Width".

####heightLabel#### The label to use next to the height input used for keyboard users. This is especially useful if you need to localize the text. The default is "Height".

####offsetXLabel#### The label to use next to the offset X input used for keyboard users. This is especially useful if you need to localize the text. The default is "Offset X".

####offsetYLabel#### The label to use next to the offset Y input used for keyboard users. This is especially useful if you need to localize the text. The default is "Offset Y".

###Running the Example###

  • Clone the repo
  • npm i
  • npm run docs
  • Visit localhost:8080

react-crop's People

Contributors

aaronshaf avatar claydiffrient avatar eschiebel avatar fidelisclayton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-crop's Issues

Image rotation

Hi team,
Wonderful plugin and easily customizable.
One issue I faced is not able to rotate the image and then crop.
It will be really appreciate if that also incorporate

Module not found: Can't resolve 'react-crop/css'

I am just trying to get Cropper up and running in a project but cannot seem to get react-crop/css to load.

I have check all dependencies and they are installed ok.

I am using import 'react-crop/css';

Do I need to specify the full path?

DraggableResizableBox Warning

Please update the DraggableResizableBox for 2 warning :

Warning: Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead.

Warning: DraggableResizableBox: React.createClass is deprecated and will be removed in version 16. Use plain JavaScript classes instead. If you're not yet ready to migrate, create-react-class is available on npm as a drop-in replacement.

Thank you very much!

Tall images not contained

If the aspect ratio of the image is taller than react-crop's aspect ratio, the image height is not adjusted to fit.

screen shot 2017-02-01 at 8 37 52 pm

Can't change image

Hey, this looks great!

I'm not able to change the image after setting it, without calling clear. Seeing this in the demo as well.

Cropper Options

Hi @aaronshaf ! Thank you for open sourcing this useful library. Wondering if there are configuration options for the following -

  1. Have a circular cropper instead of square or rectangle?
  2. Have the cropper centered on the image when initialized?

Thanks!

Cropper is not rendering in HTML

Hey,

Thanks for the package, I am stcuk while rendering the cropper. Below is my code, can you please help?

import React, { Component } from 'react';

let Cropper = null;

export default class UserDetailEdit extends Component {
  
  state = {
    image_src: null
  };
  
  componentDidMount() {
    Cropper = require('react-crop');
  }
  
  uploadFile(e) {
    this.setState({ image_src: e.target.files[0] });
  }
  
  render() {
    return (
      <div>
        {
          this.state.image_src === null || Cropper === null ? (
            <input type="file" onChange={this.uploadFile} />
          ) : (
            <Cropper
              src={this.state.image_src}
              ref={(c) => { this.cropper = c; }}
            />
          )
        }
      </div>
    );
  }
}

It is giving me the below error,

React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

Different Width and Height on cropper

Hi,

Can you make an optional to set width and height for the cropper ?.

For example I want crop an image with width and height 800*600,
I don't want the cropper to be that big.

Thanks.

Negative scale selection

It's currently possible to drag left-top corner to the bottom and right of bottom-right corner. Found in demo that is linked on title page.

prop-types warning

Hi there, please update the source for new prop-types package. It would be nice.

Get Crop values

Hey, great job with this component.

A simple question , How can I get the crop value, height, Width, top, left ?. Not just blob object, sometimes is util only get this values.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.