Completed
Branch master (ddc2b8)
by Michael
02:46
created

class/class_thumbnail.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Module: XoopsTube
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 *
9
 * PHP version 5
10
 *
11
 * @category        Module
12
 * @package         Xoopstube
13
 * @author          XOOPS Development Team
14
 * @copyright       2001-2016 XOOPS Project (http://xoops.org)
15
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
16
 * @link            http://xoops.org/
17
 * @since           1.0.6
18
 */
19
20
if (!defined('_PATH')) {
21
    define('_PATH', XOOPS_ROOT_PATH);
22
}
23
24
if (!defined('DEFAULT_PATH')) {
25
    define('DEFAULT_PATH', XOOPS_UPLOAD_URL . '/blank.gif');
26
}
27
28
/**
29
 * Class XtubeThumbsNails
30
 */
31
class XtubeThumbsNails
32
{
33
    public $_imgName      = 'blank.gif';
34
    public $_img_path     = 'uploads';
35
    public $_img_savepath = 'thumbs';
36
37
    public $_source_path  = '';
38
    public $_save_path    = '';
39
    public $_source_url   = '';
40
    public $_source_image = '';
41
    public $_save_image   = '';
42
43
    public $_usethumbs       = 0;
44
    public $_image_type      = 'gd2';
45
    public $_return_fullpath = 0;
46
47
    public $img_width   = 100;
48
    public $img_height  = 100;
49
    public $img_quality = 100;
50
    public $img_update  = 1;
51
    public $img_aspect  = 1;
52
53
    // @access private
54
    public $_img_info = array();
55
56
    /**
57
     * Constructor
58
     *
59
     * @param null $img_name
60
     * @param null $img_path
61
     * @param null $img_savepath
62
     *
63
     * @internal param string $_imgName
64
     * @internal param string $_img_path
65
     * @internal param string $_img_savepath
66
     * @return \XtubeThumbsNails
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
67
     */
68
    public function __construct($img_name = null, $img_path = null, $img_savepath = null)
69
    {
70
        if (!preg_match("/\.(jpg|gif|png|jpeg)$/i", $img_name)) {
71
            return false;
72
        }
73
74
        // The actual image we will be processing
75
        if (!null === $img_name) {
76
            $this->_imgName = (string)trim($img_name);
77
        }
78
79
        // The image path
80
        if (!null === $img_path) {
81
            $this->_img_path = (string)trim($img_path);
82
        }
83
84
        // The image save path
85
        if (!null === $img_savepath) {
86
            $this->_img_savepath = (string)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
     * XtubeThumbsNails::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::createThumbnail()
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 createThumbnail(
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->_imgName}";
146
147
        if (isset($img_width) && !null === $img_width) {
148
            $this->img_width = (int)$img_width;
149
        }
150
151
        if (isset($img_height) && !null === $img_height) {
152
            $this->img_height = (int)$img_height;
153
        }
154
155
        if (isset($img_quality) && !null === $img_quality) {
156
            $this->img_quality = (int)$img_quality;
157
        }
158
159
        if (isset($img_update) && !null === $img_update) {
160
            $this->img_update = (int)$img_update;
161
        }
162
163
        if (isset($img_aspect) && !null === $img_aspect) {
164
            $this->img_aspect = (int)$img_aspect;
165
        }
166
167
        // Return false if we are not using thumb nails
168
        if (!$this->useThumbs()) {
169
            return $this->_source_url . '/' . $this->_imgName;
170
        }
171
        // Return false if the server does not have gd lib installed or activated
172
        if (!$this->checkGdLibrary()) {
173
            return $this->_source_url . '/' . $this->_imgName;
174
        }
175
176
        // Return false if the paths to the file are wrong
177
        if (!$this->checkPaths()) {
178
            return DEFAULT_PATH;
179
        }
180
181
        if (!$this->checkImage()) {
182
            return DEFAULT_PATH;
183
        }
184
185
        $image = $this->resizeThumbnail();
186
        return false === $image ? DEFAULT_PATH : $image;
187
    }
188
189
    /**
190
     * @param $value
191
     */
192
    public function setImageName($value)
193
    {
194
        $this->_imgName = (string)trim($value);
195
    }
196
197
    /**
198
     * @param $value
199
     */
200
    public function setImagePath($value)
201
    {
202
        $this->_img_path = (string)trim($value);
203
    }
204
205
    /**
206
     * @param $value
207
     */
208
    public function setImgSavePath($value)
209
    {
210
        $this->_img_savepath = (string)trim($value);
211
    }
212
213
    // ThumbsNails::resizeThumbnail()
214
    // @return
215
    /**
216
     * @return bool|string
217
     */
218
    public function resizeThumbnail()
219
    {
220
        // Get image size and scale ratio
221
        $scale = min($this->img_width / $this->_img_info[0], $this->img_height / $this->_img_info[1]);
222
        // If the image is larger than the max shrink it
223
        $newWidth  = $this->img_width;
224
        $newHeight = $this->img_height;
225
        if ($scale < 1 && 1 == $this->img_aspect) {
226
            $newWidth  = floor($scale * $this->_img_info[0]);
227
            $newHeight = floor($scale * $this->_img_info[1]);
228
        }
229
        $newWidth  = ($newWidth > $this->_img_info[0]) ? $this->_img_info[0] : $newWidth;
230
        $newHeight = ($newHeight > $this->_img_info[0]) ? $this->_img_info[0] : $newHeight;
231
232
        $savefile          = "{$newWidth}x{$newHeight}_{$this->_imgName}";
233
        $this->_save_image = "{$this->_save_path}/{$savefile}";
234
235
        if (0 == $this->img_update && file_exists($this->_save_image)) {
236
            return $this->_return_fullpath == 1 ? $this->_source_url . "/{$this->_img_savepath}/{$savefile}" : "{$this->_img_savepath}/{$savefile}";
237
        }
238
239
        switch ($this->_image_type) {
240
            case 'im':
241
                if (!empty($GLOBALS['xoopsModuleConfig']['path_magick']) && is_dir($GLOBALS['xoopsModuleConfig']['path_magick'])) {
242
                    if (preg_match("#[A-Z]:|\\\\#Ai", __FILE__)) {
243
                        $cur_dir     = __DIR__;
244
                        $src_file_im = '"' . $cur_dir . '\\' . strtr($this->_source_image, '/', '\\') . '"';
245
                        $new_file_im = '"' . $cur_dir . '\\' . strtr($this->_save_image, '/', '\\') . '"';
246
                    } else {
247
                        $src_file_im = escapeshellarg($this->_source_image);
248
                        $new_file_im = escapeshellarg($this->_save_image);
249
                    }
250
                    $magick_command = $GLOBALS['xoopsModuleConfig']['path_magick']
251
                                      . '/convert -quality {$GLOBALS["xoopsModuleConfig"]["imagequality"]} -antialias -sample {$newWidth}x{$newHeight} {$src_file_im} +profile "*" ' . str_replace('\\',
252
                                                                                                                                                                                                   '/',
253
                                                                                                                                                                                                   $new_file_im)
254
                                      . '';
255
                    passthru($magick_command);
256
257
                    return $this->_source_url . "/{$this->_img_savepath}/{$savefile}";
258
                } else {
259
                    return false;
260
                }
261
262
                break;
263
264
            case 'gd1':
265
            case 'gd2':
266
            default :
267
268
                $imageCreateFunction = (function_exists('imagecreatetruecolor') && 'gd2' === $this->_image_type) ? 'imagecreatetruecolor' : 'imagecreate';
269
                $imageCopyfunction   = (function_exists('ImageCopyResampled') && 'gd2' === $this->_image_type) ? 'imagecopyresampled' : 'imagecopyresized';
270
271
                switch ($this->_img_info[2]) {
272 View Code Duplication
                    case 1:
273
                        // GIF image
274
                        $img     = function_exists('imagecreatefromgif') ? imagecreatefromgif($this->_source_image) : imagecreatefrompng($this->_source_image);
275
                        $tmp_img = $imageCreateFunction($newWidth, $newHeight);
276
                        $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]);
277
                        if (function_exists('imagegif')) {
278
                            imagegif($tmp_img, $this->_save_image);
279
                        } else {
280
                            imagepng($tmp_img, $this->_save_image);
281
                        }
282
                        imagedestroy($tmp_img);
283
                        break;
284
285 View Code Duplication
                    case 2:
286
                        // echo $this->_save_image;
287
                        $img     = function_exists('imagecreatefromjpeg') ? imagecreatefromjpeg($this->_source_image) : imagecreatefrompng($this->_source_image);
288
                        $tmp_img = $imageCreateFunction($newWidth, $newHeight);
289
                        $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]);
290
                        if (function_exists('imagejpeg')) {
291
                            imagejpeg($tmp_img, $this->_save_image, $this->img_quality);
292
                        } else {
293
                            imagepng($tmp_img, $this->_save_image);
294
                        }
295
                        imagedestroy($tmp_img);
296
                        break;
297
298
                    case 3:
299
                        // PNG image
300
                        $img     = imagecreatefrompng($this->_source_image);
301
                        $tmp_img = $imageCreateFunction($newWidth, $newHeight);
302
                        $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]);
303
                        imagepng($tmp_img, $this->_save_image);
304
                        imagedestroy($tmp_img);
305
                        break;
306
                    default:
307
                        return false;
308
                }
309
                if (1 == $this->_return_fullpath) {
310
                    return $this->_source_url . "/{$this->_img_savepath}/{$savefile}";
311
                } else {
312
                    return "{$this->_img_savepath}/{$savefile}";
313
                }
314
                break;
315
        }
316
        //        return FALSE;
317
    }
318
319
    // ThumbsNails::checkPaths()
320
    // @return
321
    /**
322
     * @return bool
323
     */
324
    public function checkPaths()
325
    {
326
        if (file_exists($this->_source_image) || is_readable($this->_source_image)) {
327
            return true;
328
        }
329
        if (is_dir($this->_save_image) || is_writable($this->_save_image)) {
330
            return true;
331
        }
332
333
        return false;
334
    }
335
336
    /**
337
     * @return bool
338
     */
339
    public function checkImage()
340
    {
341
        $this->_img_info = getimagesize($this->_source_image, $imageinfo);
342
        if (null === $this->_img_info) {
343
            return false;
344
        }
345
346
        return true;
347
    }
348
349
    // wfsThumbs::checkGdLibrary()
350
    // Private function
351
    // @return false if gd lib not found on the system
352
    /**
353
     * @return array|bool
354
     */
355
    public function checkGdLibrary()
356
    {
357
        if (!extension_loaded('gd')) {
358
            return false;
359
        }
360
        $gdlib = function_exists('gd_info');
361
        if (false === $gdlib = gd_info()) {
362
            return false;
363
        }
364
365
        return $gdlib;
366
    }
367
368
    // ThumbsNails::useThumbs()
369
    //
370
    // @return
371
    /**
372
     * @return bool
373
     */
374
    public function useThumbs()
375
    {
376
        if ($this->_usethumbs) {
377
            return true;
378
        }
379
380
        return false;
381
    }
382
}
383