1 | <?php |
||
23 | class Ghostscript |
||
24 | { |
||
25 | /** |
||
26 | * The default binary. |
||
27 | */ |
||
28 | const DEFAULT_BINARY = 'gs'; |
||
29 | |||
30 | /** |
||
31 | * The versions. |
||
32 | * |
||
33 | * @var string[] |
||
34 | */ |
||
35 | protected static $versions = []; |
||
36 | |||
37 | /** |
||
38 | * The options. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $options; |
||
43 | |||
44 | /** |
||
45 | * Create Ghostscript object. |
||
46 | * |
||
47 | * @param array $options |
||
48 | * |
||
49 | * @throws \RuntimeException |
||
50 | */ |
||
51 | 30 | public function __construct(array $options = []) |
|
52 | { |
||
53 | 30 | $this->options = $options; |
|
54 | |||
55 | 30 | if (version_compare('9.00', $this->getVersion()) > 0) { |
|
56 | 2 | throw new \RuntimeException('Ghostscript version 9.00 or higher is required'); |
|
57 | } |
||
58 | 26 | } |
|
59 | |||
60 | /** |
||
61 | * Get option. |
||
62 | * |
||
63 | * @param string $name |
||
64 | * @param mixed $default |
||
65 | * |
||
66 | * @return mixed |
||
67 | */ |
||
68 | 28 | public function getOption($name, $default = null) |
|
69 | { |
||
70 | 28 | if (array_key_exists($name, $this->options)) { |
|
71 | 12 | return $this->options[$name]; |
|
72 | } |
||
73 | |||
74 | 26 | return $default; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Get process to identify version. |
||
79 | * |
||
80 | * @param string $binary |
||
81 | * |
||
82 | * @return Process |
||
83 | */ |
||
84 | 2 | protected function createProcessToIdentifyVersion($binary) |
|
88 | |||
89 | /** |
||
90 | * Get version. |
||
91 | * |
||
92 | * @throws \RuntimeException |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 28 | public function getVersion() |
|
97 | { |
||
98 | 28 | $binary = $this->getOption('bin', static::DEFAULT_BINARY); |
|
99 | |||
100 | 28 | if (!isset(static::$versions[$binary])) { |
|
101 | 2 | $process = $this->createProcessToIdentifyVersion($binary); |
|
102 | 2 | $process->run(); |
|
103 | |||
104 | 2 | if (!$process->isSuccessful()) { |
|
105 | 2 | throw new \RuntimeException($process->getErrorOutput()); |
|
106 | } |
||
107 | |||
108 | static::$versions[$binary] = $process->getOutput(); |
||
109 | } |
||
110 | |||
111 | 26 | return static::$versions[$binary]; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Create arguments object. |
||
116 | * |
||
117 | * @return Arguments |
||
118 | */ |
||
119 | 18 | protected function createArguments() |
|
129 | |||
130 | /** |
||
131 | * Create PDF device object. |
||
132 | * |
||
133 | * @param null|string $outputFile |
||
134 | * |
||
135 | * @return PdfWrite |
||
136 | */ |
||
137 | 8 | public function createPdfDevice($outputFile = null) |
|
153 | |||
154 | /** |
||
155 | * Create no display device object. |
||
156 | * |
||
157 | * @return NoDisplay |
||
158 | */ |
||
159 | 2 | public function createNoDisplayDevice() |
|
163 | |||
164 | /** |
||
165 | * Create PDF info device object. |
||
166 | * |
||
167 | * @param string $pdfInfoPath Path to toolbin/pdf_info.ps |
||
168 | * |
||
169 | * @return PdfInfo |
||
170 | */ |
||
171 | 2 | public function createPdfInfoDevice($pdfInfoPath) |
|
175 | |||
176 | /** |
||
177 | * Create bounding box info device object. |
||
178 | * |
||
179 | * @return BoundingBoxInfo |
||
180 | */ |
||
181 | 2 | public function createBoundingBoxInfoDevice() |
|
191 | |||
192 | /** |
||
193 | * Create inkcov device object |
||
194 | * |
||
195 | * @return Inkcov |
||
196 | */ |
||
197 | 2 | public function createInkcovDevice() |
|
209 | } |
||
210 |