Skip to content

Commit 45ace03

Browse files
committed
add stream support
1 parent ceb4a9f commit 45ace03

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

src/JPEGService.php

+47-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function __construct(string $bin_path = null, string $args = "--min 50 {I
4848
}
4949

5050
/**
51+
* Compress a specified JPEG file and write to a specified output path.
52+
*
5153
* @param string $input absolute path to input JPEG file
5254
* @param string $output absolute path of output JPEG file
5355
*
@@ -58,7 +60,7 @@ public function compress(string $input, string $output)
5860
$command = strtr(
5961
"{$this->bin_path} {$this->args}",
6062
[
61-
"{INPUT}" => escapeshellarg($input),
63+
"{INPUT}" => escapeshellarg($input),
6264
"{OUTPUT}" => escapeshellarg($output),
6365
]
6466
);
@@ -67,4 +69,48 @@ public function compress(string $input, string $output)
6769

6870
$process->mustRun();
6971
}
72+
73+
/**
74+
* Compress JPEG data from a given input stream and write the output data
75+
* either to a given stream, or to a temporary stream, which will be returned.
76+
*
77+
* @param resource $input input stream resource
78+
* @param resource|null $output optional output stream resource
79+
*
80+
* @return resource output stream resource (file pointer will be at the start of the stream)
81+
*
82+
* @throws ProcessFailedException on failure to execute the command-line tool
83+
*/
84+
public function compressStream($input, &$output = null)
85+
{
86+
$command = strtr(
87+
"{$this->bin_path} {$this->args}",
88+
[
89+
"{INPUT}" => "-",
90+
"{OUTPUT}" => "-",
91+
]
92+
);
93+
94+
$process = new Process($command);
95+
96+
$process->setInput($input);
97+
98+
if ($output === null) {
99+
$output = fopen("php://temp", "w");
100+
}
101+
102+
$status = $process->run(function ($type, $buffer) use ($output) {
103+
if ($type === Process::OUT) {
104+
fwrite($output, $buffer);
105+
}
106+
});
107+
108+
if ($status !== 0) {
109+
throw new ProcessFailedException($process);
110+
}
111+
112+
rewind($output);
113+
114+
return $output;
115+
}
70116
}

test/test.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require dirname(__DIR__) . '/vendor/autoload.php';
66

77
test(
8-
"compress images",
8+
"compress JPEG file",
99
function () {
1010
$service = new JPEGService();
1111

@@ -27,4 +27,30 @@ function () {
2727
}
2828
);
2929

30+
test(
31+
"compress JPEG stream",
32+
function () {
33+
$service = new JPEGService();
34+
35+
$input = fopen(__DIR__ . '/lena.jpg', "r");
36+
37+
$output = $service->compressStream($input);
38+
39+
$tmp_file = __DIR__ . '/lena-out.jpg';
40+
41+
@unlink($tmp_file); // clean up from prior test-run
42+
43+
$tmp = fopen($tmp_file, "w");
44+
45+
stream_copy_to_stream($output, $tmp);
46+
47+
fclose($tmp);
48+
49+
list($width, $height) = getimagesize($tmp_file);
50+
51+
eq($width, 512);
52+
eq($height, 512);
53+
}
54+
);
55+
3056
exit(run());

0 commit comments

Comments
 (0)