1 | <?php |
||
22 | class Ghostscript |
||
23 | { |
||
24 | /** |
||
25 | * The default binary |
||
26 | */ |
||
27 | const DEFAULT_BINARY = 'gs'; |
||
28 | |||
29 | /** |
||
30 | * The versions. |
||
31 | * |
||
32 | * @var string[] |
||
33 | */ |
||
34 | protected static $versions = []; |
||
35 | |||
36 | /** |
||
37 | * The options. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $options; |
||
42 | |||
43 | /** |
||
44 | * Create Ghostscript object. |
||
45 | * |
||
46 | * @param array $options |
||
47 | * |
||
48 | * @throws \RuntimeException |
||
49 | */ |
||
50 | 28 | public function __construct(array $options = []) |
|
51 | { |
||
52 | 28 | $this->options = $options; |
|
53 | |||
54 | 28 | if (version_compare('9.00', $this->getVersion()) > 0) { |
|
55 | 2 | throw new \RuntimeException('Ghostscript version 9.00 or higher is required'); |
|
56 | } |
||
57 | 24 | } |
|
58 | |||
59 | /** |
||
60 | * Get option. |
||
61 | * |
||
62 | * @param string $name |
||
63 | * @param mixed $default |
||
64 | * |
||
65 | * @return mixed |
||
66 | */ |
||
67 | 26 | public function getOption($name, $default = null) |
|
68 | { |
||
69 | 26 | if (array_key_exists($name, $this->options)) { |
|
70 | 10 | return $this->options[$name]; |
|
71 | } |
||
72 | |||
73 | 24 | return $default; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * Get version. |
||
78 | * |
||
79 | * @throws \RuntimeException |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | 26 | public function getVersion() |
|
84 | { |
||
85 | 26 | $binary = $this->getOption('bin', static::DEFAULT_BINARY); |
|
86 | |||
87 | 26 | if (!isset(static::$versions[$binary])) { |
|
88 | 2 | $process = new Process($binary . ' --version'); |
|
89 | 2 | $process->run(); |
|
90 | |||
91 | 2 | if (!$process->isSuccessful()) { |
|
92 | 2 | throw new \RuntimeException($process->getErrorOutput()); |
|
93 | } |
||
94 | |||
95 | static::$versions[$binary] = $process->getOutput(); |
||
96 | } |
||
97 | |||
98 | 24 | return static::$versions[$binary]; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Create arguments object. |
||
103 | * |
||
104 | * @return Arguments |
||
105 | */ |
||
106 | 16 | protected function createArguments() |
|
107 | { |
||
108 | 16 | $arguments = new Arguments(); |
|
109 | |||
110 | 16 | if ($this->getOption('quiet', true)) { |
|
111 | 16 | $arguments->addArgument('-q'); |
|
112 | 8 | } |
|
113 | |||
114 | 16 | return $arguments; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Create PDF device object. |
||
119 | * |
||
120 | * @param null|string $outputFile |
||
121 | * |
||
122 | * @return PdfWrite |
||
123 | */ |
||
124 | 8 | public function createPdfDevice($outputFile = null) |
|
125 | { |
||
126 | 8 | $device = new PdfWrite($this, $this->createArguments()); |
|
127 | $device |
||
128 | 8 | ->setSafer() |
|
129 | 8 | ->setBatch() |
|
130 | 8 | ->setNoPause(); |
|
131 | |||
132 | 8 | if (null !== $outputFile) { |
|
133 | 8 | $device->setOutputFile($outputFile); |
|
134 | 4 | } |
|
135 | |||
136 | 8 | return $device; |
|
137 | } |
||
138 | |||
139 | /** |
||
140 | * Create no display device object. |
||
141 | * |
||
142 | * @return NoDisplay |
||
143 | */ |
||
144 | 2 | public function createNoDisplayDevice() |
|
148 | |||
149 | /** |
||
150 | * Create PDF info device object. |
||
151 | * |
||
152 | * @param string $pdfInfoPath Path to toolbin/pdf_info.ps |
||
153 | * |
||
154 | * @return PdfInfo |
||
155 | */ |
||
156 | 2 | public function createPdfInfoDevice($pdfInfoPath) |
|
160 | |||
161 | /** |
||
162 | * Create bounding box info device object. |
||
163 | * |
||
164 | * @return BoundingBoxInfo |
||
165 | */ |
||
166 | 2 | public function createBoundingBoxInfoDevice() |
|
176 | } |
||
177 |