dongmeng2687
2019-07-14 16:31
采纳率: 0%
浏览 245

如何使用php中的imagejpeg在Kb中设置特定压缩

I'm trying to compress uploaded images down to a specific size of 200Kb. I don't want to compress them any more than they need to be and using lossless compression like PNG isn't enough. Simply setting it to imagejpeg($image, null, 40) creates different compressed sizes for different images. Is there a way to set the desired compression size in bytes or at least have some algorithm that can find out the compression output without looping through imagejpeg() from 100 to 0 quality?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将上传的图像压缩到特定大小200Kb。 我不想压缩它们比它们需要的更多,并且使用像PNG这样的无损压缩是不够的。 只需将其设置为 imagejpeg($ image,null,40)即可为不同的图像创建不同的压缩大小。 有没有办法以字节为单位设置所需的压缩大小,或者至少有一些算法可以找出压缩输出而无需通过 imagejpeg()从100到0质量循环?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dpp3047 2019-07-15 03:49

    There really isn't any way to predict the level of compression beforehand. The effect of compression depends upon the image source. One of the problems is that there is a myriad of JPEG compression settings.

    1. The quantization tables (up to 3) with 64 different values.
    2. Sub sampling.
    3. Spectral selection in progressive scans.
    4. Successive Approximation in progressive scans.
    5. Optimize huffman tables.

    So there are billions upon billions of parameters that you can set.

    There are JPEG optimization applications out there that will look at the compressed data.

    点赞 评论
  • dongyanggan3025 2019-07-15 11:30

    I found a way to use ob to view the file size of the image before it is uploaded so I used it in a loop

    // Get get new image data
        ob_start();
        // Build image with minimal campression
        imagejpeg($newImage, NULL, 100);
        // Get the size of the image file in bytes
        $size = ob_get_length();
        // Save new image into a variable
        $compressedImage = addslashes(ob_get_contents());
        // Clear memory
        ob_end_clean();
    
        // If image is larger than 200Kb
        if ($size > 200000) {
          // This variable will decrease by 2 every loop to try most combinations 
          // from least compressed to most compressed
          $compressionValue = 100;
          for ($i=0; $i < 50; $i++) {
            $compressionValue = $compressionValue - 2;
            ob_start();
            imagejpeg($newImage, NULL, $compressionValue);
            $size = ob_get_length();
            // Overwrite old compressed image with the new compressed image
            $compressedImage = addslashes(ob_get_contents());
            // Clear memory
            ob_end_clean();
            // If image is less than or equal to 200.5Kb stop the loop
            if ($size <= 200500) {
              break;
            }
          }
        }
    

    This is incredibly well optimized on its own too. The whole process only takes a few milliseconds with a 1.5Mb starting image even when it is trying 50 combinations.

    点赞 评论

相关推荐 更多相似问题