question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Image crop out of bounds on load

See original GitHub issue

Hello. I am trying to get my image cropped immediately on load within set aspect road. however when using the code below, from the documentation here, my crop ends up being incorrect.

  const onLoad = useCallback(img => {
    imgRef.current = img;
  
    const aspect = 16 / 9;
    const width = img.width / aspect < img.height * aspect ? 100 : ((img.height * aspect) / img.width) * 100;
    const height = img.width / aspect > img.height * aspect ? 100 : (img.width / aspect / img.height) * 100;
    const y = (100 - height) / 2;
    const x = (100 - width) / 2;
  
    setCrop({
      unit: '%',
      width,
      height,
      x,
      y,
      aspect,
    });
  
    return false; // Return false if you set crop state in here.
  }, []);

https://codesandbox.io/s/react-image-crop-demo-with-react-hooks-forked-sy59w?file=/src/App.js:1127-1683

This is the image:

132-21-24 (2)

and here is how it looks after load: image

and here is how i expect it to look: image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
DominicTobiascommented, Mar 7, 2022

Thanks I’ve updated the documentation and added some helpers to make it easier to do % images now, in v10:

function onImageLoad(e) {
  const { width, height } = e.currentTarget

  const crop = centerCrop(
    makeAspectCrop(
      {
        unit: '%',
        width: 100,
      },
      16 / 9,
      width,
      height
    ),
    width,
    height
  )

  setCrop(crop)
}

It’s also on the codesandbox link: https://codesandbox.io/s/react-image-crop-demo-with-react-hooks-y831o

1reaction
gschiercommented, Jan 15, 2022

Could you please share exactly what you did to avoid this issue. This would be really helpful, thanks.

Sure, here’s my full function that gets the default crop based on the image size and the size of the element that contains the crop component. I hope it helps @supasus!

function getDefaultCrop(img: ResizeImageResult, clientWidth: number, clientHeight: number): Crop {
  const aspect = 0.6; // Locked aspect for crop rectangle
  const padding = 20; // How many pixels to leave around the crop rectangle

  // Calculate the max width/height of the image based on its size and the crop container
  const imgAspect = img.width / img.height;
  const imgWidthMax = Math.min(img.width, clientWidth);
  const imgHeightMax = Math.min(img.height, clientHeight);

  // Calculate size of image based on aspect and max width/height
  const imgWidth = imgHeightMax * imgAspect < clientWidth ? imgHeightMax * imgAspect : imgWidthMax;
  const imgHeight = imgWidthMax / imgAspect < clientHeight ? imgWidthMax / imgAspect : imgHeightMax;

  // Calculate the default crop rectangle based on image size and crop aspect
  const width = (imgHeight * aspect < imgWidth ? imgHeight * aspect : imgWidth) - padding;
  const height = (imgWidth / aspect < imgHeight ? imgWidth / aspect : imgHeight) - padding;

  // Position crop rectangle in the exact center
  const x = (imgWidth - width) / 2;
  const y = (imgHeight - height) / 2;

  return { aspect, width, height, x, y, unit: 'px' };
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

crop uiimageview which is out of bounds of another view
I have an UIImaveView(red one) which I have rotated to 45°. In this I a drawed an UIImage with a gradient and places...
Read more >
Cropping an image out of bounds stretches the output image
If I specify a crop area that is outside of the image area then the output image will only contain a stretched version...
Read more >
crop - Fastly Developer Hub
If the specified cropped region is outside the bounds of the image, the transformation will fail with the error "Invalid transformation for requested...
Read more >
Cropping an Image using OpenCV - LearnOpenCV
Load the height and width to specify the range till which the smaller patches need to be cropped out. For this, use the...
Read more >
Customize an image | Jetpack Compose - Android Developers
Specify a contentScale option to crop or change how an image is scaled inside its bounds. By default, if you don't specify a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found