Comments (7)
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.
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.
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.
I used the natualHeight and naturalWidth img properties: http://www.w3schools.com/jsref/prop_img_naturalheight.asp
from react-image-crop.
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.
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.
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)
- when we have circularCrop as prop, how to set the points on circle? HOT 3
- Need to rotate selected(croped) image HOT 4
- Synchronization Issue Between Multiple Cropper Instances on the Same Page HOT 2
- Image Cropping with 8/3 Aspect Ratio Causing Loss of Half Portion After Retrieval for s3 HOT 2
- Canvas toBlob crop not working on Sandbox (iOS) HOT 3
- Crop mask stays present with zero-size crop HOT 1
- SecurityError: OffscreenCanvas.convertToBlob: Cannot get blob from write-only canvas. HOT 1
- Canvas returns null after cropping image iOS HOT 1
- Mask is does'nt showing the first time HOT 6
- Memory leak risk due to event listeners not removed on componentWillUnmount HOT 1
- mask does not fully cover the image when the image to a smaller aspect ratio HOT 2
- Multiple crop selection on an image at the same time HOT 1
- Can the cropper be used for outpainting?
- React Crop as Dialog Box
- The previous image layout still be kept after switch image
- Feature request: Add the option to rotate the crop selection
- Proportional Stretching of Crop Box
- iPhone 15's 24 MP (res 4284 X 5712) image resolution is making the image black after cropping. HOT 1
- Library not supporting image cropping in iOS WebView HOT 6
- Add whitespace around image being cropped?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-image-crop.