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

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

展开原文

原文

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?

doutong1890
doutong1890 您可以使用Imagick(PHP绑定到ImageMagick),然后使用$imagick->setOption('jpeg:extent',200000);
大约一年之前 回复
dongzhimin2231
dongzhimin2231 或者你可以“炮轰”到ImageMagick并执行此操作...stackoverflow.com/a/29549024/2836621
大约一年之前 回复
douxian8883
douxian8883 你不需要遍历从100到0的所有大小,你可以像这样使用二进制搜索...stackoverflow.com/a/52281257/2836621
大约一年之前 回复

2个回答



事实上,没有任何方法可以预测压缩程度。 压缩的效果取决于图像源。 其中一个问题是有无数的JPEG压缩设置。 </ p>


  1. 量化表(最多3个),包含64个不同的值。</ li>
  2. 子采样。</ li>
  3. 逐行扫描中的光谱选择。</ li>
  4. 逐行扫描中的逐次逼近。</ li>
  5. 优化霍夫曼表格。</ li>
    </ ol>

    因此,您可以设置数十亿个参数。</ p>

    有一些JPEG优化应用程序可以查看压缩数据。 </ p>
    </ div>

展开原文

原文

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.

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐