sekoyo / react-image-crop

A responsive image cropping tool for React
ISC License
3.89k stars 344 forks source link

While trying to rotate the image, the image itself is not fitting inside the container, full image is not visible after rotating to 90 or 270 degree #598

Open rajputkamal opened 3 months ago

rajputkamal commented 3 months ago
              ` <ReactCrop
                  crop={crop}
                  onChange={(_, percentageCrop) => setCrop(percentageCrop)}
                  onComplete={(c) =>
                    setCompletedCrop && setCompletedCrop(c)
                  }
                  aspect={builderLandscapeAspectRatio(builderAspectRatio)} // passing different aspect ratios
                  keepSelection
                  style={{
                    minHeight: "100%",
                    height: "auto",
                  }}
                >
                  <StyledImage
                    id="cropped-image"
                    ref={imgRef}
                    src={largeThumbnailMediaurl}
                    alt="Crop me"
                    onLoad={onImageLoad}
                    style={{
                      transform: `scale(${scale}) rotate(${rotate}deg)`,
                    }}
                  />
             </ReactCrop>`

I tried the below approach to swap the height and width during the rotation.

                `const image = document.getElementById("cropped-image") as HTMLCanvasElement;
                  const reactCropperContainer = document.querySelector(
                    ".ReactCrop--fixed-aspect",
                  ) as HTMLDivElement;

                  const isRotated = [90, 270, -90, -270].includes(degree);

                  if (isRotated) {
                      reactCropperContainer.style.width = `${newHeight}px`;
                      reactCropperContainer.style.height = `${newWidth}px`;
                      image.style.transform = `scale(${scale}) rotate(${rotate}deg)`;
                    } else {
                      reactCropperContainer.style.width = `${newWidth}px`;
                      reactCropperContainer.style.height = "100%";
                      image.style.transform = `scale(${scale}) rotate(${rotate}deg)`;
                  }`

but this approach is also not working. My main problem is image is entirely not fitting inside the container during rotation

Rishab-Mishra commented 2 months ago

did you solved it ?

rajputkamal commented 2 months ago

Yes, thanks

pedrodias2703 commented 2 months ago

@rajputkamal how?

hiimkosu1 commented 2 months ago

How did you fix it?

rajputkamal commented 2 months ago

I fixed by making internal API calls to return the updated image url with rotation.