Completed
Pull Request — master (#149)
by
unknown
03:12
created

Pdf::getRemoteImageData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.7998
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Spatie\PdfToImage;
4
5
use Jcupitt\Vips;
6
use Spatie\PdfToImage\Exceptions\InvalidFormat;
7
use Spatie\PdfToImage\Exceptions\PageDoesNotExist;
8
use Spatie\PdfToImage\Exceptions\PdfDoesNotExist;
9
10
class Pdf
11
{
12
    protected $pdfFile;
13
14
    protected $resolution = 144;
15
16
    protected $outputFormat = 'jpg';
17
18
    protected $page = 1;
19
20
    protected $numberOfPages;
21
22
    protected $validOutputFormats = ['jpg', 'jpeg', 'png'];
23
24
    protected $compressionQuality;
25
26
    public function __construct(string $pdfFile)
27
    {
28
        $this->pdfFile = $pdfFile;
29
30
        if (! file_exists($this->pdfFile)) {
31
            throw new PdfDoesNotExist("File `{$this->pdfFile}` does not exist");
32
        }
33
34
        // php-vips will just read the header and not decode the whole file, so
35
        // this is quick
36
        $image = Vips\Image::newFromFile($this->pdfFile);
37
        $this->numberOfPages = $image->get("n-pages");
38
    }
39
40
    public function setResolution(int $resolution)
41
    {
42
        $this->resolution = $resolution;
43
44
        return $this;
45
    }
46
47
    public function setOutputFormat(string $outputFormat)
48
    {
49
        if (! $this->isValidOutputFormat($outputFormat)) {
50
            throw new InvalidFormat("Format {$outputFormat} is not supported");
51
        }
52
53
        $this->outputFormat = $outputFormat;
54
55
        return $this;
56
    }
57
58
    public function getOutputFormat(): string
59
    {
60
        return $this->outputFormat;
61
    }
62
63
    /**
64
     * Sets the layer method for Imagick::mergeImageLayers()
65
     * If int, should correspond to a predefined LAYERMETHOD constant.
66
     * If null, Imagick::mergeImageLayers() will not be called.
67
     *
68
     * @param int|null
69
     *
70
     * @return $this
71
     *
72
     * @throws \Spatie\PdfToImage\Exceptions\InvalidLayerMethod
73
     *
74
     * @see https://secure.php.net/manual/en/imagick.constants.php
75
     * @see Pdf::getImageData()
76
     */
77
    public function setLayerMethod(?int $layerMethod)
0 ignored issues
show
Unused Code introduced by
The parameter $layerMethod is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    {
79
        // not needed by php-vips, I think
80
        return null;
81
    }
82
83
    public function isValidOutputFormat(string $outputFormat): bool
84
    {
85
        return in_array($outputFormat, $this->validOutputFormats);
86
    }
87
88
    public function setPage(int $page)
89
    {
90
        if ($page > $this->getNumberOfPages() || $page < 1) {
91
            throw new PageDoesNotExist("Page {$page} does not exist");
92
        }
93
94
        $this->page = $page;
95
96
        return $this;
97
    }
98
99
    public function getNumberOfPages(): int
100
    {
101
        return $this->numberOfPages;
102
    }
103
104
    public function saveImage(string $pathToImage): bool
105
    {
106
        if (is_dir($pathToImage)) {
107
            $pathToImage = rtrim($pathToImage, '\/').DIRECTORY_SEPARATOR.$this->page.'.'.$this->outputFormat;
108
        }
109
110
        $page = $this->getImageData($pathToImage);
111
112
        $compression = $this->compressionQuality !== null ?
113
            $this->compressionQuality : 75;
114
115
        // php-vips will set the image format from the suffix
116
        $page->writeToFile($pathToImage, [
117
            "Q" => $compression
118
        ]);
119
120
        // php-vips will throw an exception in writeToFile if there's an error
121
        return TRUE;
122
    }
123
124
    public function saveAllPagesAsImages(string $directory, string $prefix = ''): array
125
    {
126
        $numberOfPages = $this->getNumberOfPages();
127
128
        if ($numberOfPages === 0) {
129
            return [];
130
        }
131
132
        return array_map(function ($pageNumber) use ($directory, $prefix) {
133
            $this->setPage($pageNumber);
134
135
            $destination = "{$directory}/{$prefix}{$pageNumber}.{$this->outputFormat}";
136
137
            $this->saveImage($destination);
138
139
            return $destination;
140
        }, range(1, $numberOfPages));
141
    }
142
143
    public function getImageData(string $pathToImage): Vips\Image
0 ignored issues
show
Unused Code introduced by
The parameter $pathToImage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
    {
145
        $page = Vips\Image::newFromFile($this->pdfFile, [
146
            "dpi" => $this->resolution,
147
            "page" => $this->page - 1,
148
            # this enables image streaming
149
            "access" => "sequential"
150
        ]);
151
152
        return $page;
153
    }
154
155
    public function setColorspace(int $colorspace)
0 ignored issues
show
Unused Code introduced by
The parameter $colorspace is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
    {
157
        // php-vips always renders PDFs as RGB ... you'll need to use imagick if
158
        // you want CMYK
159
        return null;
160
    }
161
162
    public function setCompressionQuality(int $compressionQuality)
163
    {
164
        $this->compressionQuality = $compressionQuality;
165
166
        return $this;
167
    }
168
169
    protected function determineOutputFormat(string $pathToImage): string
170
    {
171
        $outputFormat = pathinfo($pathToImage, PATHINFO_EXTENSION);
172
173
        if ($this->outputFormat != '') {
174
            $outputFormat = $this->outputFormat;
175
        }
176
177
        $outputFormat = strtolower($outputFormat);
178
179
        if (! $this->isValidOutputFormat($outputFormat)) {
180
            $outputFormat = 'jpg';
181
        }
182
183
        return $outputFormat;
184
    }
185
}
186