Passed
Branch master (4e30dc)
by Michael
02:20
created

functions.image.php ➔ sf_createThumbnail()   F

Complexity

Conditions 30
Paths 8006

Size

Total Lines 120
Code Lines 84

Duplication

Lines 16
Ratio 13.33 %

Importance

Changes 0
Metric Value
cc 30
eloc 84
nc 8006
nop 2
dl 16
loc 120
rs 2
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright      {@link https://xoops.org/ XOOPS Project}
14
 * @license        {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package
16
 * @since
17
 * @author         XOOPS Development Team, phppp (D.J., [email protected])
18
 */
19
20
if (!defined('NEWBB_FUNCTIONS_IMAGE')) :
21
    define('NEWBB_FUNCTIONS_IMAGE', true);
22
23
    /**
24
     * @param $source
25
     * @return string
26
     */
27
    function sf_attachmentImage($source)
28
    {
29
        global $xoopsModuleConfig;
30
31
        $img_path   = XOOPS_ROOT_PATH . '/' . $xoopsModuleConfig['dir_attachments'];
0 ignored issues
show
Bug introduced by
The constant XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
32
        $img_url    = XOOPS_URL . '/' . $xoopsModuleConfig['dir_attachments'];
0 ignored issues
show
Bug introduced by
The constant XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
33
        $thumb_path = $img_path . '/thumbs';
34
        $thumb_url  = $img_url . '/thumbs';
35
36
        $thumb     = $thumb_path . '/' . $source;
37
        $image     = $img_path . '/' . $source;
38
        $thumb_url = $thumb_url . '/' . $source;
39
        $image_url = $img_url . '/' . $source;
40
41
        $imginfo  = @getimagesize($image);
42
        $img_info = (count($imginfo) > 0) ? $imginfo[0] . 'X' . $imginfo[1] . ' px' : '';
0 ignored issues
show
Bug introduced by
It seems like $imginfo can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        $img_info = (count(/** @scrutinizer ignore-type */ $imginfo) > 0) ? $imginfo[0] . 'X' . $imginfo[1] . ' px' : '';
Loading history...
43
44
        if ($xoopsModuleConfig['max_image_width'] > 0 && $xoopsModuleConfig['max_image_height'] > 0) {
45
            if ($imginfo[0] > $xoopsModuleConfig['max_image_width']
46
                || $imginfo[1] > $xoopsModuleConfig['max_image_height']) {
47
                //if (!file_exists($thumb_path.'/'.$source) && $imginfo[0] > $xoopsModuleConfig['max_img_width']) {
48
                if (!file_exists($thumb_path . '/' . $source)) {
49
                    sf_createThumbnail($source, $xoopsModuleConfig['max_image_width']);
50
                }
51
            }
52
53
            if ($imginfo[0] > $xoopsModuleConfig['max_image_width']
54
                || $imginfo[1] > $xoopsModuleConfig['max_image_height']) {
55
                $pseudo_width  = $xoopsModuleConfig['max_image_width'];
56
                $pseudo_height = $xoopsModuleConfig['max_image_width'] * ($imginfo[1] / $imginfo[0]);
57
                $pseudo_size   = "width='" . $pseudo_width . "px' height='" . $pseudo_height . "px'";
58
            }
59
            // irmtfan to fix Undefined variable: pseudo_height
60
            if (!empty($pseudo_height) && $xoopsModuleConfig['max_image_height'] > 0
61
                && $pseudo_height > $xoopsModuleConfig['max_image_height']) {
62
                $pseudo_height = $xoopsModuleConfig['max_image_height'];
63
                $pseudo_width  = $xoopsModuleConfig['max_image_height'] * ($imginfo[0] / $imginfo[1]);
64
                $pseudo_size   = "width='" . $pseudo_width . "px' height='" . $pseudo_height . "px'";
65
            }
66
        }
67
68
        if (file_exists($thumb)) {
69
            $attachmentImage = '<a href="' . $image_url . '" title="' . $source . ' ' . $img_info . '" target="_blank">';
70
            $attachmentImage .= '<img src="' . $thumb_url . '" alt="' . $source . ' ' . $img_info . '">';
71
            $attachmentImage .= '</a>';
72
        } elseif (!empty($pseudo_size)) {
73
            $attachmentImage = '<a href="' . $image_url . '" title="' . $source . ' ' . $img_info . '" target="_blank">';
74
            $attachmentImage .= '<img src="' . $image_url . '" ' . $pseudo_size . ' alt="' . $source . ' ' . $img_info . '">';
75
            $attachmentImage .= '</a>';
76
        } elseif (file_exists($image)) {
77
            $attachmentImage = '<img src="' . $image_url . '" alt="' . $source . ' ' . $img_info . '">';
78
        } else {
79
            $attachmentImage = '';
80
        }
81
82
        return $attachmentImage;
83
    }
84
85
    /**
86
     * @param $source
87
     * @param $thumb_width
88
     * @return bool
89
     */
90
    function sf_createThumbnail($source, $thumb_width)
91
    {
92
        global $xoopsModuleConfig;
93
94
        $img_path   = XOOPS_ROOT_PATH . '/' . $xoopsModuleConfig['dir_attachments'];
0 ignored issues
show
Bug introduced by
The constant XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
95
        $thumb_path = $img_path . '/thumbs';
96
        $src_file   = $img_path . '/' . $source;
97
        $new_file   = $thumb_path . '/' . $source;
98
        //$imageLibs = sf_getImageLibs();
99
100
        if (!filesize($src_file) || !is_readable($src_file)) {
101
            return false;
102
        }
103
104
        if (!is_dir($thumb_path) || !is_writable($thumb_path)) {
105
            return false;
106
        }
107
108
        $imginfo = @getimagesize($src_file);
109
110
        if (null === $imginfo) {
0 ignored issues
show
introduced by
The condition null === $imginfo is always false.
Loading history...
111
            return false;
112
        }
113
        if ($imginfo[0] < $thumb_width) {
114
            return false;
115
        }
116
117
        $newWidth  = (int)min($imginfo[0], $thumb_width);
118
        $newHeight = (int)($imginfo[1] * $newWidth / $imginfo[0]);
119
120
        if (1 == $xoopsModuleConfig['image_lib'] or 0 == $xoopsModuleConfig['image_lib']) {
121
            if (preg_match("#[A-Z]:|\\\\#Ai", __FILE__)) {
122
                $cur_dir     = __DIR__;
123
                $src_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $src_file) . '"';
124
                $new_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $new_file) . '"';
125
            } else {
126
                $src_file_im = @escapeshellarg($src_file);
127
                $new_file_im = @escapeshellarg($new_file);
128
            }
129
            $path           = empty($xoopsModuleConfig['path_magick']) ? '' : $xoopsModuleConfig['path_magick'] . '/';
130
            $magick_command = $path . 'convert -quality 85 -antialias -sample ' . $newWidth . 'x' . $newHeight . ' ' . $src_file_im . ' +profile "*" ' . str_replace('\\', '/', $new_file_im) . '';
131
132
            @passthru($magick_command);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for passthru(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

132
            /** @scrutinizer ignore-unhandled */ @passthru($magick_command);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
Are you sure the usage of passthru($magick_command) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
133
            if (file_exists($new_file)) {
134
                return true;
135
            }
136
        }
137
138
        if (2 == $xoopsModuleConfig['image_lib'] or 0 == $xoopsModuleConfig['image_lib']) {
139
            $path = empty($xoopsModuleConfig['path_netpbm']) ? '' : $xoopsModuleConfig['path_netpbm'] . '/';
140
            if (preg_match("/\.png/i", $source)) {
141
                $cmd = $path . "pngtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "pnmtopng > $new_file";
142
            } elseif (preg_match("/\.(jpg|jpeg)/i", $source)) {
143
                $cmd = $path . "jpegtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "ppmtojpeg -quality=90 > $new_file";
144
            } elseif (preg_match("/\.gif/i", $source)) {
145
                $cmd = $path . "giftopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | ppmquant 256 | " . $path . "ppmtogif > $new_file";
146
            }
147
148
            @exec($cmd, $output, $retval);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for exec(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

148
            /** @scrutinizer ignore-unhandled */ @exec($cmd, $output, $retval);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Comprehensibility Best Practice introduced by
The variable $cmd does not seem to be defined for all execution paths leading up to this point.
Loading history...
149
            if (file_exists($new_file)) {
150
                return true;
151
            }
152
        }
153
154
        $type            = $imginfo[2];
155
        $supported_types = [];
156
157
        if (!extension_loaded('gd')) {
158
            return false;
159
        }
160
        if (function_exists('imagegif')) {
161
            $supported_types[] = 1;
162
        }
163
        if (function_exists('imagejpeg')) {
164
            $supported_types[] = 2;
165
        }
166
        if (function_exists('imagepng')) {
167
            $supported_types[] = 3;
168
        }
169
170
        $imageCreateFunction = function_exists('imagecreatetruecolor') ? 'imagecreatetruecolor' : 'imagecreate';
171
172
        if (in_array($type, $supported_types)) {
173
            switch ($type) {
174
                case 1:
175
                    if (!function_exists('imagecreatefromgif')) {
176
                        return false;
177
                    }
178
                    $im     = imagecreatefromgif($src_file);
179
                    $new_im = imagecreate($newWidth, $newHeight);
180
                    imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
181
                    imagegif($new_im, $new_file);
182
                    imagedestroy($im);
183
                    imagedestroy($new_im);
184
                    break;
185
                case 2:
186
                    $im     = imagecreatefromjpeg($src_file);
187
                    $new_im = $imageCreateFunction($newWidth, $newHeight);
188
                    imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
189
                    imagejpeg($new_im, $new_file, 90);
190
                    imagedestroy($im);
191
                    imagedestroy($new_im);
192
                    break;
193
                case 3:
194
                    $im     = imagecreatefrompng($src_file);
195
                    $new_im = $imageCreateFunction($newWidth, $newHeight);
196
                    imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
197
                    imagepng($new_im, $new_file);
198
                    imagedestroy($im);
199
                    imagedestroy($new_im);
200
                    break;
201
            }
202
        }
203
204
        if (file_exists($new_file)) {
205
            return true;
206
        } else {
207
            return false;
208
        }
209
    }
210
211
endif;
212