WebThumbnailer::thumbnail()   B
last analyzed

Complexity

Conditions 8
Paths 12

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 38
rs 8.4444
c 0
b 0
f 0
cc 8
nc 12
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebThumbnailer;
6
7
use WebThumbnailer\Application\Thumbnailer;
8
use WebThumbnailer\Exception\MissingRequirementException;
9
use WebThumbnailer\Exception\WebThumbnailerException;
10
11
/**
12
 * WebThumbnailer.php
13
 */
14
class WebThumbnailer
15
{
16
    /*
17
     * SIZES
18
     */
19
    public const MAX_WIDTH = 'MAX_WIDTH';
20
    public const MAX_HEIGHT = 'MAX_HEIGHT';
21
    public const SIZE_SMALL = 'small';
22
    public const SIZE_MEDIUM = 'medium';
23
    public const SIZE_LARGE = 'large';
24
25
    /*
26
     * DOWNLOAD & CACHE
27
     */
28
    /** Flag to download and serve locally all image. */
29
    public const DOWNLOAD = 'DOWNLOAD';
30
31
    /** Flag to use hotlink if available. */
32
    public const HOTLINK = 'HOTLINK';
33
34
    /** Use only hotlink, no thumbnail if not available. */
35
    public const HOTLINK_STRICT = 'HOTLINK_STRICT';
36
37
    /** Network timeout, in seconds. */
38
    public const DOWNLOAD_TIMEOUT = 'DOWNLOAD_TIMEOUT';
39
40
    /** Number of bytes to download for a thumbnail. Default 4194304 (4MB). */
41
    public const DOWNLOAD_MAX_SIZE = 'DOWNLOAD_MAX_SIZE';
42
43
    /** Enable verbose mode: log errors with error_log */
44
    public const VERBOSE = 'VERBOSE';
45
46
    /** Disable the cache system. */
47
    public const NOCACHE = 'NOCACHE';
48
49
    /** Crop image to fixed size. */
50
    public const CROP = 'CROP';
51
52
    /** Debug mode. Throw exceptions. */
53
    public const DEBUG = 'DEBUG';
54
55
    /** Setting to define resize mode */
56
    public const RESIZE_MODE = 'RESIZE_MODE';
57
58
    /** Resize mode: less CPU usage but could end up pixellized */
59
    public const RESIZE = 'RESIZE';
60
61
    /** Resample mode: more CPU usage but smoother rendering */
62
    public const RESAMPLE = 'RESAMPLE';
63
64
    /** @var int|null */
65
    protected $maxWidth = null;
66
67
    /** @var int|null */
68
    protected $maxHeight = null;
69
70
    /** @var int|null */
71
    protected $downloadTimeout = null;
72
73
    /** @var int|null */
74
    protected $downloadMaxSize = null;
75
76
    /** @var bool|null */
77
    protected $debug = null;
78
79
    /** @var bool|null */
80
    protected $verbose = null;
81
82
    /** @var bool|null */
83
    protected $nocache = null;
84
85
    /** @var bool|null */
86
    protected $crop = null;
87
88
    /** @var string|null */
89
    protected $downloadMode = null;
90
91
    /** @var string|null */
92
    protected $resizeMode = null;
93
94
    /**
95
     * Get the thumbnail for the given URL>
96
     *
97
     * @param string  $url     User URL.
98
     * @param mixed[] $options Options array. See the documentation for more infos.
99
     *
100
     * @return string|false Thumbnail URL, false if not found.
101
     *
102
     * @throws WebThumbnailerException Only throw exception in debug mode.
103
     */
104
    public function thumbnail(string $url, array $options = [])
105
    {
106
        $url = trim($url);
107
        if (empty($url)) {
108
            return false;
109
        }
110
111
        $options = array_merge(
112
            [
113
                static::DEBUG => $this->debug,
114
                static::VERBOSE => $this->verbose,
115
                static::NOCACHE => $this->nocache,
116
                static::MAX_WIDTH => $this->maxWidth,
117
                static::MAX_HEIGHT => $this->maxHeight,
118
                static::DOWNLOAD_TIMEOUT => $this->downloadTimeout,
119
                static::DOWNLOAD_MAX_SIZE => $this->downloadMaxSize,
120
                static::CROP => $this->crop,
121
                static::RESIZE_MODE => $this->resizeMode,
122
                $this->downloadMode
123
            ],
124
            $options
125
        );
126
127
        try {
128
            $downloader = new Thumbnailer($url, $options, $_SERVER);
129
            return $downloader->getThumbnail();
130
        } catch (MissingRequirementException $e) {
131
            throw $e;
132
        } catch (WebThumbnailerException $e) {
133
            if (isset($options[static::VERBOSE]) && $options[static::VERBOSE] === true) {
134
                error_log($e->getMessage());
135
            }
136
137
            if (isset($options[static::DEBUG]) && $options[static::DEBUG] === true) {
138
                throw $e;
139
            }
140
141
            return false;
142
        }
143
    }
144
145
    /**
146
     * @param int|string $maxWidth Either number of pixels or SIZE_SMALL|SIZE_MEDIUM|SIZE_LARGE.
147
     *
148
     * @return WebThumbnailer self instance.
149
     */
150
    public function maxWidth($maxWidth): self
151
    {
152
        $this->maxWidth = (int) $maxWidth;
153
154
        return $this;
155
    }
156
157
    /**
158
     * @param int|string $maxHeight Either number of pixels or SIZE_SMALL|SIZE_MEDIUM|SIZE_LARGE.
159
     *
160
     * @return WebThumbnailer self instance.
161
     */
162
    public function maxHeight($maxHeight): self
163
    {
164
        $this->maxHeight = (int) $maxHeight;
165
166
        return $this;
167
    }
168
169
    /**
170
     * @param bool $debug
171
     *
172
     * @return WebThumbnailer self instance.
173
     */
174
    public function debug(bool $debug): self
175
    {
176
        $this->debug = $debug;
177
178
        return $this;
179
    }
180
181
    /**
182
     * @param bool $verbose
183
     *
184
     * @return WebThumbnailer self instance.
185
     */
186
    public function verbose(bool $verbose): self
187
    {
188
        $this->verbose = $verbose;
189
        return $this;
190
    }
191
192
    /**
193
     * @param bool $nocache
194
     *
195
     * @return WebThumbnailer self instance.
196
     */
197
    public function noCache(bool $nocache): self
198
    {
199
        $this->nocache = $nocache;
200
201
        return $this;
202
    }
203
204
    /**
205
     * @param bool $crop
206
     *
207
     * @return WebThumbnailer $this
208
     */
209
    public function crop(bool $crop): self
210
    {
211
        $this->crop = $crop;
212
213
        return $this;
214
    }
215
216
    /**
217
     * @param int $downloadTimeout in seconds
218
     *
219
     * @return WebThumbnailer $this
220
     */
221
    public function downloadTimeout(int $downloadTimeout): self
222
    {
223
        $this->downloadTimeout = $downloadTimeout;
224
225
        return $this;
226
    }
227
228
    /**
229
     * @param int $downloadMaxSize in bytes
230
     *
231
     * @return WebThumbnailer $this
232
     */
233
    public function downloadMaxSize(int $downloadMaxSize): self
234
    {
235
        $this->downloadMaxSize = $downloadMaxSize;
236
237
        return $this;
238
    }
239
240
    /**
241
     * Enable download mode
242
     * It will download thumbnail, resize it and save it in the cache folder.
243
     *
244
     * @return WebThumbnailer $this
245
     */
246
    public function modeDownload(): self
247
    {
248
        $this->downloadMode = static::DOWNLOAD;
249
250
        return $this;
251
    }
252
253
    /**
254
     * Enable hotlink mode
255
     * It will use image hotlinking if the domain authorize it, download it otherwise.
256
     *
257
     * @return WebThumbnailer $this
258
     */
259
    public function modeHotlink(): self
260
    {
261
        $this->downloadMode = static::HOTLINK;
262
263
        return $this;
264
    }
265
266
    /**
267
     * Enable strict hotlink mode
268
     * It will use image hotlinking if the domain authorize it, fail otherwise.
269
     *
270
     * @return WebThumbnailer $this
271
     */
272
    public function modeHotlinkStrict(): self
273
    {
274
        $this->downloadMode = static::HOTLINK_STRICT;
275
276
        return $this;
277
    }
278
279
    /**
280
     * Apply resize mode during resizing:
281
     * lower CPU usage but sometimes rougher rendering.
282
     *
283
     * @return WebThumbnailer $this
284
     */
285
    public function resize(): self
286
    {
287
        $this->resizeMode = static::RESIZE;
288
289
        return $this;
290
    }
291
292
    /**
293
     * Apply resample mode during resizing:
294
     * slightly more CPU usage but smoother rendering.
295
     *
296
     * @return WebThumbnailer $this
297
     */
298
    public function resample(): self
299
    {
300
        $this->resizeMode = static::RESAMPLE;
301
302
        return $this;
303
    }
304
}
305