There is a two-dimensional array - bitmap - representing the image. That is a set of pics. Each pixel is encoded by 3 bytes - RGB. For example, this is ImageData for Canvas . The following parameters are known: image width, height, and dpi. It’s easy to display all this on canvas.
However, here is a special case: width = 5100, height = 6600 and dpi = 600.
That is, the picture is very large in itself, and here it is assumed to decrease it. Not strong in this topic, but as I understand it, you need to do the following.
- Determine the ppi of the monitor (as I understand it more correctly speak ppi), for example 96.
- Find the ratio of dpi images to the ppi monitor, for example 600/96 = 6.25.
- Divide the width and height of the image on the resulting ratio. That is 5100 / 6.25 = 816, 6600 / 6.25 = 1056.
- Display a compressed image with a new width and height.
There is a question - how to determine the ppi monitor. offsetWidth some sources on the network on this topic, I came to the conclusion that the easiest way is to create an element with a width of '1in', and then request it to offsetWidth . However, this method does not always work.
In general, now the questions:
Do I understand the rules for drawing images correctly? (relations ppi and dpi, image compression, etc.)
Are there any other ways to determine ppi? I read about media queries, but as I understood it css and did not figure out how to get this info through js. If anyone can explain in detail, I will be very grateful.
How to compress a picture? Here there is an initial height and width, and it is necessary to receive in a compressed form. How to bypass an array of pixels and what to do with them?