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