问题
Pngquant has the following example for 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));
I want to replace $path_of_file
with the actual content.
This will avoid wasting I/O when converting a file from one format to png and then optimize it
What will be the new shell_exec()
command in that situation
回答1:
I am no PHP expert, but I believe you are looking for a 2-way pipe (write and read) to another process, so that you can write data to its stdin and read data from its stdout. So, I think that means you need proc_open()
which is described here.
It will look something like this (untested):
$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);
}
来源:https://stackoverflow.com/questions/37838561/content-instead-of-path-in-shell