【问题标题】:How to use pngquant for compressing jpeg files on ubuntu server using php如何使用 pngquant 在 ubuntu 服务器上使用 php 压缩 jpeg 文件
【发布时间】:2016-07-09 08:04:24
【问题描述】:

我在上传到我的服务器时尝试使用 pngquant 来压缩图像(jpeg 和 png)。我从 pngquant 网站获得的脚本适用于 png 文件,但不适用于 jpeg 文件。我正在使用此代码(适用于 png 文件):

function compress_png($path_to_png_file, $max_quality = 90)
{
    if (!file_exists($path_to_png_file)) {
        throw new Exception("File does not exist: $path_to_png_file");
    }

    // guarantee that quality won't be worse than that.
    $min_quality = 60;

    // '-' makes it use stdout, required to save to $compressed_png_content variable
    // '<' makes it read from the given file path
    // escapeshellarg() makes this safe to use with any path
    $compressed_png_content = shell_exec("pngquant --quality=$min_quality-$max_quality - < ".escapeshellarg(    $path_to_png_file));

    if (!$compressed_png_content) {
        throw new Exception("Conversion to compressed PNG failed. Is pngquant 1.8+ installed on the server?");
    }

    return $compressed_png_content;
}

/****************************************************/


$download_url = "https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png";


    // maximum execution time in seconds
    set_time_limit (24 * 60 * 60);

    // folder to save downloaded files to. must end with slash
    $destination_folder = '/IMAGES/';

    $ImageName = "testing_pngquant_image3.png";
    $CompressedImageName = "testing_pngquant_image_compressed3.png";

    $url = $download_url;
    $newfname = $destination_folder . $ImageName;

    $file = fopen ($url, "rb");
    if ($file) {
      $newf = fopen ($newfname, "wb");

      if ($newf)
      while(!feof($file)) {
        fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
      }
    }

    if ($file) {
      fclose($file);
    }

    if ($newf) {
      fclose($newf);
    }



    $path_to_uncompressed_file = $newfname;
    $path_to_compressed_file = $destination_folder . $CompressedImageName;

// this will ensure that $path_to_compressed_file points to compressed file
// and avoid re-compressing if it's been done already
if (!file_exists($path_to_compressed_file)) {
    file_put_contents($path_to_compressed_file, compress_png($path_to_uncompressed_file));
}

// and now, for example, you can output the compressed file:
header("Content-Type: image/png");
header('Content-Length: '.filesize($path_to_compressed_file));
readfile($path_to_compressed_file);
?>

pngquant 可以用于 jpeg 文件吗?或者有没有更好的(免费)工具来压缩 jpeg 文件(比如付费的 jpegmini)来使用 php 在我的 ubuntu 服务器上执行此操作。

【问题讨论】:

    标签: php image ubuntu pngquant


    【解决方案1】:

    据我所知,Pngquant 用于以有损方式压缩 png 格式。对于 jpeg,您可以在输出时使用品质因子。

    【讨论】:

      【解决方案2】:

      我很确定 pngquant 仅适用于 PNG 文件。有许多类似的实用程序可用于 JPG。您可能只想考虑使用现有的脚本 imgopt(bash shell 脚本)。它位于这里:imgopt

      此脚本运行良好(JPG 和 PNG)并且非常易于使用。它是完全无损的,因此相当安全,但是如果需要,您应该可以在其中添加 pngquant(pngquant 是有损的)。您需要先安装一些必需的程序,如果您选择使用它,还需要安装一个二进制文件 (pngout)。

      脚本不是我写的,我只是用了一段时间。

      【讨论】:

        猜你喜欢
        • 2012-06-07
        • 1970-01-01
        • 1970-01-01
        • 2015-11-17
        • 2011-07-07
        • 1970-01-01
        • 1970-01-01
        • 2021-10-05
        • 1970-01-01
        相关资源
        最近更新 更多