forked from masterexploder/PHPThumb
/
ThumbBase.inc.php
323 lines (297 loc) · 6.76 KB
/
ThumbBase.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* PhpThumb Base Class Definition File
*
* This file contains the definition for the ThumbBase object
*
* PHP Version 5 with GD 2.0+
* PhpThumb : PHP Thumb Library <http://phpthumb.gxdlabs.com>
* Copyright (c) 2009, Ian Selby/Gen X Design
*
* Author(s): Ian Selby <ian@gen-x-design.com>
*
* Licensed under the MIT License
* Redistributions of files must retain the above copyright notice.
*
* @author Ian Selby <ian@gen-x-design.com>
* @copyright Copyright (c) 2009 Gen X Design
* @link http://phpthumb.gxdlabs.com
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @version 3.0
* @package PhpThumb
* @filesource
*/
/**
* ThumbBase Class Definition
*
* This is the base class that all implementations must extend. It contains the
* core variables and functionality common to all implementations, as well as the functions that
* allow plugins to augment those classes.
*
* @package PhpThumb
* @subpackage Core
*/
abstract class ThumbBase
{
/**
* All imported objects
*
* An array of imported plugin objects
*
* @var array
*/
protected $imported;
/**
* All imported object functions
*
* An array of all methods added to this class by imported plugin objects
*
* @var array
*/
protected $importedFunctions;
/**
* The last error message raised
*
* @var string
*/
protected $errorMessage;
/**
* Whether or not the current instance has any errors
*
* @var bool
*/
protected $hasError;
/**
* The name of the file we're manipulating
*
* This must include the path to the file (absolute paths recommended)
*
* @var string
*/
protected $fileName;
/**
* What the file format is (mime-type)
*
* @var string
*/
protected $format;
/**
* Whether or not the image is hosted remotely
*
* @var bool
*/
protected $remoteImage;
/**
* Whether or not the current image is an actual file, or the raw file data
*
* By "raw file data" it's meant that we're actually passing the result of something
* like file_get_contents() or perhaps from a database blob
*
* @var bool
*/
protected $isDataStream;
/**
* Class constructor
*
* @return ThumbBase
*/
public function __construct ($fileName, $isDataStream = false)
{
$this->imported = array();
$this->importedFunctions = array();
$this->errorMessage = null;
$this->hasError = false;
$this->fileName = $fileName;
$this->remoteImage = false;
$this->isDataStream = $isDataStream;
$this->fileExistsAndReadable();
}
/**
* Imports plugins in $registry to the class
*
* @param array $registry
*/
public function importPlugins ($registry)
{
foreach ($registry as $plugin => $meta)
{
$this->imports($plugin);
}
}
/**
* Imports a plugin
*
* This is where all the plugins magic happens! This function "loads" the plugin functions, making them available as
* methods on the class.
*
* @param string $object The name of the object to import / "load"
*/
protected function imports ($object)
{
// the new object to import
$newImport = new $object();
// the name of the new object (class name)
$importName = get_class($newImport);
// the new functions to import
$importFunctions = get_class_methods($newImport);
// add the object to the registry
array_push($this->imported, array($importName, $newImport));
// add the methods to the registry
foreach ($importFunctions as $key => $functionName)
{
$this->importedFunctions[$functionName] = &$newImport;
}
}
/**
* Checks to see if $this->fileName exists and is readable
*
*/
protected function fileExistsAndReadable ()
{
if ($this->isDataStream === true)
{
return;
}
if (preg_match('/https?:\/\//', $this->fileName) !== 0)
{
$this->remoteImage = true;
return;
}
if (!file_exists($this->fileName))
{
$this->triggerError('Image file not found: ' . $this->fileName);
}
elseif (!is_readable($this->fileName))
{
$this->triggerError('Image file not readable: ' . $this->fileName);
}
}
/**
* Sets $this->errorMessage to $errorMessage and throws an exception
*
* Also sets $this->hasError to true, so even if the exceptions are caught, we don't
* attempt to proceed with any other functions
*
* @param string $errorMessage
*/
protected function triggerError ($errorMessage)
{
$this->hasError = true;
$this->errorMessage = $errorMessage;
throw new Exception ($errorMessage);
}
/**
* Calls plugin / imported functions
*
* This is also where a fair amount of plugins magaic happens. This magic method is called whenever an "undefined" class
* method is called in code, and we use that to call an imported function.
*
* You should NEVER EVER EVER invoke this function manually. The universe will implode if you do... seriously ;)
*
* @param string $method
* @param array $args
*/
public function __call ($method, $args)
{
if( array_key_exists($method, $this->importedFunctions))
{
$args[] =& $this;
return call_user_func_array(array($this->importedFunctions[$method], $method), $args);
}
throw new BadMethodCallException ('Call to undefined method/class function: ' . $method);
}
/**
* Returns $imported.
* @see ThumbBase::$imported
* @return array
*/
public function getImported ()
{
return $this->imported;
}
/**
* Returns $importedFunctions.
* @see ThumbBase::$importedFunctions
* @return array
*/
public function getImportedFunctions ()
{
return $this->importedFunctions;
}
/**
* Returns $errorMessage.
*
* @see ThumbBase::$errorMessage
*/
public function getErrorMessage ()
{
return $this->errorMessage;
}
/**
* Sets $errorMessage.
*
* @param object $errorMessage
* @see ThumbBase::$errorMessage
*/
public function setErrorMessage ($errorMessage)
{
$this->errorMessage = $errorMessage;
}
/**
* Returns $fileName.
*
* @see ThumbBase::$fileName
*/
public function getFileName ()
{
return $this->fileName;
}
/**
* Sets $fileName.
*
* @param object $fileName
* @see ThumbBase::$fileName
*/
public function setFileName ($fileName)
{
$this->fileName = $fileName;
}
/**
* Returns $format.
*
* @see ThumbBase::$format
*/
public function getFormat ()
{
return $this->format;
}
/**
* Sets $format.
*
* @param object $format
* @see ThumbBase::$format
*/
public function setFormat ($format)
{
$this->format = $format;
}
/**
* Returns $hasError.
*
* @see ThumbBase::$hasError
*/
public function getHasError ()
{
return $this->hasError;
}
/**
* Sets $hasError.
*
* @param object $hasError
* @see ThumbBase::$hasError
*/
public function setHasError ($hasError)
{
$this->hasError = $hasError;
}
}