Completed
Pull Request — master (#11)
by Arthur
03:02
created

WebThumbnailer::debug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
     * OTHER
57
     */
58
    /**
59
     * Either get a relative or absolute path to the cached image.
60
     *
61
     * With `relative`, WebThumbnailer will try to retrieve a proper relative path to cached thumbnails
62
     * using SCRIPT_FILENAME server variable, but may fail in CLI or if the cache directory isn't
63
     * in the same tree as your script file. In this case, you should use `absolute`.
64
     */
65
    const PATH_TYPE = 'PATH_TYPE';
66
67
    const PATH_RELATIVE = 'relative';
68
69
    const PATH_ABSOLUTE = 'absolute';
70
71
    /**
72
     * Debug mode. Throw exceptions.
73
     */
74
    const DEBUG = 'DEBUG';
75
76
    protected $maxWidth;
77
78
    protected $maxHeight;
79
80
    protected $debug;
81
82
    protected $nocache;
83
    
84
    protected $crop;
85
86
    protected $downloadMode = self::DOWNLOAD;
87
88
    protected $pathType = self::PATH_RELATIVE;
89
90
    /**
91
     * Get the thumbnail for the given URL>
92
     *
93
     * @param string $url     User URL.
94
     * @param array  $options Options array. See the documentation for more infos.
95
     *
96
     * @return bool|string Thumbnail URL, false if not found.
97
     *
98
     * @throws WebThumbnailerException Only throw exception in debug mode.
99
     */
100
    public function thumbnail($url, $options = [])
101
    {
102
        $url = trim($url);
103
        if (empty($url)) {
104
            return false;
105
        }
106
107
        $options = array_merge(
108
            [
109
                self::DEBUG => $this->debug,
110
                self::NOCACHE => $this->nocache,
111
                self::MAX_WIDTH => $this->maxWidth,
112
                self::MAX_HEIGHT => $this->maxHeight,
113
                self::CROP => $this->crop,
114
                self::PATH_TYPE => $this->pathType,
115
                $this->downloadMode
116
            ],
117
            $options
118
        );
119
120
        try {
121
            $downloader = new Thumbnailer($url, $options, $_SERVER);
122
            return $downloader->getThumbnail();
123
        } catch (MissingRequirementException $e) {
124
            throw $e;
125
        } catch (WebThumbnailerException $e) {
126
            if (isset($options[self::DEBUG]) && $options[self::DEBUG] === true) {
127
                throw $e;
128
            }
129
            error_log($e->getMessage());
130
131
            return false;
132
        }
133
    }
134
135
    /**
136
     * @param int|string $maxWidth Either number of pixels or SIZE_SMALL|SIZE_MEDIUM|SIZE_LARGE.
137
     *
138
     * @return WebThumbnailer self instance.
139
     */
140
    public function maxWidth($maxWidth)
141
    {
142
        $this->maxWidth = $maxWidth;
143
        return $this;
144
    }
145
146
    /**
147
     * @param int|string $maxHeight Either number of pixels or SIZE_SMALL|SIZE_MEDIUM|SIZE_LARGE.
148
     *
149
     * @return WebThumbnailer self instance.
150
     */
151
    public function maxHeight($maxHeight)
152
    {
153
        $this->maxHeight = $maxHeight;
154
        return $this;
155
    }
156
157
    /**
158
     * @param bool $debug
159
     *
160
     * @return WebThumbnailer self instance.
161
     */
162
    public function debug($debug)
163
    {
164
        $this->debug = $debug;
165
        return $this;
166
    }
167
168
    /**
169
     * @param mixed $nocache
170
     *
171
     * @return WebThumbnailer self instance.
172
     */
173
    public function noCache($nocache)
174
    {
175
        $this->nocache = $nocache;
176
        return $this;
177
    }
178
179
    /**
180
     * @param bool $crop
181
     *
182
     * @return WebThumbnailer $this
183
     */
184
    public function crop($crop)
185
    {
186
        $this->crop = $crop;
187
        return $this;
188
    }
189
190
    /**
191
     * Enable download mode
192
     * It will download thumbnail, resize it and save it in the cache folder.
193
     *
194
     * @return WebThumbnailer $this
195
     */
196
    public function modeDownload()
197
    {
198
        $this->downloadMode = self::DOWNLOAD;
199
        return $this;
200
    }
201
202
    /**
203
     * Enable hotlink mode
204
     * It will use image hotlinking if the domain authorize it, download it otherwise.
205
     *
206
     * @return WebThumbnailer $this
207
     */
208
    public function modeHotlink()
209
    {
210
        $this->downloadMode = self::HOTLINK;
211
        return $this;
212
    }
213
214
    /**
215
     * Enable strict hotlink mode
216
     * It will use image hotlinking if the domain authorize it, fail otherwise.
217
     *
218
     * @return WebThumbnailer $this
219
     */
220
    public function modeHotlinkStrict()
221
    {
222
        $this->downloadMode = self::HOTLINK_STRICT;
223
        return $this;
224
    }
225
226
    /**
227
     * WT will return a relative path from the calling script to the cached thumbnail,
228
     * using provided $_SERVER variables.
229
     *
230
     * @return WebThumbnailer $this
231
     */
232
    public function pathRelative()
233
    {
234
        $this->pathType = self::PATH_RELATIVE;
235
        return $this;
236
    }
237
238
    /**
239
     * WT will return an absolute path to the cached thumbnail.
240
     *
241
     * @return WebThumbnailer $this
242
     */
243
    public function pathAbsolute()
244
    {
245
        $this->pathType = self::PATH_ABSOLUTE;
246
        return $this;
247
    }
248
}
249