Completed
Pull Request — master (#7)
by Lasse
02:04
created

Pdf::setOutputColorSpace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Spatie\PdfToImage;
4
5
use Imagick;
6
use Spatie\PdfToImage\Exceptions\InvalidColorSpace;
7
use Spatie\PdfToImage\Exceptions\InvalidFormat;
8
use Spatie\PdfToImage\Exceptions\PageDoesNotExist;
9
use Spatie\PdfToImage\Exceptions\PdfDoesNotExist;
10
11
class Pdf
12
{
13
    protected $pdfFile;
14
15
    protected $resolution = 144;
16
17
    protected $outputFormat = '';
18
19
    protected $outputColorSpace = Imagick::COLORSPACE_RGB;
20
21
    protected $page = 1;
22
23
    protected $validOutputFormats = ['jpg', 'jpeg', 'png'];
24
25
    /**
26
     * @param string $pdfFile The path to the pdffile.
27
     *
28
     * @throws \Spatie\PdfToImage\Exceptions\PdfDoesNotExist
29
     */
30
    public function __construct($pdfFile)
31
    {
32
        if (!file_exists($pdfFile)) {
33
            throw new PdfDoesNotExist();
34
        }
35
36
        $this->pdfFile = $pdfFile;
37
    }
38
39
    /**
40
     * Set the raster resolution.
41
     *
42
     * @param int $resolution
43
     *
44
     * @return $this
45
     */
46
    public function setResolution($resolution)
47
    {
48
        $this->resolution = $resolution;
49
50
        return $this;
51
    }
52
53
    /**
54
     * Set the output format.
55
     *
56
     * @param string $outputFormat
57
     *
58
     * @return $this
59
     *
60
     * @throws \Spatie\PdfToImage\Exceptions\InvalidFormat
61
     */
62
    public function setOutputFormat($outputFormat)
63
    {
64
        if (!$this->isValidOutputFormat($outputFormat)) {
65
            throw new InvalidFormat('Format '.$outputFormat.' is not supported');
66
        }
67
68
        $this->outputFormat = $outputFormat;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Set the output color space.
75
     *
76
     * @param string $outputColorSpace
77
     *
78
     * @return $this
79
     *
80
     * @throws \Spatie\PdfToImage\Exceptions\InvalidColorspace
81
     */
82
    public function setOutputColorSpace($outputColorSpace)
83
    {        
84
        if (!$this->isValidOutputColorSpace($outputColorSpace)) {
85
            throw new InvalidColorSpace('Colorspace '.$outputColorSpace.' is not valid');
86
        }
87
88
        $this->outputColorSpace = constant('Imagick::'.$outputColorSpace);
89
90
        return $this;
91
    }
92
93
    /**
94
     * Determine if the given format is a valid output format.
95
     *
96
     * @param $outputFormat
97
     *
98
     * @return bool
99
     */
100
    public function isValidOutputFormat($outputFormat)
101
    {
102
        return in_array($outputFormat, $this->validOutputFormats);
103
    }
104
105
    /**
106
     * Determine if the given colorspace is a valid.
107
     *
108
     * @param $outputFormat
109
     *
110
     * @return bool
111
     */
112
    public function isValidOutputColorSpace($outputColorSpace)
113
    {                
114
        return defined('Imagick::'.$outputColorSpace);        
115
    }
116
117
    /**
118
     * Set the page number that should be rendered.
119
     *
120
     * @param int $page
121
     *
122
     * @return $this
123
     *
124
     * @throws \Spatie\PdfToImage\Exceptions\PageDoesNotExist
125
     */
126
    public function setPage($page)
127
    {
128
        if ($page > $this->getNumberOfPages()) {
129
            throw new PageDoesNotExist('Page '.$page.' does not exist');
130
        }
131
132
        $this->page = $page;
133
134
        return $this;
135
    }
136
137
    /**
138
     * Get the number of pages in the pdf file.
139
     *
140
     * @return int
141
     */
142
    public function getNumberOfPages()
143
    {
144
        return (new \Imagick($this->pdfFile))->getNumberImages();
145
    }
146
147
    /**
148
     * Save the image to the given path.
149
     *
150
     * @param string $pathToImage
151
     *
152
     * @return bool
153
     */
154
    public function saveImage($pathToImage)
155
    {
156
        $imageData = $this->getImageData($pathToImage);
157
158
        file_put_contents($pathToImage, $imageData);
159
160
        return true;
161
    }
162
163
    /**
164
     * Save the file as images to the given directory.
165
     *
166
     * @param string $directory
167
     * @param string $prefix
168
     *
169
     * @return array $files the paths to the created images
170
     */
171
    public function saveAllPagesAsImages($directory, $prefix = '')
172
    {
173
        $numberOfPages = $this->getNumberOfPages();
174
175
        if ($numberOfPages === 0) {
176
            return [];
177
        }
178
179
        return array_map(function ($pageNumber) use ($directory, $prefix) {
180
181
            $this->setPage($pageNumber);
182
183
            $destination = "{$directory}/{$prefix}{$pageNumber}.{$this->outputFormat}";
184
185
            $this->saveImage($destination);
186
187
            return $destination;
188
189
        }, range(1, $numberOfPages));
190
    }
191
192
    /**
193
     * Return raw image data.
194
     *
195
     * @param string $pathToImage
196
     *
197
     * @return \Imagick
198
     */
199
    public function getImageData($pathToImage)
200
    {
201
        $imagick = new \Imagick();
202
203
        $imagick->setResolution($this->resolution, $this->resolution);
204
205
        $imagick->readImage(sprintf('%s[%s]', $this->pdfFile, $this->page - 1));
206
207
        $imagick->setImageFormat($this->determineOutputFormat($pathToImage));
208
209
        $imagick->transformImageColorspace($this->outputColorSpace);
210
211
        $imagick->setFormat($this->determineOutputFormat($pathToImage));
212
213
        file_put_contents($pathToImage, $imagick);
214
215
        return $imagick;
216
    }
217
218
    /**
219
     * Determine in which format the image must be rendered.
220
     *
221
     * @param $pathToImage
222
     *
223
     * @return string
224
     */
225
    protected function determineOutputFormat($pathToImage)
226
    {
227
        $outputFormat = pathinfo($pathToImage, PATHINFO_EXTENSION);
228
229
        if ($this->outputFormat != '') {
230
            $outputFormat = $this->outputFormat;
231
        }
232
233
        $outputFormat = strtolower($outputFormat);
234
235
        if (!$this->isValidOutputFormat($outputFormat)) {
236
            $outputFormat = 'jpg';
237
        }
238
239
        return $outputFormat;
240
    }
241
}
242