Detect JPEG image quality

后端 未结 7 883
借酒劲吻你
借酒劲吻你 2021-02-10 10:25

I allow users to upload images. However, I want to keep JPEG quality not more than 90%. What I plan to do is to detect the current quality: - If less than 90% do nothing - If mo

7条回答
  •  野性不改
    2021-02-10 10:44

    whats up? I was facing the same problem with an app I'm developing... My problem is that, I extract several images from a random site and each item have several images, i would like to show a single image for each item, and bring to the user the best quality image.

    I came out with this idea, its pretty simple, and will work for any language and any type of compression:

    //Parameters you will need to retrieve from image
    $width = 0; 
    $height = 0;
    $filesize = 0;
    
    //Quality answer for your image
    $quality = (101-(($width*$height)*3)/$filesize);
    

    I ran this algorithm against the http://fotoforensics.com/tutorial-estq.php mentioned above and here are the results:

    Filename            Width   Height  Pixels  BitmapBytes FileBytes   Quality 
    estq-baseline.png   400     300     120000  360000      163250      98,79
    estq-90.jpg         400     300     120000  360000      34839       90,67
    estq-80.jpg         400     300     120000  360000      24460       86,28
    estq-70.jpg         400     300     120000  360000      19882       82,89
    estq-25.jpg         400     300     120000  360000      10300       66,05
    

    The basic idea behind this algorithm is compare the size that an image could reach if written in a bitmap way (without any compression, 3 bytes per pixel, 3 bytes for RGB) to the size that this image is currently using. The smaller is the image size, the higher is the compression, independently of the compression method used, rather its a JPG, a PNG or whatever, and that will lead us into having a bigger gap or smaller gap between uncompressed and compressed image.

    It is also important to mention, that this is a mathematical solution for comparison purposes, this method will not return the actually quality of the image, it will answer the distance percentage between uncompressed and compressed sizes!

    if you need more details, you can send me an email: rafaelkarst@gmail.com

提交回复
热议问题