Completed
Push — master ( 440ea6...6c9d64 )
by Freek
01:57
created

Pdf::extractAllPagesAsImages()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
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 14
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 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)
158
    {
159
        $imagick = new \Imagick();
160
161
        $imagick->setResolution($this->resolution, $this->resolution);
162
163
        $imagick->readImage(sprintf('%s[%s]', $this->pdfFile, $this->page - 1));
164
165
        $imagick->setFormat($this->determineOutputFormat($pathToImage));
166
167
        return $imagick;
168
    }
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)
178
    {
179
        $outputFormat = pathinfo($pathToImage, PATHINFO_EXTENSION);
180
181
        if ($this->outputFormat != '') {
182
            $outputFormat = $this->outputFormat;
183
        }
184
185
        $outputFormat = strtolower($outputFormat);
186
187
        if (!$this->isValidOutputFormat($outputFormat)) {
188
            $outputFormat = 'jpg';
189
        }
190
191
        return $outputFormat;
192
    }
193
}
194