How to count DPI of an image (challenge)? a) I have used an online service where you can upload an image.

b) Afterwards, you can select a print size and it gives you information on what is actual DPI based on that print size.

c) When it gives you information if that particular DPI (dots per inch) for that print size is enough quality to print, so basically it outputs "bad", "average" or "good".

I want to built this functionality on my own site, but I lack logic's behind how they count DPI.

This is my two tests where I upload slightly different photos. As site outputs DPI it is easy to try to get how it counts it, but as for different photos it does differently. I just can not grasp the pattern on why it does so.

Here is my two different photos uploaded:

Product: Poster
Size: 1000x1200

I will just explain the first value - 8x10 -> 1200 / 10  -  you get DPI by subtracting photo height with print size height.

8x10 -&gt; 1200 / 10   -&gt;&gt;&gt; why this DPI is counted differently from other image?     
10x10 -&gt; 1000 / 10                
12x12 -&gt; 1000 / 12               
12x16 -&gt; 1200 / 16  -&gt;&gt;&gt; why this DPI is counted differently from other image?     
14x14 -&gt; 1000 / 14                
16x16 -&gt; 1000 / 16                
12x18 -&gt; 1200 / 18                
18x18 -&gt; 1000 / 18                
16x20 -&gt; 1200 / 20  -&gt;&gt;&gt; why this DPI is counted differently from other image?     
18x24 -&gt; 1200 / 24  -&gt;&gt;&gt; why this DPI is counted differently from other image?     
24x36 -&gt; 1200 / 36   


Product: Poster
Size: 1000x1333

8x10 -&gt; 1000 / 8    -&gt;&gt;&gt; comparing to this value 
10x10 -&gt; 1000 / 10
12x12 -&gt; 1000 / 12
12x16 -&gt; 1000 / 12  -&gt;&gt;&gt; comparing to this value   
14x14 -&gt; 1000 / 14
16x16 -&gt; 1000 / 16
12x18 -&gt; 1333 / 18                   
18x18 -&gt; 1000 / 18
16x20 -&gt; 1000 / 20  -&gt;&gt;&gt; comparing to this value 
18x24 -&gt; 1000 / 18  -&gt;&gt;&gt; comparing to this value 
24x36 -&gt; 1333 / 36    


Just do not get it why for a very similar image, counting of the same print sizes are different for particular values?

Any ideas?
They are not "very similar images". They have quite different aspect ratios. The first one has a 5:6 aspect ratio, while the other has a 3:4 ratio.

If you try to print a 5:6 image at 8x10, what you find is that if the width is 8, then a 5:6 image would have a height of 9.6, which is less than 10. So that means the 5:6 image can fit into the 8x10 region with its full width; some of the height will go unused.

However, a 3:4 aspect ratio image at 8x10 doesn't do that. If the width is 8, then the 3:4 aspect ratio would require a height of 10.6. That would exceed the maximum available height, so the width cannot be full width. Instead, it uses the full height of 10, and the width used will only be 7.5.

You will find that this is the case for all of the ones where you noted such a difference.

The "DPI" its computing is essentially the number of pixels in the dimension that isn't the maximum. For the 3:4 image at 8x10, it takes the image width (1000) and divides by the area width (8). For the 5:6 image at 8x10, it takes the image height (1200) and divided by the area height (10).

This number only makes sense if we assume that the image is being scaled independently in width and height to fit the page. That is, the aspect ratio of the image is not being preserved when printed. So a 5:6 image is being transformed into a 4:5 aspect ratio image when printed at 8x10.

Since each dimension is being scaled differently, each dimension has its own DPI. The lowest DPI will be for the dimension being stretched the most. And that's what this formula seems to be computing.

Of course... who cares? Stretching images creates obvious visual distortion, and should pretty much never be done when printing something you actually want to have a paper copy of. So I would declare this computation algorithm to be bogus and move to something that actually makes sense with a reasonable printing methodology.