在php脚本中使用pngquant,正确的shell命令进行PNG压缩

时间:2014-05-11 19:45:52

标签: php apache shell

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");
    }
    $min_quality = 60;
    $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;
}
$read_from_path = "pic.png";
$save_to_path = "compressed_file.png";
$compressed_png_content = compress_png($read_from_path);
file_put_contents($save_to_path, $compressed_png_content);

此代码返回错误:

  

致命错误:未捕获的异常'异常',消息'转换为压缩的PNG失败。是否在服务器上安装了pngquant 1.8+?'   堆栈跟踪:compress_png('pic.png',60)#1 {main}抛出

如何编写shell命令?以及如何安装pngquant服务器:windows server 2008 r2 sp1,php 5.5.5,apache 2.4。此处下载的Windows文件:http://pngquant.org/pngquant-windows.zip

0 个答案:

没有答案