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