Completed
Pull Request — master (#4)
by James
01:45
created

Pdf::getImageData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
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
     * Return raw image data.
129
     *
130
     * @param  string $pathToImage
131
     *
132
     * @return \Imagick
133
     */
134
    public function getImageData($pathToImage)
135
    {
136
        $imagick = new \Imagick();
137
138
        $imagick->setResolution($this->resolution, $this->resolution);
139
140
        $imagick->readImage(sprintf('%s[%s]', $this->pdfFile, $this->page - 1));
141
142
        $imagick->setFormat($this->determineOutputFormat($pathToImage));
143
144
        return $imagick;
145
    }
146
147
    /**
148
     * Determine in which format the image must be rendered.
149
     *
150
     * @param $pathToImage
151
     *
152
     * @return string
153
     */
154
    protected function determineOutputFormat($pathToImage)
155
    {
156
        $outputFormat = pathinfo($pathToImage, PATHINFO_EXTENSION);
157
158
        if ($this->outputFormat != '') {
159
            $outputFormat = $this->outputFormat;
160
        }
161
162
        $outputFormat = strtolower($outputFormat);
163
164
        if (!$this->isValidOutputFormat($outputFormat)) {
165
            $outputFormat = 'jpg';
166
        }
167
168
        return $outputFormat;
169
    }
170
}
171