Completed
Push — master ( 8ca430...3024c9 )
by Michael
03:12
created

class/class_thumbnail.php (6 issues)

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-2013 The XOOPS Project
15
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
16
 * @version         $Id$
17
 * @link            http://sourceforge.net/projects/xoops/
18
 * @since           1.0.6
19
 */
20
21
if (!defined('_PATH')) {
22
    define("_PATH", XOOPS_ROOT_PATH);
23
}
24
25
if (!defined('DEFAULT_PATH')) {
26
    define('DEFAULT_PATH', XOOPS_UPLOAD_URL . '/blank.gif');
27
}
28
29
/**
30
 * Class XtubeThumbsNails
31
 */
32
class XtubeThumbsNails
33
{
34
    public $_imgName = 'blank.gif';
35
    public $_img_path = 'uploads';
36
    public $_img_savepath = 'thumbs';
37
38
    public $_source_path = '';
39
    public $_save_path = '';
40
    public $_source_url = '';
41
    public $_source_image = '';
42
    public $_save_image = '';
43
44
    public $_usethumbs = 0;
45
    public $_image_type = 'gd2';
46
    public $_return_fullpath = 0;
47
48
    public $img_width = 100;
49
    public $img_height = 100;
50
    public $img_quality = 100;
51
    public $img_update = 1;
52
    public $img_aspect = 1;
53
54
    // @access private
55
    public $_img_info = array();
56
57
    /**
58
     * Constructor
59
     *
60
     * @param null $img_name
61
     * @param null $img_path
62
     * @param null $img_savepath
63
     *
64
     * @internal param string $_imgName
65
     * @internal param string $_img_path
66
     * @internal param string $_img_savepath
67
     * @return \XtubeThumbsNails
68
     */
69
    public function __construct($img_name = null, $img_path = null, $img_savepath = null)
70
    {
71
        if (!preg_match("/\.(jpg|gif|png|jpeg)$/i", $img_name)) {
72
            return false;
73
        }
74
75
        // The actual image we will be processing
76
        if (!is_null($img_name)) {
77
            $this->_imgName = strval(trim($img_name));
78
        }
79
80
        // The image path
81
        if (!is_null($img_path)) {
82
            $this->_img_path = strval(trim($img_path));
83
        }
84
85
        // The image save path
86
        if (!is_null($img_savepath)) {
87
            $this->_img_savepath = strval(trim($img_savepath));
88
        }
89
90
        $path_to_check = XOOPS_ROOT_PATH . "/$img_path/$img_savepath";
91
92
        if (!is_dir($path_to_check)) {
93
            if (false == mkdir("$path_to_check", 0777)) {
94
                return false;
95
            }
96
        }
97
98
        return null;
99
    }
100
101
    /**
102
     * wfThumbsNails::setUseThumbs()
103
     *
104
     * @param integer $value
105
     *
106
     * @return void
107
     */
108
    public function setUseThumbs($value = 1)
109
    {
110
        $this->_usethumbs = $value;
111
    }
112
113
    /**
114
     * XtubeThumbsNails::setImageType()
115
     *
116
     * @param string $value
117
     *
118
     * @return void
119
     */
120
    public function setImageType($value = 'gd2')
121
    {
122
        $this->_image_type = $value;
123
    }
124
125
    /**
126
     * ThumbsNails::createThumbnail()
127
     *
128
     * @param int $img_width
129
     * @param int $img_height
130
     * @param int $img_quality
131
     * @param int $img_update
132
     * @param int $img_aspect
133
     *
134
     * @return bool|string
135
     */
136
    public function createThumbnail(
137
        $img_width = null,
138
        $img_height = null,
139
        $img_quality = null,
140
        $img_update = null,
141
        $img_aspect = null
142
    ) {
143
        $this->_source_path  = XOOPS_ROOT_PATH . "/{$this->_img_path}";
144
        $this->_save_path    = XOOPS_ROOT_PATH . "/{$this->_img_path}/{$this->_img_savepath}";
145
        $this->_source_url   = XOOPS_URL . "/{$this->_img_path}";
146
        $this->_source_image = "{$this->_source_path}/{$this->_imgName}";
147
148
        if (isset($img_width) && !is_null($img_width)) {
149
            $this->img_width = intval($img_width);
150
        }
151
152
        if (isset($img_height) && !is_null($img_height)) {
153
            $this->img_height = intval($img_height);
154
        }
155
156
        if (isset($img_quality) && !is_null($img_quality)) {
157
            $this->img_quality = intval($img_quality);
158
        }
159
160
        if (isset($img_update) && !is_null($img_update)) {
161
            $this->img_update = intval($img_update);
162
        }
163
164
        if (isset($img_aspect) && !is_null($img_aspect)) {
165
            $this->img_aspect = intval($img_aspect);
166
        }
167
168
        // Return false if we are not using thumb nails
169
        if (!$this->useThumbs()) {
170
            return $this->_source_url . '/' . $this->_imgName;
171
        }
172
        // Return false if the server does not have gd lib installed or activated
173
        if (!$this->checkGdLibrary()) {
174
            return $this->_source_url . '/' . $this->_imgName;
175
        }
176
177
        // Return false if the paths to the file are wrong
178
        if (!$this->checkPaths()) {
179
            return DEFAULT_PATH;
180
        }
181
182
        if (!$this->checkImage()) {
183
            return DEFAULT_PATH;
184
        }
185
186
        $image = $this->resizeThumbnail();
187
        if ($image == false) {
188
            return DEFAULT_PATH;
189
        } else {
190
            return $image;
191
        }
192
    }
193
194
    /**
195
     * @param $value
196
     */
197
    public function setImageName($value)
198
    {
199
        $this->_imgName = strval(trim($value));
200
    }
201
202
    /**
203
     * @param $value
204
     */
205
    public function setImagePath($value)
206
    {
207
        $this->_img_path = strval(trim($value));
208
    }
209
210
    /**
211
     * @param $value
212
     */
213
    public function setImgSavePath($value)
214
    {
215
        $this->_img_savepath = strval(trim($value));
216
    }
217
218
    // ThumbsNails::resizeThumbnail()
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
219
    // @return
220
    /**
221
     * @return bool|string
222
     */
223
    public function resizeThumbnail()
224
    {
225
        global $xoopsModuleConfig;
226
227
        // Get image size and scale ratio
228
        $scale = min($this->img_width / $this->_img_info[0], $this->img_height / $this->_img_info[1]);
229
        // If the image is larger than the max shrink it
230
        $newWidth  = $this->img_width;
231
        $newHeight = $this->img_height;
232
        if ($scale < 1 && $this->img_aspect == 1) {
233
            $newWidth  = floor($scale * $this->_img_info[0]);
234
            $newHeight = floor($scale * $this->_img_info[1]);
235
        }
236
        $newWidth  = ($newWidth > $this->_img_info[0]) ? $this->_img_info[0] : $newWidth;
237
        $newHeight = ($newHeight > $this->_img_info[0]) ? $this->_img_info[0] : $newHeight;
238
239
        $savefile          = "{$newWidth}x{$newHeight}_{$this->_imgName}";
240
        $this->_save_image = "{$this->_save_path}/{$savefile}";
241
242
        if ($this->img_update == 0 && file_exists($this->_save_image)) {
243 View Code Duplication
            if ($this->_return_fullpath == 1) {
244
                return $this->_source_url . "/{$this->_img_savepath}/{$savefile}";
245
            } else {
246
                return "{$this->_img_savepath}/{$savefile}";
247
            }
248
        }
249
250
        switch ($this->_image_type) {
251
            case 'im':
252
                if (!empty($xoopsModuleConfig['path_magick']) && is_dir($xoopsModuleConfig['path_magick'])) {
253
                    if (preg_match("#[A-Z]:|\\\\#Ai", __FILE__)) {
254
                        $cur_dir     = __DIR__;
255
                        $src_file_im = '"' . $cur_dir . '\\' . strtr($this->_source_image, '/', '\\') . '"';
256
                        $new_file_im = '"' . $cur_dir . '\\' . strtr($this->_save_image, '/', '\\') . '"';
257
                    } else {
258
                        $src_file_im = escapeshellarg($this->_source_image);
259
                        $new_file_im = escapeshellarg($this->_save_image);
260
                    }
261
                    $magick_command
262
                        = $xoopsModuleConfig['path_magick'] . '/convert -quality {$xoopsModuleConfig["imagequality"]} -antialias -sample {$newWidth}x{$newHeight} {$src_file_im} +profile "*" '
263
                        . str_replace('\\', '/', $new_file_im) . '';
264
                    passthru($magick_command);
265
266
                    return $this->_source_url . "/{$this->_img_savepath}/{$savefile}";
267
                } else {
268
                    return false;
269
                }
270
271
                break;
272
273
            case 'gd1':
274
            case 'gd2':
275
            default :
0 ignored issues
show
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
276
277
                $imageCreateFunction = (function_exists('imagecreatetruecolor')
278
                    && $this->_image_type == 'gd2') ? 'imagecreatetruecolor' : 'imagecreate';
279
                $imageCopyfunction   = (function_exists('ImageCopyResampled')
280
                    && $this->_image_type == 'gd2') ? 'imagecopyresampled' : 'imagecopyresized';
281
282
                switch ($this->_img_info[2]) {
283 View Code Duplication
                    case 1:
284
                        // GIF image
285
                        $img     = (function_exists('imagecreatefromgif')) ? imagecreatefromgif(
286
                            $this->_source_image
287
                        ) : imageCreateFromPNG($this->_source_image);
288
                        $tmp_img = $imageCreateFunction($newWidth, $newHeight);
289
                        $imageCopyfunction(
290
                            $tmp_img,
291
                            $img,
292
                            0,
293
                            0,
294
                            0,
295
                            0,
296
                            $newWidth,
297
                            $newHeight,
298
                            $this->_img_info[0],
299
                            $this->_img_info[1]
300
                        );
301
                        if (function_exists('imagegif')) {
302
                            imagegif($tmp_img, $this->_save_image);
303
                        } else {
304
                            imagePNG($tmp_img, $this->_save_image);
305
                        }
306
                        imagedestroy($tmp_img);
307
                        break;
308
309 View Code Duplication
                    case 2:
310
                        // echo $this->_save_image;
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
311
                        $img     = (function_exists('imagecreatefromjpeg')) ? imageCreateFromJPEG(
312
                            $this->_source_image
313
                        ) : imageCreateFromPNG($this->_source_image);
314
                        $tmp_img = $imageCreateFunction($newWidth, $newHeight);
315
                        $imageCopyfunction(
316
                            $tmp_img,
317
                            $img,
318
                            0,
319
                            0,
320
                            0,
321
                            0,
322
                            $newWidth,
323
                            $newHeight,
324
                            $this->_img_info[0],
325
                            $this->_img_info[1]
326
                        );
327
                        if (function_exists('imagejpeg')) {
328
                            imageJPEG($tmp_img, $this->_save_image, $this->img_quality);
329
                        } else {
330
                            imagePNG($tmp_img, $this->_save_image);
331
                        }
332
                        imagedestroy($tmp_img);
333
                        break;
334
335
                    case 3:
336
                        // PNG image
337
                        $img     = imageCreateFromPNG($this->_source_image);
338
                        $tmp_img = $imageCreateFunction($newWidth, $newHeight);
339
                        $imageCopyfunction(
340
                            $tmp_img,
341
                            $img,
342
                            0,
343
                            0,
344
                            0,
345
                            0,
346
                            $newWidth,
347
                            $newHeight,
348
                            $this->_img_info[0],
349
                            $this->_img_info[1]
350
                        );
351
                        imagePNG($tmp_img, $this->_save_image);
352
                        imagedestroy($tmp_img);
353
                        break;
354
                    default:
355
                        return false;
356
                }
357 View Code Duplication
                if ($this->_return_fullpath == 1) {
358
                    return $this->_source_url . "/{$this->_img_savepath}/{$savefile}";
359
                } else {
360
                    return "{$this->_img_savepath}/{$savefile}";
361
                }
362
                break;
363
        }
364
//        return FALSE;
365
    }
366
367
    // ThumbsNails::checkPaths()
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
368
    // @return
369
    /**
370
     * @return bool
371
     */
372
    public function checkPaths()
373
    {
374
        if (file_exists($this->_source_image) || is_readable($this->_source_image)) {
375
            return true;
376
        }
377
        if (is_dir($this->_save_image) || is_writable($this->_save_image)) {
378
            return true;
379
        }
380
381
        return false;
382
    }
383
384
    /**
385
     * @return bool
386
     */
387
    public function checkImage()
388
    {
389
        $this->_img_info = getimagesize($this->_source_image, $imageinfo);
390
        if (null == $this->_img_info) {
391
            return false;
392
        }
393
394
        return true;
395
    }
396
397
    // wfsThumbs::checkGdLibrary()
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
398
    // Private function
399
    // @return false if gd lib not found on the system
400
    /**
401
     * @return array|bool
402
     */
403
    public function checkGdLibrary()
404
    {
405
        if (!extension_loaded('gd')) {
406
            return false;
407
        }
408
        $gdlib = (function_exists('gd_info'));
409
        if (false == $gdlib = gd_info()) {
410
            return false;
411
        }
412
413
        return $gdlib;
414
    }
415
416
    // ThumbsNails::useThumbs()
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
417
    //
418
    // @return
419
    /**
420
     * @return bool
421
     */
422
    public function useThumbs()
423
    {
424
        if ($this->_usethumbs) {
425
            return true;
426
        }
427
428
        return false;
429
    }
430
}
431