Completed
Push — master ( 6c9d64...6d4332 )
by Freek
01:56
created

Pdf::saveAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
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
        $imagick = $this->getImageData($pathToImage);
121
122
        file_put_contents($pathToImage, $imagick);
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 full path to the newly created images.
134
     */
135
    public function saveAll($directory, $prefix = '')
136
    {
137
        $numberOfPages = $this->getNumberOfPages();
138
        if($numberOfPages === 0) return [];
139
140
        return array_map(function($pageNumber) use ($directory, $prefix){
141
            $this->setPage($pageNumber);
142
            
143
            $filePath = "{$directory}/{$prefix}{$pageNumber}.{$this->outputFormat}";
144
            $imageData = $this->getImageData($filePath);
145
146
            file_put_contents($filePath, $imageData);
147
            return $filePath;
148
        }, range(1,$numberOfPages));
149
    }
150
151
    /**
152
     * Return raw image data.
153
     *
154
     * @param  string $pathToImage
155
     *
156
     * @return \Imagick
157
     */
158
    public function getImageData($pathToImage)
159
    {
160
        $imagick = new \Imagick();
161
162
        $imagick->setResolution($this->resolution, $this->resolution);
163
164
        $imagick->readImage(sprintf('%s[%s]', $this->pdfFile, $this->page - 1));
165
166
        $imagick->setFormat($this->determineOutputFormat($pathToImage));
167
168
        return $imagick;
169
    }
170
171
    /**
172
     * Determine in which format the image must be rendered.
173
     *
174
     * @param $pathToImage
175
     *
176
     * @return string
177
     */
178
    protected function determineOutputFormat($pathToImage)
179
    {
180
        $outputFormat = pathinfo($pathToImage, PATHINFO_EXTENSION);
181
182
        if ($this->outputFormat != '') {
183
            $outputFormat = $this->outputFormat;
184
        }
185
186
        $outputFormat = strtolower($outputFormat);
187
188
        if (!$this->isValidOutputFormat($outputFormat)) {
189
            $outputFormat = 'jpg';
190
        }
191
192
        return $outputFormat;
193
    }
194
}
195