1 | <?php |
||
11 | class Pdf |
||
12 | { |
||
13 | protected $pdfFile; |
||
14 | |||
15 | protected $resolution = 144; |
||
16 | |||
17 | protected $outputFormat = 'jpg'; |
||
18 | |||
19 | protected $page = 1; |
||
20 | |||
21 | public $imagick; |
||
22 | |||
23 | protected $numberOfPages; |
||
24 | |||
25 | protected $validOutputFormats = ['jpg', 'jpeg', 'png']; |
||
26 | |||
27 | protected $layerMethod = Imagick::LAYERMETHOD_FLATTEN; |
||
28 | |||
29 | protected $colorspace; |
||
30 | |||
31 | protected $compressionQuality; |
||
32 | |||
33 | protected $fileName; |
||
34 | |||
35 | /** |
||
36 | * @param string $pdfFile The path or url to the pdffile. |
||
37 | * |
||
38 | * @throws \Spatie\PdfToImage\Exceptions\PdfDoesNotExist |
||
39 | */ |
||
40 | public function __construct($pdfFile) |
||
52 | |||
53 | /** |
||
54 | * Set the raster resolution. |
||
55 | * |
||
56 | * @param int $resolution |
||
57 | * |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function setResolution($resolution) |
||
66 | |||
67 | /** |
||
68 | * Set the output format. |
||
69 | * |
||
70 | * @param string $outputFormat |
||
71 | * |
||
72 | * @return $this |
||
73 | * |
||
74 | * @throws \Spatie\PdfToImage\Exceptions\InvalidFormat |
||
75 | */ |
||
76 | public function setOutputFormat($outputFormat) |
||
86 | |||
87 | /** |
||
88 | * Get the output format. |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | public function getOutputFormat() |
||
96 | |||
97 | /** |
||
98 | * Sets the layer method for Imagick::mergeImageLayers() |
||
99 | * If int, should correspond to a predefined LAYERMETHOD constant. |
||
100 | * If null, Imagick::mergeImageLayers() will not be called. |
||
101 | * |
||
102 | * @param int|null |
||
103 | * |
||
104 | * @return $this |
||
105 | * |
||
106 | * @throws \Spatie\PdfToImage\Exceptions\InvalidLayerMethod |
||
107 | * |
||
108 | * @see https://secure.php.net/manual/en/imagick.constants.php |
||
109 | * @see Pdf::getImageData() |
||
110 | */ |
||
111 | public function setLayerMethod($layerMethod) |
||
124 | |||
125 | /** |
||
126 | * Determine if the given format is a valid output format. |
||
127 | * |
||
128 | * @param $outputFormat |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function isValidOutputFormat($outputFormat) |
||
136 | |||
137 | /** |
||
138 | * Set the page number that should be rendered. |
||
139 | * |
||
140 | * @param int $page |
||
141 | * |
||
142 | * @return $this |
||
143 | * |
||
144 | * @throws \Spatie\PdfToImage\Exceptions\PageDoesNotExist |
||
145 | */ |
||
146 | public function setPage($page) |
||
156 | |||
157 | /** |
||
158 | * Get the number of pages in the pdf file. |
||
159 | * |
||
160 | * @return int |
||
161 | */ |
||
162 | public function getNumberOfPages() |
||
166 | |||
167 | |||
168 | /** |
||
169 | * Defines a custom name to save the file |
||
170 | * |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function setFileName($fileName) |
||
179 | |||
180 | /** |
||
181 | * Returns the current file name that is going to be used to save the file |
||
182 | * |
||
183 | * @return string|null |
||
184 | */ |
||
185 | public function getFileName() |
||
189 | |||
190 | /** |
||
191 | * Save the image to the given path. |
||
192 | * |
||
193 | * @param string $pathToImage |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | public function saveImage($pathToImage) |
||
209 | |||
210 | /** |
||
211 | * Save the file as images to the given directory. |
||
212 | * |
||
213 | * @param string $directory |
||
214 | * @param string $prefix |
||
215 | * |
||
216 | * @return array $files the paths to the created images |
||
217 | */ |
||
218 | public function saveAllPagesAsImages($directory, $prefix = '') |
||
236 | |||
237 | /** |
||
238 | * Return raw image data. |
||
239 | * |
||
240 | * @param string $pathToImage |
||
241 | * |
||
242 | * @return \Imagick |
||
243 | */ |
||
244 | public function getImageData($pathToImage) |
||
276 | |||
277 | /** |
||
278 | * @param int $colorspace |
||
279 | * |
||
280 | * @return $this |
||
281 | */ |
||
282 | public function setColorspace(int $colorspace) |
||
288 | |||
289 | /** |
||
290 | * @param int $compressionQuality |
||
291 | * |
||
292 | * @return $this |
||
293 | */ |
||
294 | public function setCompressionQuality(int $compressionQuality) |
||
300 | |||
301 | /** |
||
302 | * Return remote raw image data. |
||
303 | * |
||
304 | * @param string $pathToImage |
||
305 | * |
||
306 | * @return \Imagick |
||
307 | */ |
||
308 | protected function getRemoteImageData($pathToImage) |
||
322 | |||
323 | /** |
||
324 | * Determine in which format the image must be rendered. |
||
325 | * |
||
326 | * @param $pathToImage |
||
327 | * |
||
328 | * @return string |
||
329 | */ |
||
330 | protected function determineOutputFormat($pathToImage) |
||
346 | } |
||
347 |