0

Pngquant 有以下 php 示例

// '-' 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));

我想$path_of_file用实际内容替换。

这将避免在将文件从一种格式转换为 png 并对其进行优化时浪费 I/O

在那种情况下新的shell_exec()命令是什么

4

1 回答 1

0

我不是 PHP 专家,但我相信您正在寻找到另一个进程的 2 路管道(写入和读取),以便您可以将数据写入其stdin并从其stdout读取数据。所以,我认为这意味着你需要这里proc_open()描述的内容。

它看起来像这样(未经测试):

$cmd = 'pngquant --quality ... -';

$spec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w"));

$process = proc_open($cmd, $spec, $pipes);

if (is_resource($process)) 
{

    // write your data to $pipes[0] so that "pngquant" gets it
    fclose($pipes[0]);

    $result=stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    proc_close($process);
}
于 2016-06-16T15:05:16.550 回答