Code Monkey home page Code Monkey logo

react-image-cropper-dynamic's Introduction

React Image Cropper Dynamic

Changes

The purpose of this repo is to give more control to image sizing. There is a bug that exists in the current source code which will force any image to be sized to the dimensions of the parent container.

Problems:

  1. If your parent container is 600px wide and you upload a 1500px image, the 1500px image will be stretched down to 600px meaning the maxium crop size is 600px. This is a major flaw.
  2. If your container is 1500px wide and your image is 600px, the 600px image will be stretched. Even though the original image is only 600px wide, your can crop this image to a 1500px wide segment.

These are the problems that prompted me to make some additions to the source code.

How to implement the changes

Step 1) Referencing the new package

To use this package instead of the original you must edit your project's package.json to reference this repo like this:

"react-image-cropper": "git+https://github.com/jamesdorrian/react-image-cropper-dynamic.git"

Step 2) Finding the image Width

The react image documentation available here has a method called .getSize() which is an asynchronous call returning the width and height of an image. However, This method can fail if the image cannot be found, or fails to download and as such should not be relied upon too heavily.

A more robust (but somewhat hacky) change that can be used is to use the image src to render a background image with z-index -10 and position:absolute; Use the <Img ref='hiddenImage' className='hideMe' onLoad={() => this.executeFunctionName()}> method to access the width and height of the image when loaded. This can be done easily using refs to access the Image object's information.

Only when the image is loaded should you allow the cropper to render (using conditional react rendering techniques found here.

Step 3) Loading the image width prop into the component

Once these dimensions are attained and the cropper is ready to be loaded, you must enter the new width as a prop to the <Cropper newItem={WIDTH HERE AS INTEGER} />

See the original package

See the original demo

react-image-cropper-dynamic's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

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.