<p>Here are some ways to achieve your (1) and get it right.</p>
<ol>
<li><p>There are ways to do this by fitting to the quantization tables. Sherloq - for example - does this:</p>
<p><a href="https://github.com/GuidoBartoli/sherloq" rel="nofollow noreferrer">https://github.com/GuidoBartoli/sherloq</a></p>
<p>The relevant (python) code is at <a href="https://github.com/GuidoBartoli/sherloq/blob/master/gui/quality.py" rel="nofollow noreferrer">https://github.com/GuidoBartoli/sherloq/blob/master/gui/quality.py</a></p>
</li>
<li><p>There is another algorithm written up in <a href="https://arxiv.org/abs/1802.00992" rel="nofollow noreferrer">https://arxiv.org/abs/1802.00992</a> - you might consider contacting the author for any code etc.</p>
</li>
<li><p>You can also simulate <code>file_size(image_dimensions,quality_level)</code> and then invert that function/lookup table to get <code>quality_level(image_dimensions,file_size)</code>. Hey presto!</p>
</li>
<li><p>Finally, you can adopt a brute-force <a href="https://en.wikipedia.org/wiki/Error_level_analysis" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Error_level_analysis</a> approach by calculating the difference between the original image and recompressed versions each saved at a different quality level. The quality level of the original is roughly the one for which the difference is minimized. Seems to work reasonably well (but is linear in the for-loop..).</p>
</li>
</ol>
<p>Most often the quality factor used seems to be 75 or 95 which might help you to get to the result faster. Probably no-one would save a JPEG at 100. Probably no-one would usefully save it at &lt; 60 either.</p>
<p>I can add other links for this as they become available - please put them in the comments.</p>
Is it possible to tell the quality level of a JPEG?
<p>This is really a two part question, since I don't fully understand how these things work just yet:</p>

<p>My situation: I'm writing a web app which lets the user upload an image. My app then resizes to something displayable (eg: 640x480-ish) and saves the file for use later.</p>

<p>My questions:</p>

<ol>
<li>Given an arbitrary JPEG file, is it possible to tell what the quality level is, so that I can use that same quality when saving the resized image?</li>
<li>Does this even matter?? Should I be saving all the images at a decent level (eg: 75-80), regardless of the original quality?</li>
</ol>

<p>I'm not so sure about this because, as I figure it: (let's take an extreme example), if someone had a 5 megapixel image saved at quality 0, it would be blocky as anything. Reducing the image size to 640x480, the blockiness would be smoothed out and <strike>barely</strike> less noticeable... until I saved it with quality 0 again...</p>

<p>On the other end of the spectrum, if there was an image which was 800x600 with q=0, resizing to 640x480 isn't going to change the fact that it looks like utter crap, so saving with q=80 would be redundant.</p>

<p>Am I even close?</p>

<p><em>I'm using GD2 library on PHP if that is of any use</em></p>