Skip to content

Commit 43fec28

Browse files
committed
Support local file instance as output file instead of stdout
1 parent 054fa5b commit 43fec28

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

src/Ghostscript.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace RobGridley\Ghostscript;
44

5-
use Symfony\Component\Process\Process;
65
use RobGridley\Ghostscript\Contracts\Device;
7-
use RobGridley\Ghostscript\Contracts\LocalFile;
86
use RobGridley\Ghostscript\Contracts\DownScaling;
7+
use RobGridley\Ghostscript\Contracts\LocalFile;
8+
use Symfony\Component\Process\Process;
99

1010
class Ghostscript
1111
{
@@ -175,9 +175,10 @@ public function setFitPage(int $width, int $height)
175175
*
176176
* @param LocalFile $file
177177
* @param int $page
178+
* @param LocalFile|null $outputFile
178179
* @return string
179180
*/
180-
public function convert(LocalFile $file, int $page = 1): string
181+
public function convert(LocalFile $file, int $page = 1, LocalFile $outputFile = null): string
181182
{
182183
$command[] = $this->path;
183184
array_push($command, ...$this->device->getArguments());
@@ -205,7 +206,11 @@ public function convert(LocalFile $file, int $page = 1): string
205206
$command[] = '-dBATCH';
206207
$command[] = '-dNOPAUSE';
207208
$command[] = '-dSAFER';
208-
$command[] = '-sOutputFile=%stdout';
209+
if (is_null($outputFile)) {
210+
$command[] = '-sOutputFile=%stdout';
211+
} else {
212+
$command[] = "-sOutputFile={$outputFile->getLocalPath()}";
213+
}
209214
$command[] = '-sstdout=%stderr';
210215
$command[] = '-q';
211216
$command[] = $file->getLocalPath();

src/VirtualFile.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace RobGridley\Ghostscript;
44

5-
use RuntimeException;
65
use RobGridley\Ghostscript\Contracts\LocalFile;
6+
use RuntimeException;
77

88
class VirtualFile implements LocalFile
99
{
@@ -17,16 +17,18 @@ class VirtualFile implements LocalFile
1717
/**
1818
* Create a new temp file instance.
1919
*
20-
* @param string|array|resource $data
20+
* @param string|array|resource|null $data
2121
*/
22-
public function __construct($data)
22+
public function __construct($data = null)
2323
{
2424
if (false === $path = tempnam(sys_get_temp_dir(), 'gs')) {
2525
throw new RuntimeException('Failed to create the temp file');
2626
}
2727

28-
if (false === file_put_contents($path, $data)) {
29-
throw new RuntimeException('Failed to write data to the temp file');
28+
if (!is_null($data)) {
29+
if (false === file_put_contents($path, $data)) {
30+
throw new RuntimeException('Failed to write data to the temp file');
31+
}
3032
}
3133

3234
$this->path = $path;

0 commit comments

Comments
 (0)