Completed
Push — master ( e61258...4cb8f2 )
by Arthur
9s
created

WebThumbnailer::pathAbsolute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
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
     * Debug mode. Throw exceptions.
57
     */
58
    const DEBUG = 'DEBUG';
59
60
    protected $maxWidth;
61
62
    protected $maxHeight;
63
64
    protected $debug;
65
66
    protected $nocache;
67
    
68
    protected $crop;
69
70
    protected $downloadMode;
71
72
    /**
73
     * Get the thumbnail for the given URL>
74
     *
75
     * @param string $url     User URL.
76
     * @param array  $options Options array. See the documentation for more infos.
77
     *
78
     * @return bool|string Thumbnail URL, false if not found.
79
     *
80
     * @throws WebThumbnailerException Only throw exception in debug mode.
81
     */
82
    public function thumbnail($url, $options = [])
83
    {
84
        $url = trim($url);
85
        if (empty($url)) {
86
            return false;
87
        }
88
89
        $options = array_merge(
90
            [
91
                self::DEBUG => $this->debug,
92
                self::NOCACHE => $this->nocache,
93
                self::MAX_WIDTH => $this->maxWidth,
94
                self::MAX_HEIGHT => $this->maxHeight,
95
                self::CROP => $this->crop,
96
                $this->downloadMode
97
            ],
98
            $options
99
        );
100
101
        try {
102
            $downloader = new Thumbnailer($url, $options, $_SERVER);
103
            return $downloader->getThumbnail();
104
        } catch (MissingRequirementException $e) {
105
            throw $e;
106
        } catch (WebThumbnailerException $e) {
107
            if (isset($options[self::DEBUG]) && $options[self::DEBUG] === true) {
108
                throw $e;
109
            }
110
            error_log($e->getMessage());
111
112
            return false;
113
        }
114
    }
115
116
    /**
117
     * @param int|string $maxWidth Either number of pixels or SIZE_SMALL|SIZE_MEDIUM|SIZE_LARGE.
118
     *
119
     * @return WebThumbnailer self instance.
120
     */
121
    public function maxWidth($maxWidth)
122
    {
123
        $this->maxWidth = $maxWidth;
124
        return $this;
125
    }
126
127
    /**
128
     * @param int|string $maxHeight Either number of pixels or SIZE_SMALL|SIZE_MEDIUM|SIZE_LARGE.
129
     *
130
     * @return WebThumbnailer self instance.
131
     */
132
    public function maxHeight($maxHeight)
133
    {
134
        $this->maxHeight = $maxHeight;
135
        return $this;
136
    }
137
138
    /**
139
     * @param bool $debug
140
     *
141
     * @return WebThumbnailer self instance.
142
     */
143
    public function debug($debug)
144
    {
145
        $this->debug = $debug;
146
        return $this;
147
    }
148
149
    /**
150
     * @param mixed $nocache
151
     *
152
     * @return WebThumbnailer self instance.
153
     */
154
    public function noCache($nocache)
155
    {
156
        $this->nocache = $nocache;
157
        return $this;
158
    }
159
160
    /**
161
     * @param bool $crop
162
     *
163
     * @return WebThumbnailer $this
164
     */
165
    public function crop($crop)
166
    {
167
        $this->crop = $crop;
168
        return $this;
169
    }
170
171
    /**
172
     * Enable download mode
173
     * It will download thumbnail, resize it and save it in the cache folder.
174
     *
175
     * @return WebThumbnailer $this
176
     */
177
    public function modeDownload()
178
    {
179
        $this->downloadMode = self::DOWNLOAD;
180
        return $this;
181
    }
182
183
    /**
184
     * Enable hotlink mode
185
     * It will use image hotlinking if the domain authorize it, download it otherwise.
186
     *
187
     * @return WebThumbnailer $this
188
     */
189
    public function modeHotlink()
190
    {
191
        $this->downloadMode = self::HOTLINK;
192
        return $this;
193
    }
194
195
    /**
196
     * Enable strict hotlink mode
197
     * It will use image hotlinking if the domain authorize it, fail otherwise.
198
     *
199
     * @return WebThumbnailer $this
200
     */
201
    public function modeHotlinkStrict()
202
    {
203
        $this->downloadMode = self::HOTLINK_STRICT;
204
        return $this;
205
    }
206
}
207