Completed
Push — master ( db6791...c08dac )
by Freek
03:48 queued 01:51
created

Pdf::saveAllPagesAsImages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace Spatie\PdfToImage;
4
5
use Spatie\PdfToImage\Exceptions\InvalidFormat;
6
use Spatie\PdfToImage\Exceptions\PageDoesNotExist;
7
use Spatie\PdfToImage\Exceptions\PdfDoesNotExist;
8
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)
27
    {
28
        if (!file_exists($pdfFile)) {
29
            throw new PdfDoesNotExist();
30
        }
31
32
        $this->pdfFile = $pdfFile;
33
    }
34
35
    /**
36
     * Set the raster resolution.
37
     *
38
     * @param int $resolution
39
     *
40
     * @return $this
41
     */
42
    public function setResolution($resolution)
43
    {
44
        $this->resolution = $resolution;
45
46
        return $this;
47
    }
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)
59
    {
60
        if (!$this->isValidOutputFormat($outputFormat)) {
61
            throw new InvalidFormat('Format '.$outputFormat.' is not supported');
62
        }
63
64
        $this->outputFormat = $outputFormat;
65
66
        return $this;
67
    }
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)
77
    {
78
        return in_array($outputFormat, $this->validOutputFormats);
79
    }
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)
91
    {
92
        if ($page > $this->getNumberOfPages()) {
93
            throw new PageDoesNotExist('Page '.$page.' does not exist');
94
        }
95
96
        $this->page = $page;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Get the number of pages in the pdf file.
103
     *
104
     * @return int
105
     */
106
    public function getNumberOfPages()
107
    {
108
        return (new \Imagick($this->pdfFile))->getNumberImages();
109
    }
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)
119
    {
120
        $imageData = $this->getImageData($pathToImage);
121
122
        file_put_contents($pathToImage, $imageData);
123
124
        return true;
125
    }
126
127
    /**
128
     * Save the file as images to the given directory.
129
     *
130
     * @param string $directory
131
     * @param string $prefix
132
     *
133
     * @return array $files the paths to the created images
134
     */
135
    public function saveAllPagesAsImages($directory, $prefix = '')
136
    {
137
        $numberOfPages = $this->getNumberOfPages();
138
139
        if ($numberOfPages === 0) {
140
            return [];
141
        }
142
143
        return array_map(function ($pageNumber) use ($directory, $prefix) {
144
145
            $this->setPage($pageNumber);
146
147
            $destination = "{$directory}/{$prefix}{$pageNumber}.{$this->outputFormat}";
148
149
            $this->saveImage($destination);
150
151
            return $destination;
152
153
        }, range(1, $numberOfPages));
154
    }
155
156
    /**
157
     * Return raw image data.
158
     *
159
     * @param string $pathToImage
160
     *
161
     * @return \Imagick
162
     */
163
    public function getImageData($pathToImage)
164
    {
165
        $imagick = new \Imagick();
166
167
        $imagick->setResolution($this->resolution, $this->resolution);
168
169
        $imagick->readImage(sprintf('%s[%s]', $this->pdfFile, $this->page - 1));
170
171
        $imagick->setFormat($this->determineOutputFormat($pathToImage));
172
173
        return $imagick;
174
    }
175
176
    /**
177
     * Determine in which format the image must be rendered.
178
     *
179
     * @param $pathToImage
180
     *
181
     * @return string
182
     */
183
    protected function determineOutputFormat($pathToImage)
184
    {
185
        $outputFormat = pathinfo($pathToImage, PATHINFO_EXTENSION);
186
187
        if ($this->outputFormat != '') {
188
            $outputFormat = $this->outputFormat;
189
        }
190
191
        $outputFormat = strtolower($outputFormat);
192
193
        if (!$this->isValidOutputFormat($outputFormat)) {
194
            $outputFormat = 'jpg';
195
        }
196
197
        return $outputFormat;
198
    }
199
}
200