1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* this is the image that will be return apon error |
4
|
|
|
*/ |
5
|
|
|
if (!defined('_PATH')) { |
6
|
|
|
define('_PATH', XOOPS_ROOT_PATH); |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
if (!defined('DEFAULT_PATH')) { |
10
|
|
|
define('DEFAULT_PATH', XOOPS_UPLOAD_URL . '/blank.gif'); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ThumbsNails |
15
|
|
|
* |
16
|
|
|
* @package |
17
|
|
|
* @author John N |
18
|
|
|
* @copyright WF-Projects Copyright (c) 2005 |
19
|
|
|
* @copyright Using this class without our permission or removing this notice voids the license agreement. |
20
|
|
|
* @access public |
21
|
|
|
*/ |
22
|
|
|
class WfThumbsNails |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
public $_img_name = 'blank.gif'; |
25
|
|
|
public $_img_path = 'uploads'; |
26
|
|
|
public $_img_savepath = 'thumbs'; |
27
|
|
|
|
28
|
|
|
public $_source_path = ''; |
29
|
|
|
public $_save_path = ''; |
30
|
|
|
public $_source_url = ''; |
31
|
|
|
public $_source_image = ''; |
32
|
|
|
public $_save_image = ''; |
33
|
|
|
|
34
|
|
|
public $_usethumbs = 0; |
35
|
|
|
public $_image_type = 'gd2'; |
36
|
|
|
public $_return_fullpath = 0; |
37
|
|
|
|
38
|
|
|
public $img_width = 100; |
39
|
|
|
public $img_height = 100; |
40
|
|
|
public $img_quality = 100; |
41
|
|
|
public $img_update = 1; |
42
|
|
|
public $img_aspect = 1; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* @access private |
47
|
|
|
*/ |
48
|
|
|
public $_img_info = array(); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor |
52
|
|
|
* |
53
|
|
|
* @param null $img_name |
54
|
|
|
* @param null $img_path |
55
|
|
|
* @param null $img_savepath |
56
|
|
|
* |
57
|
|
|
* @internal param string $_img_name |
58
|
|
|
* @internal param string $_img_path |
59
|
|
|
* @internal param string $_img_savepath |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
|
|
public function __construct($img_name = null, $img_path = null, $img_savepath = null) |
63
|
|
|
{ |
64
|
|
|
if (!preg_match("/\.(jpg|gif|png|jpeg)$/i", $img_name)) { |
|
|
|
|
65
|
|
|
// return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/* |
69
|
|
|
* The actual image we will be processing |
70
|
|
|
*/ |
71
|
|
|
if (null !== $img_name) { |
72
|
|
|
$this->_img_name = trim($img_name); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/* |
76
|
|
|
* The image path |
77
|
|
|
*/ |
78
|
|
|
if (null !== $img_path) { |
79
|
|
|
$this->_img_path = trim($img_path); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/* |
83
|
|
|
* The image save path |
84
|
|
|
*/ |
85
|
|
|
if (null !== $img_savepath) { |
86
|
|
|
$this->_img_savepath = trim($img_savepath); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$path_to_check = XOOPS_ROOT_PATH . "/$img_path/$img_savepath"; |
90
|
|
|
|
91
|
|
|
if (!is_dir($path_to_check)) { |
92
|
|
|
if (false === mkdir("$path_to_check", 0777)) { |
|
|
|
|
93
|
|
|
// return false; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
// return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* WfThumbsNails::setUseThumbs() |
102
|
|
|
* |
103
|
|
|
* @param integer $value |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function setUseThumbs($value = 1) |
108
|
|
|
{ |
109
|
|
|
$this->_usethumbs = $value; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* WfThumbsNails::setImageType() |
114
|
|
|
* |
115
|
|
|
* @param string $value |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
public function setImageType($value = 'gd2') |
120
|
|
|
{ |
121
|
|
|
$this->_image_type = $value; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* ThumbsNails::createThumb() |
126
|
|
|
* |
127
|
|
|
* @param int $img_width |
|
|
|
|
128
|
|
|
* @param int $img_height |
|
|
|
|
129
|
|
|
* @param int $img_quality |
|
|
|
|
130
|
|
|
* @param int $img_update |
|
|
|
|
131
|
|
|
* @param int $img_aspect |
|
|
|
|
132
|
|
|
* |
133
|
|
|
* @return bool|string |
134
|
|
|
*/ |
135
|
|
|
public function createThumb( |
136
|
|
|
$img_width = null, |
137
|
|
|
$img_height = null, |
138
|
|
|
$img_quality = null, |
139
|
|
|
$img_update = null, |
140
|
|
|
$img_aspect = null) |
141
|
|
|
{ |
142
|
|
|
$this->_source_path = XOOPS_ROOT_PATH . "/{$this->_img_path}"; |
143
|
|
|
$this->_save_path = XOOPS_ROOT_PATH . "/{$this->_img_path}/{$this->_img_savepath}"; |
144
|
|
|
$this->_source_url = XOOPS_URL . "/{$this->_img_path}"; |
145
|
|
|
$this->_source_image = "{$this->_source_path}/{$this->_img_name}"; |
146
|
|
|
|
147
|
|
|
if (null !== $img_width && isset($img_width)) { |
148
|
|
|
$this->img_width = (int)$img_width; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (null !== $img_height && isset($img_height)) { |
152
|
|
|
$this->img_height = (int)$img_height; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (null !== $img_quality && isset($img_quality)) { |
156
|
|
|
$this->img_quality = (int)$img_quality; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (null !== $img_update && isset($img_update)) { |
160
|
|
|
$this->img_update = (int)$img_update; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (null !== $img_aspect && isset($img_aspect)) { |
164
|
|
|
$this->img_aspect = (int)$img_aspect; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Return false if we are not using thumb nails |
169
|
|
|
*/ |
170
|
|
|
if (!$this->isUsingThumbs()) { |
171
|
|
|
return $this->_source_url . '/' . $this->_img_name; |
172
|
|
|
} |
173
|
|
|
/** |
174
|
|
|
* Return false if the server does not have gd lib installed or activated |
175
|
|
|
*/ |
176
|
|
|
if (!$this->gd_lib_check()) { |
177
|
|
|
return $this->_source_url . '/' . $this->_img_name; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Return false if the paths to the file are wrong |
182
|
|
|
*/ |
183
|
|
|
if (!$this->checkPaths()) { |
184
|
|
|
return DEFAULT_PATH; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (!$this->checkImage()) { |
188
|
|
|
return DEFAULT_PATH; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$image = $this->resizeImage(); |
192
|
|
|
if ($image === false) { |
193
|
|
|
return DEFAULT_PATH; |
194
|
|
|
} else { |
195
|
|
|
return $image; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param $value |
201
|
|
|
*/ |
202
|
|
|
public function setImgName($value) |
203
|
|
|
{ |
204
|
|
|
$this->_img_name = trim($value); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param $value |
209
|
|
|
*/ |
210
|
|
|
public function setImgPath($value) |
211
|
|
|
{ |
212
|
|
|
$this->_img_path = trim($value); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param $value |
217
|
|
|
*/ |
218
|
|
|
public function setImgSavePath($value) |
219
|
|
|
{ |
220
|
|
|
$this->_img_savepath = trim($value); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* ThumbsNails::resizeImage() |
225
|
|
|
* |
226
|
|
|
* @return bool|string |
227
|
|
|
*/ |
228
|
|
|
public function resizeImage() |
229
|
|
|
{ |
230
|
|
|
global $xoopsModuleConfig; |
|
|
|
|
231
|
|
|
// $this->_img_info = info array to the image being resized |
232
|
|
|
// $this->_img_info[0] == width |
|
|
|
|
233
|
|
|
// $this->_img_info[1] == height |
|
|
|
|
234
|
|
|
// $this->_img_info[2] == is a flag indicating the type of the image: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF(intel byte order), 8 = TIFF(motorola byte order) |
235
|
|
|
// $this->_img_info[3] == is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag |
236
|
|
|
/** |
237
|
|
|
* Get image size and scale ratio |
238
|
|
|
*/ |
239
|
|
|
$scale = min($this->img_width / $this->_img_info[0], $this->img_height / $this->_img_info[1]); |
240
|
|
|
/** |
241
|
|
|
* If the image is larger than the max shrink it |
242
|
|
|
*/ |
243
|
|
|
$newWidth = $this->img_width; |
244
|
|
|
$newHeight = $this->img_height; |
245
|
|
|
if ($scale < 1 && $this->img_aspect == 1) { |
246
|
|
|
$newWidth = floor($scale * $this->_img_info[0]); |
247
|
|
|
$newHeight = floor($scale * $this->_img_info[1]); |
248
|
|
|
} |
249
|
|
|
$newWidth = ($newWidth > $this->_img_info[0]) ? $this->_img_info[0] : $newWidth; |
250
|
|
|
$newHeight = ($newHeight > $this->_img_info[0]) ? $this->_img_info[0] : $newHeight; |
251
|
|
|
/** |
252
|
|
|
*/ |
253
|
|
|
$savefile = "{$newWidth}x{$newHeight}_{$this->_img_name}"; |
254
|
|
|
$this->_save_image = "{$this->_save_path}/{$savefile}"; |
255
|
|
|
|
256
|
|
|
if ($this->img_update == 0 && file_exists($this->_save_image)) { |
257
|
|
|
if ($this->_return_fullpath == 1) { |
258
|
|
|
return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; |
259
|
|
|
} else { |
260
|
|
|
return "{$this->_img_savepath}/{$savefile}"; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
switch ($this->_image_type) { |
265
|
|
|
case 'im': |
266
|
|
|
if (!empty($xoopsModuleConfig['path_magick']) && is_dir($xoopsModuleConfig['path_magick'])) { |
267
|
|
|
if (preg_match("#[A-Z]:|\\\\#Ai", __FILE__)) { |
268
|
|
|
$cur_dir = __DIR__; |
269
|
|
|
$src_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $this->_source_image) . '"'; |
|
|
|
|
270
|
|
|
$new_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $this->_save_image) . '"'; |
271
|
|
|
} else { |
272
|
|
|
$src_file_im = escapeshellarg($this->_source_image); |
|
|
|
|
273
|
|
|
$new_file_im = escapeshellarg($this->_save_image); |
274
|
|
|
} |
275
|
|
|
$magick_command = $xoopsModuleConfig['path_magick'] . '/convert -quality {$xoopsModuleConfig["imagequality"]} -antialias -sample {$newWidth}x{$newHeight} {$src_file_im} +profile "*" ' . str_replace('\\', '/', $new_file_im) . ''; |
276
|
|
|
passthru($magick_command); |
277
|
|
|
|
278
|
|
|
return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; |
279
|
|
|
} else { |
280
|
|
|
return false; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
break; |
|
|
|
|
284
|
|
|
|
285
|
|
|
case 'gd1': |
286
|
|
|
case 'gd2': |
287
|
|
|
default: |
|
|
|
|
288
|
|
|
|
289
|
|
|
$imageCreateFunction = (function_exists('imagecreatetruecolor') |
290
|
|
|
&& $this->_image_type === 'gd2') ? 'imagecreatetruecolor' : 'imagecreate'; |
291
|
|
|
$imageCopyfunction = (function_exists('imagecopyresampled') |
292
|
|
|
&& $this->_image_type === 'gd2') ? 'imagecopyresampled' : 'imagecopyresized'; |
293
|
|
|
|
294
|
|
|
switch ($this->_img_info[2]) { |
295
|
|
|
case 1: |
296
|
|
|
// GIF image |
297
|
|
|
$img = function_exists('imagecreatefromgif') ? imagecreatefromgif($this->_source_image) : imagecreatefrompng($this->_source_image); |
298
|
|
|
$tmp_img = $imageCreateFunction($newWidth, $newHeight); |
299
|
|
|
$imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); |
300
|
|
|
if (function_exists('imagegif')) { |
301
|
|
|
imagegif($tmp_img, $this->_save_image); |
302
|
|
|
} else { |
303
|
|
|
imagepng($tmp_img, $this->_save_image); |
304
|
|
|
} |
305
|
|
|
imagedestroy($tmp_img); |
306
|
|
|
break; |
307
|
|
|
|
308
|
|
|
case 2: |
309
|
|
|
// echo $this->_save_image; |
|
|
|
|
310
|
|
|
$img = function_exists('imagecreatefromjpeg') ? imagecreatefromjpeg($this->_source_image) : imagecreatefrompng($this->_source_image); |
311
|
|
|
$tmp_img = $imageCreateFunction($newWidth, $newHeight); |
312
|
|
|
$imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); |
313
|
|
|
if (function_exists('imagejpeg')) { |
314
|
|
|
imagejpeg($tmp_img, $this->_save_image, $this->img_quality); |
315
|
|
|
} else { |
316
|
|
|
imagepng($tmp_img, $this->_save_image); |
317
|
|
|
} |
318
|
|
|
imagedestroy($tmp_img); |
319
|
|
|
break; |
320
|
|
|
|
321
|
|
|
case 3: |
322
|
|
|
// PNG image |
323
|
|
|
$img = imagecreatefrompng($this->_source_image); |
324
|
|
|
$tmp_img = $imageCreateFunction($newWidth, $newHeight); |
325
|
|
|
$imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); |
326
|
|
|
imagepng($tmp_img, $this->_save_image); |
327
|
|
|
imagedestroy($tmp_img); |
328
|
|
|
break; |
329
|
|
|
default: |
330
|
|
|
return false; |
331
|
|
|
} |
332
|
|
|
if ($this->_return_fullpath == 1) { |
333
|
|
|
return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; |
334
|
|
|
} else { |
335
|
|
|
return "{$this->_img_savepath}/{$savefile}"; |
336
|
|
|
} |
337
|
|
|
break; |
|
|
|
|
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return false; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* ThumbsNails::checkPaths() |
345
|
|
|
* |
346
|
|
|
* @return bool |
347
|
|
|
*/ |
348
|
|
|
public function checkPaths() |
349
|
|
|
{ |
350
|
|
|
if (file_exists($this->_source_image) || is_readable($this->_source_image)) { |
351
|
|
|
return true; |
352
|
|
|
} |
353
|
|
|
if (is_dir($this->_save_image) || is_writable($this->_save_image)) { |
354
|
|
|
return true; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
return false; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @return bool |
362
|
|
|
*/ |
363
|
|
|
public function checkImage() |
364
|
|
|
{ |
365
|
|
|
$this->_img_info = getimagesize($this->_source_image, $imageinfo); |
366
|
|
|
// if ( $this->_img_info[0] < $this->img_width && $this->_img_info[1] < $this->img_height ) |
|
|
|
|
367
|
|
|
// return false; |
368
|
|
|
return !(null === $this->_img_info); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* wfsThumbs::gd_lib_check() |
373
|
|
|
* |
374
|
|
|
* Private function |
375
|
|
|
* |
376
|
|
|
* @return array|false if gd lib not found on the system |
377
|
|
|
*/ |
378
|
|
|
public function gd_lib_check() |
379
|
|
|
{ |
380
|
|
|
if (!extension_loaded('gd')) { |
381
|
|
|
return false; |
382
|
|
|
} |
383
|
|
|
$gdlib = function_exists('gd_info'); |
|
|
|
|
384
|
|
|
if (false === $gdlib = gd_info()) { |
385
|
|
|
return false; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
return $gdlib; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* ThumbsNails::isUsingThumbs() |
393
|
|
|
* |
394
|
|
|
* @return bool |
395
|
|
|
*/ |
396
|
|
|
public function isUsingThumbs() |
397
|
|
|
{ |
398
|
|
|
if ($this->_usethumbs) { |
399
|
|
|
return true; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
return false; |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.