|
@@ -49,6 +49,14 @@ const ImageCropModal: FC<Props> = (props: Props) => {
|
|
|
const { t } = useTranslation();
|
|
const { t } = useTranslation();
|
|
|
const reset = useCallback(() => {
|
|
const reset = useCallback(() => {
|
|
|
if (imageRef) {
|
|
if (imageRef) {
|
|
|
|
|
+ // Some SVG files may not have width and height properties, causing the render size to be 0x0
|
|
|
|
|
+ // Force imageRef to have width and height by create temporary image element then set the imageRef width with tempImage width
|
|
|
|
|
+ // Set imageRef width & height by natural width / height if image has no dimension
|
|
|
|
|
+ if (imageRef.width === 0 || imageRef.height === 0) {
|
|
|
|
|
+ imageRef.width = imageRef.naturalWidth;
|
|
|
|
|
+ imageRef.height = imageRef.naturalHeight;
|
|
|
|
|
+ }
|
|
|
|
|
+ // Get size of Image, min value of width and height
|
|
|
const size = Math.min(imageRef.width, imageRef.height);
|
|
const size = Math.min(imageRef.width, imageRef.height);
|
|
|
setCropOtions({
|
|
setCropOtions({
|
|
|
aspect: 1,
|
|
aspect: 1,
|
|
@@ -63,6 +71,7 @@ const ImageCropModal: FC<Props> = (props: Props) => {
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
document.body.style.position = 'static';
|
|
document.body.style.position = 'static';
|
|
|
|
|
+ setIsCropImage(true);
|
|
|
reset();
|
|
reset();
|
|
|
}, [reset]);
|
|
}, [reset]);
|
|
|
|
|
|
|
@@ -73,18 +82,22 @@ const ImageCropModal: FC<Props> = (props: Props) => {
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
- const onCropChange = (crop) => {
|
|
|
|
|
- setCropOtions(crop);
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ const getCroppedImg = async(image: HTMLImageElement, crop: ICropOptions) => {
|
|
|
|
|
+ const {
|
|
|
|
|
+ naturalWidth: imageNaturalWidth, naturalHeight: imageNaturalHeight, width: imageWidth, height: imageHeight,
|
|
|
|
|
+ } = image;
|
|
|
|
|
+
|
|
|
|
|
+ const {
|
|
|
|
|
+ width: cropWidth, height: cropHeight, x, y,
|
|
|
|
|
+ } = crop;
|
|
|
|
|
|
|
|
- const getCroppedImg = async(image, crop) => {
|
|
|
|
|
const canvas = document.createElement('canvas');
|
|
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 scaleX = imageNaturalWidth / imageWidth;
|
|
|
|
|
+ const scaleY = imageNaturalHeight / imageHeight;
|
|
|
|
|
+ canvas.width = cropWidth;
|
|
|
|
|
+ canvas.height = cropHeight;
|
|
|
const ctx = canvas.getContext('2d');
|
|
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);
|
|
|
|
|
|
|
+ ctx?.drawImage(image, x * scaleX, y * scaleY, cropWidth * scaleX, cropHeight * scaleY, 0, 0, cropWidth, cropHeight);
|
|
|
try {
|
|
try {
|
|
|
const blob = await canvasToBlob(canvas);
|
|
const blob = await canvasToBlob(canvas);
|
|
|
return blob;
|
|
return blob;
|
|
@@ -105,7 +118,6 @@ const ImageCropModal: FC<Props> = (props: Props) => {
|
|
|
// Clear image and set isImageCrop true on modal close
|
|
// Clear image and set isImageCrop true on modal close
|
|
|
const onModalCloseHandler = async() => {
|
|
const onModalCloseHandler = async() => {
|
|
|
setImageRef(null);
|
|
setImageRef(null);
|
|
|
- setIsCropImage(true);
|
|
|
|
|
onModalClose();
|
|
onModalClose();
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -129,12 +141,21 @@ const ImageCropModal: FC<Props> = (props: Props) => {
|
|
|
<ModalBody className="my-4">
|
|
<ModalBody className="my-4">
|
|
|
{
|
|
{
|
|
|
isCropImage
|
|
isCropImage
|
|
|
- ? (<ReactCrop src={src} crop={cropOptions} onImageLoaded={onImageLoaded} onChange={onCropChange} circularCrop={isCircular} />)
|
|
|
|
|
|
|
+ ? (
|
|
|
|
|
+ <ReactCrop
|
|
|
|
|
+ style={{ backgroundColor: 'transparent' }}
|
|
|
|
|
+ src={src}
|
|
|
|
|
+ crop={cropOptions}
|
|
|
|
|
+ onImageLoaded={onImageLoaded}
|
|
|
|
|
+ onChange={crop => setCropOtions(crop)}
|
|
|
|
|
+ circularCrop={isCircular}
|
|
|
|
|
+ />
|
|
|
|
|
+ )
|
|
|
: (<img style={{ maxWidth: imageRef?.width }} src={imageRef?.src} />)
|
|
: (<img style={{ maxWidth: imageRef?.width }} src={imageRef?.src} />)
|
|
|
}
|
|
}
|
|
|
</ModalBody>
|
|
</ModalBody>
|
|
|
<ModalFooter>
|
|
<ModalFooter>
|
|
|
- <button type="button" className="btn btn-outline-danger rounded-pill mr-auto" onClick={reset}>
|
|
|
|
|
|
|
+ <button type="button" className="btn btn-outline-danger rounded-pill mr-auto" disabled={!isCropImage} onClick={reset}>
|
|
|
{t('crop_image_modal.reset')}
|
|
{t('crop_image_modal.reset')}
|
|
|
</button>
|
|
</button>
|
|
|
{ !showCropOption && (
|
|
{ !showCropOption && (
|