Skip to content

Instantly share code, notes, and snippets.

@lackneets
Created June 5, 2016 08:14
Show Gist options
  • Save lackneets/84866726e32cbb79bfe375bf569724ef to your computer and use it in GitHub Desktop.
Save lackneets/84866726e32cbb79bfe375bf569724ef to your computer and use it in GitHub Desktop.
Example of PHP streaming pngquant
<?php
$new_path = '/path/to/input.png';
$cmd = 'pngquant -';
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
$process = proc_open($cmd, $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], file_get_contents($new_path)); // file_get_contents('php://stdin')
fclose($pipes[0]);
$content = stream_get_contents($pipes[1]);
fclose($pipes[1]);
file_put_contents($new_path . '-min.png', $content);
$return_value = proc_close($process);
header('Content-type: image/png');
echo $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment