1

After the user uploads an image, i possibly have to scale it down to a size that will fit into a given bounding box even after being rotated by an arbitrary angle later on. In this case, this has to be done right after the upload.

How do i calculate the target size for the image?

Thanks in advance for your help.

Edit: i don't see any relation to the question this one has been marked as duplicate for.

normen
  • 53
  • 6
  • 1
    What have you done so far? – Maheer Ali Jan 30 '19 at 07:25
  • Possible duplicate of [Javascript Image Resize Calculation](https://stackoverflow.com/questions/35517241/javascript-image-resize-calculation) – Fabio_MO Jan 30 '19 at 07:27
  • @MaheerAli I've created the image scaling code using a canvas element, nothing unusual. It works, but i need to know the formula to calculate it's maximum dimensions so the product of width and height of it's bounding box will never exceed 2^24 even after rotation while preserving the best possible image quality. – normen Jan 30 '19 at 07:33
  • @Fabio_MO Unfortunately, i cannot find any information about the bounding box for a rotated rectangle in the post you've mentioned. – normen Jan 30 '19 at 07:37
  • Problem is not stated properly. What parameters are known? – MBo Jan 30 '19 at 07:48
  • @MBo The parameter i know is the size of the bounding box which is fixed: 4096x4096 pixels. After upload, i know the image size. I need to know to which size i have to scale the image so it will definitely fit into the given bounding box after any rotation by the user. – normen Jan 30 '19 at 07:54
  • OK, my answer is not suitable for arbitrary rotation, will delete it. – MBo Jan 30 '19 at 08:26

1 Answers1

3

From geometry standpoint, you need to ensure height and width of the bounding box are at least equal to internal image diagonal, which is sqrt(a²+b²)

enter image description here

  • Thanks for these informations and the meaningful graphics. Now i just have to calculate the target image size by the known diagonal and the aspect ratio of the image. For anyone who's interested, this is answered here: https://math.stackexchange.com/questions/63681/calculate-width-and-height-of-a-rectangle-given-its-diagonal-and-ratio. So i will mark this question as answered. – normen Jan 30 '19 at 08:50