Reputation: 547
import React, { PureComponent } from 'react';
import ReactCrop from 'react-image-crop';
import 'react-image-crop/dist/ReactCrop.css';
class CoachDashboard extends PureComponent {
state = {
src: null,
crop: {
unit: '%',
width: 50,
aspect: 20 / 16,
},
};
onSelectFile = e => {
if (e.target.files && e.target.files.length > 0) {
const reader = new FileReader();
reader.addEventListener('load', () =>
this.setState({ src: reader.result })
);
reader.readAsDataURL(e.target.files[0]);
console.log(e.target.files[0])
}
};
onImageLoaded = image => {
this.imageRef = image;
};
onCropComplete = crop => {
this.makeClientCrop(crop);
};
onCropChange = (crop, percentCrop) => {
this.setState({ crop });
};
async makeClientCrop(crop) {
if (this.imageRef && crop.width && crop.height) {
const croppedImageUrl = await this.getCroppedImg(
this.imageRef,
crop,
'newFile.jpeg'
);
console.log(croppedImageUrl)
this.setState({ croppedImageUrl });
}
}
getCroppedImg(image, crop, fileName) {
const canvas = document.createElement('canvas');
const scaleX = image.naturalWidth / image.width;
const scaleY = image.naturalHeight / image.height;
canvas.width = crop.width;
canvas.height = crop.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(
image,
crop.x * scaleX,
crop.y * scaleY,
crop.width * scaleX,
crop.height * scaleY,
0,
0,
crop.width,
crop.height
);
return new Promise((resolve, reject) => {
canvas.toBlob(blob => {
if (!blob) {
console.error('Canvas is empty');
return;
}
blob.name = fileName;
window.URL.revokeObjectURL(this.fileUrl);
this.fileUrl = window.URL.createObjectURL(blob);
resolve(this.fileUrl);
}, 'image/jpeg');
});
}
render() {
const { crop, croppedImageUrl, src } = this.state;
return (
<div className="App">
<br/><br/><br/><br/><br/>
<div>
<input type="file" onChange={this.onSelectFile} />
</div>
<div style={{ width:'500px' }}>
{src && (
<ReactCrop
src={src}
crop={crop}
ruleOfThirds
onImageLoaded={this.onImageLoaded}
onComplete={this.onCropComplete}
onChange={this.onCropChange}
width= '200'
height= '200'
/>
)}
</div>
{croppedImageUrl && (
<img alt="Crop" style={{ height:'200px' }} src={croppedImageUrl} />
)}
</div>
);
}
}
export default CoachDashboard;
Here i am cropping an image using react-image-crop
.
I have to send that image file to backend and store to database.
But, in onSelectChange()
function I am getting actual image file using e.target.files[0]
.
But after cropping when i am printing croppedImageUrl
inside makeClientCrop()
function I am getting one fake image url.
How can I get actual image file after cropping like I am getting in onSelectChange()
function while console.log()
?
Upvotes: 3
Views: 5436
Reputation: 21
What you can do it is save the blob file inside the state in canvas.toBlob inside the getCroppedImg function and later convert it in a file when saving the file.
getCroppedImg(image, crop, fileName) {
const canvas = document.createElement('canvas');
const scaleX = image.naturalWidth / image.width;
const scaleY = image.naturalHeight / image.height;
canvas.width = crop.width;
canvas.height = crop.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(
image,
crop.x * scaleX,
crop.y * scaleY,
crop.width * scaleX,
crop.height * scaleY,
0,
0,
crop.width,
crop.height
);
return new Promise((resolve, reject) => {
canvas.toBlob(blob => {
if (!blob) {
console.error('Canvas is empty');
return;
}
blob.name = fileName;
window.URL.revokeObjectURL(this.fileUrl);
this.fileUrl = window.URL.createObjectURL(blob);
this.setState({blobFile:blob}) // Set blob image inside the state here
resolve(this.fileUrl);
}, 'image/jpeg');
});
}
// To save file
onFileSave(){
const {blobFile} = this.state;
const newImage = new File([blobFile], blobFile.name, { type: blobFile.type });
let fd = new FormData();
fd.append(newImage.name, newImage);
saveImageToServer(fd);
}
Upvotes: 1