1 | <?php |
||
9 | class Pdf |
||
10 | { |
||
11 | protected $pdfFile; |
||
12 | |||
13 | protected $resolution = 144; |
||
14 | |||
15 | protected $outputFormat = ''; |
||
16 | |||
17 | protected $page = 1; |
||
18 | |||
19 | protected $validOutputFormats = ['jpg', 'jpeg', 'png']; |
||
20 | |||
21 | /** |
||
22 | * @param string $pdfFile The path to the pdffile. |
||
23 | * |
||
24 | * @throws \Spatie\PdfToImage\Exceptions\PdfDoesNotExist |
||
25 | */ |
||
26 | public function __construct($pdfFile) |
||
34 | |||
35 | /** |
||
36 | * Set the raster resolution. |
||
37 | * |
||
38 | * @param int $resolution |
||
39 | * |
||
40 | * @return $this |
||
41 | */ |
||
42 | public function setResolution($resolution) |
||
48 | |||
49 | /** |
||
50 | * Set the output format. |
||
51 | * |
||
52 | * @param string $outputFormat |
||
53 | * |
||
54 | * @return $this |
||
55 | * |
||
56 | * @throws \Spatie\PdfToImage\Exceptions\InvalidFormat |
||
57 | */ |
||
58 | public function setOutputFormat($outputFormat) |
||
68 | |||
69 | /** |
||
70 | * Determine if the given format is a valid output format. |
||
71 | * |
||
72 | * @param $outputFormat |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function isValidOutputFormat($outputFormat) |
||
80 | |||
81 | /** |
||
82 | * Set the page number that should be rendered. |
||
83 | * |
||
84 | * @param int $page |
||
85 | * |
||
86 | * @return $this |
||
87 | * |
||
88 | * @throws \Spatie\PdfToImage\Exceptions\PageDoesNotExist |
||
89 | */ |
||
90 | public function setPage($page) |
||
100 | |||
101 | /** |
||
102 | * Get the number of pages in the pdf file. |
||
103 | * |
||
104 | * @return int |
||
105 | */ |
||
106 | public function getNumberOfPages() |
||
110 | |||
111 | /** |
||
112 | * Save the image to the given path. |
||
113 | * |
||
114 | * @param string $pathToImage |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function saveImage($pathToImage) |
||
126 | |||
127 | /** |
||
128 | * Save the file as images to the given path. |
||
129 | * |
||
130 | * @param string $storeDirectory |
||
131 | * @param string $fileName |
||
132 | * |
||
133 | * @return array $files the full path to the newly created images. |
||
134 | */ |
||
135 | public function extractAllPagesAsImages($storeDirectory, $fileName) |
||
136 | { |
||
137 | $numberOfPages = $this->getNumberOfPages(); |
||
138 | if($numberOfPages === 0) return []; |
||
139 | |||
140 | return array_map(function($pageNumber) use ($storeDirectory, $fileName){ |
||
141 | $this->setPage($pageNumber); |
||
142 | $filePath = "{$storeDirectory}/page_{$pageNumber}_{$fileName}.{$this->outputFormat}"; |
||
143 | $imageData = $this->getImageData($filePath); |
||
144 | |||
145 | file_put_contents($filePath, $imageData); |
||
146 | return $filePath; |
||
147 | }, range(1,$numberOfPages)); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Return raw image data. |
||
152 | * |
||
153 | * @param string $pathToImage |
||
154 | * |
||
155 | * @return \Imagick |
||
156 | */ |
||
157 | public function getImageData($pathToImage) |
||
169 | |||
170 | /** |
||
171 | * Determine in which format the image must be rendered. |
||
172 | * |
||
173 | * @param $pathToImage |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | protected function determineOutputFormat($pathToImage) |
||
193 | } |
||
194 |