Code Monkey home page Code Monkey logo

Comments (7)

zackify avatar zackify commented on May 26, 2024 2

Doing this is really easy. Here's how my component looks:

getBase64(file) {
    this.setState({ loading: true, file });

    let reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => {
      var image = new Image();
      image.src = reader.result;

      image.onload = () => {
        this.setState({
          loading: false,
          image: reader.result,
          height: image.height,
          width: image.width,
        });
      };
    };
    reader.onerror = error => console.log('Error: ', error);
  }

Get the base64 of a file object, render it to an image, get the height and width. Then, pass the height and width to your cropper props, something like this:

          minWidth: 1920 / image.width * 100,
          minHeight: 350 / image.height * 100,
          maxWidth: 1920 / image.width * 100,
          maxHeight: 350 / image.height * 100,
   

Now you have percentages from pixels, for anyone else who needs it :)

from react-image-crop.

sekoyo avatar sekoyo commented on May 26, 2024

Hi @riccardolorenzon updating the README was confusing because that version isn't released yet. RC7 only passes the normal crop object. If you want it to pass pixels then your should also pass the outputPixelUnits prop. At the moment there is no way to pass pixels in because it would require another property or for values to specify a unit e.g. instead of 220 it would be 220px as there is no way to determine whether the passed in values are pixels or percentages. I'll consider it for the next version though. In the mean time you could do the calculation yourself to convert pixels to percentages.

from react-image-crop.

riccardolorenzon avatar riccardolorenzon commented on May 26, 2024

hi @dominictobias, thank you for the quick reply.

Any suggestions on how to get the natural height and width of the image without loading it again?

from react-image-crop.

riccardolorenzon avatar riccardolorenzon commented on May 26, 2024

I used the natualHeight and naturalWidth img properties: http://www.w3schools.com/jsref/prop_img_naturalheight.asp

from react-image-crop.

riccardolorenzon avatar riccardolorenzon commented on May 26, 2024

I just updated the component to the last version.

Is it now possible to specify the crop coordinates using pixels? i can't find that part on the docs.

from react-image-crop.

sekoyo avatar sekoyo commented on May 26, 2024

Hi @riccardolorenzon no there isn't and there is no intention of adding this, most other cropping tools out there use pixels so if you prefer that you might be better off with them (it would complicate the code base too much to add support for both). The point of using percentages is so that when the crop is resized it is still correct (responsive) - there is no need to write an algorithm to detect if the crop element resizes, poll for it, and recalc the crop. This makes it smoother and the code simpler (it just works), the drawback is that if you want to pass pixels in then you have to calc them to percentage - which would be pretty trivial to do. Although you do get passed the pixels in onComplete and onChange for convenience of passing that to e.g. a backend, you are still supposed to pass in percentages.

PS if pixelCrop is undefined though, that's a bug, but I think that's working

from react-image-crop.

WaqqarSuleman avatar WaqqarSuleman commented on May 26, 2024

I used the natualHeight and naturalWidth img properties: http://www.w3schools.com/jsref/prop_img_naturalheight.asp

Getting naturalWidth and naturalHeight as 0, any idea why?

from react-image-crop.

Related Issues (20)

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.