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 | /** |
||
34 | * @param string $pdfFile The path or url to the pdffile. |
||
35 | * |
||
36 | * @throws \Spatie\PdfToImage\Exceptions\PdfDoesNotExist |
||
37 | */ |
||
38 | public function __construct($pdfFile) |
||
50 | |||
51 | /** |
||
52 | * Set the raster resolution. |
||
53 | * |
||
54 | * @param int $resolution |
||
55 | * |
||
56 | * @return $this |
||
57 | */ |
||
58 | public function setResolution($resolution) |
||
64 | |||
65 | /** |
||
66 | * Set the output format. |
||
67 | * |
||
68 | * @param string $outputFormat |
||
69 | * |
||
70 | * @return $this |
||
71 | * |
||
72 | * @throws \Spatie\PdfToImage\Exceptions\InvalidFormat |
||
73 | */ |
||
74 | public function setOutputFormat($outputFormat) |
||
84 | |||
85 | /** |
||
86 | * Get the output format. |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | public function getOutputFormat() |
||
94 | |||
95 | /** |
||
96 | * Sets the layer method for Imagick::mergeImageLayers() |
||
97 | * If int, should correspond to a predefined LAYERMETHOD constant. |
||
98 | * If null, Imagick::mergeImageLayers() will not be called. |
||
99 | * |
||
100 | * @param int|null |
||
101 | * |
||
102 | * @return $this |
||
103 | * |
||
104 | * @throws \Spatie\PdfToImage\Exceptions\InvalidLayerMethod |
||
105 | * |
||
106 | * @see https://secure.php.net/manual/en/imagick.constants.php |
||
107 | * @see Pdf::getImageData() |
||
108 | */ |
||
109 | public function setLayerMethod($layerMethod) |
||
122 | |||
123 | /** |
||
124 | * Determine if the given format is a valid output format. |
||
125 | * |
||
126 | * @param $outputFormat |
||
127 | * |
||
128 | * @return bool |
||
129 | */ |
||
130 | public function isValidOutputFormat($outputFormat) |
||
134 | |||
135 | /** |
||
136 | * Set the page number that should be rendered. |
||
137 | * |
||
138 | * @param int $page |
||
139 | * |
||
140 | * @return $this |
||
141 | * |
||
142 | * @throws \Spatie\PdfToImage\Exceptions\PageDoesNotExist |
||
143 | */ |
||
144 | public function setPage($page) |
||
154 | |||
155 | /** |
||
156 | * Get the number of pages in the pdf file. |
||
157 | * |
||
158 | * @return int |
||
159 | */ |
||
160 | public function getNumberOfPages() |
||
164 | |||
165 | /** |
||
166 | * Save the image to the given path. |
||
167 | * |
||
168 | * @param string $pathToImage |
||
169 | * |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function saveImage($pathToImage) |
||
182 | |||
183 | /** |
||
184 | * Save the file as images to the given directory. |
||
185 | * |
||
186 | * @param string $directory |
||
187 | * @param string $prefix |
||
188 | * |
||
189 | * @return array $files the paths to the created images |
||
190 | */ |
||
191 | public function saveAllPagesAsImages($directory, $prefix = '') |
||
209 | |||
210 | /** |
||
211 | * Return raw image data. |
||
212 | * |
||
213 | * @param string $pathToImage |
||
214 | * |
||
215 | * @return \Imagick |
||
216 | */ |
||
217 | public function getImageData($pathToImage) |
||
249 | |||
250 | /** |
||
251 | * @param int $colorspace |
||
252 | * |
||
253 | * @return $this |
||
254 | */ |
||
255 | public function setColorspace(int $colorspace) |
||
261 | |||
262 | /** |
||
263 | * @param int $compressionQuality |
||
264 | * |
||
265 | * @return $this |
||
266 | */ |
||
267 | public function setCompressionQuality(int $compressionQuality) |
||
273 | |||
274 | /** |
||
275 | * Return remote raw image data. |
||
276 | * |
||
277 | * @param string $pathToImage |
||
278 | * |
||
279 | * @return \Imagick |
||
280 | */ |
||
281 | protected function getRemoteImageData($pathToImage) |
||
295 | |||
296 | /** |
||
297 | * Determine in which format the image must be rendered. |
||
298 | * |
||
299 | * @param $pathToImage |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | protected function determineOutputFormat($pathToImage) |
||
319 | } |
||
320 |