newbbAttachmentImage()   F
last analyzed

Complexity

Conditions 18
Paths 256

Size

Total Lines 64
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 18
eloc 41
nc 256
nop 1
dl 0
loc 64
rs 3.3333
c 4
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 declare(strict_types=1);
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
//  Author: phppp (D.J., [email protected])                                  //
13
//  URL: https://xoops.org                                                    //
14
//  Project: Article Project                                                 //
15
//  ------------------------------------------------------------------------ //
16
17
if (!defined('NEWBB_FUNCTIONS_IMAGE')) {
18
    define('NEWBB_FUNCTIONS_IMAGE', true);
19
20
    /**
21
     * @param string $source
22
     * @return string
23
     */
24
    function newbbAttachmentImage(string $source): string
25
    {
26
        $pseudo_size = null;
27
        $img_path   = $GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments']);
28
        $img_url    = XOOPS_URL . '/' . $GLOBALS['xoopsModuleConfig']['dir_attachments'];
29
        $thumb_path = $img_path . '/thumbs';
30
        $thumb_url  = $img_url . '/thumbs';
31
32
        $thumb     = $thumb_path . '/' . $source;
33
        $image     = $img_path . '/' . $source;
34
        $thumb_url .= '/' . $source;
35
        $image_url = $img_url . '/' . $source;
36
        $img_info  = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $img_info is dead and can be removed.
Loading history...
37
38
        $imginfo = @getimagesize($image);
39
40
        // Change by BigKev73 - Removed the is_array check, otherwise the img_info is never set
41
        //if (is_array($image)) {
42
        $img_info = (is_array($imginfo) && count($imginfo) > 0) ? $imginfo[0] . 'X' . $imginfo[1] . ' px' : '';
43
        //}
44
45
        if (is_array($imginfo) && $GLOBALS['xoopsModuleConfig']['max_image_width'] > 0
46
            && $GLOBALS['xoopsModuleConfig']['max_image_height'] > 0) {
47
            if ($imginfo[0] > $GLOBALS['xoopsModuleConfig']['max_image_width']
48
                || $imginfo[1] > $GLOBALS['xoopsModuleConfig']['max_image_height']) {
49
                //if (!file_exists($thumb_path.'/'.$source) && $imginfo[0] > $GLOBALS['xoopsModuleConfig']['max_img_width']) {
50
                if (!file_exists($thumb_path . '/' . $source)) {
51
                    newbbCreateThumbnail($source, $GLOBALS['xoopsModuleConfig']['max_image_width']);
52
                }
53
            }
54
        }
55
56
        //BigKev73 Change to remove height value
57
58
        if (is_array($imginfo) && ($imginfo[0] > $GLOBALS['xoopsModuleConfig']['max_image_width']
59
            || $imginfo[1] > $GLOBALS['xoopsModuleConfig']['max_image_height'])) {
60
            $pseudo_width  = $GLOBALS['xoopsModuleConfig']['max_image_width'];
61
            $pseudo_height = $GLOBALS['xoopsModuleConfig']['max_image_width'] * ($imginfo[1] / $imginfo[0]);
62
            $pseudo_size   = "width='" . $pseudo_width . "' height='" . $pseudo_height . "'";
63
        }
64
        // irmtfan to fix Undefined variable: pseudo_height
65
        if (!empty($pseudo_height) && $GLOBALS['xoopsModuleConfig']['max_image_height'] > 0
66
            && $pseudo_height > $GLOBALS['xoopsModuleConfig']['max_image_height']) {
67
            $pseudo_height = $GLOBALS['xoopsModuleConfig']['max_image_height'];
68
            $pseudo_width  = $GLOBALS['xoopsModuleConfig']['max_image_height'] * ($imginfo[0] / $imginfo[1]);
69
            $pseudo_size   = "width='" . $pseudo_width . "' height='" . $pseudo_height . "'";
70
        }
71
72
        //BigKev73 Change to add max with property to properly scale photos
73
        if (file_exists($thumb)) {
74
            $attachmentImage = '<a href="' . $image_url . '" title="' . $source . ' ' . $img_info . '" target="_blank">';
75
            $attachmentImage .= '<img src="' . $thumb_url . '" ' . $pseudo_size . ' alt="' . $source . ' ' . $img_info . '" style="max-width: 100%; height: auto;">';
76
            $attachmentImage .= '</a>';
77
        } elseif (!empty($pseudo_size)) {
78
            $attachmentImage = '<a href="' . $image_url . '" title="' . $source . ' ' . $img_info . '" target="_blank">';
79
            $attachmentImage .= '<img src="' . $image_url . '" ' . $pseudo_size . ' alt="' . $source . ' ' . $img_info . '" style="max-width: 100%; height: auto;">';
80
            $attachmentImage .= '</a>';
81
        } elseif (file_exists($image)) {
82
            $attachmentImage = '<img src="' . $image_url . '" alt="' . $source . ' ' . $img_info . '" style="width:' . $imginfo[0] . '" height="' . $imginfo[1] . '" style="max-width: 100%; height: auto;">';
83
        } else {
84
            $attachmentImage = '';
85
        }
86
87
        return $attachmentImage;
88
    }
89
90
    /**
91
     * @param string $source
92
     * @param int $thumb_width
93
     * @return bool
94
     */
95
    function newbbCreateThumbnail(string $source, int $thumb_width): bool
96
    {
97
        $cmd        = '';
98
        $img_path   = $GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments']);
99
        $thumb_path = $img_path . '/thumbs';
100
        $src_file   = $img_path . '/' . $source;
101
        $new_file   = $thumb_path . '/' . $source;
102
        //$imageLibs = newbb_getImageLibs();
103
104
        if (!filesize($src_file) || !is_readable($src_file)) {
105
            return false;
106
        }
107
108
        if (!is_dir($thumb_path) || !is_writable($thumb_path)) {
109
            return false;
110
        }
111
112
        $imginfo = @getimagesize($src_file);
113
114
        if (empty($imginfo)) {
115
            return false;
116
        }
117
        if ($imginfo[0] < $thumb_width) {
118
            return false;
119
        }
120
121
        $newWidth  = (int)min($imginfo[0], $thumb_width);
122
        $newHeight = (int)($imginfo[1] * $newWidth / $imginfo[0]);
123
124
        if (1 == $GLOBALS['xoopsModuleConfig']['image_lib'] || 0 == $GLOBALS['xoopsModuleConfig']['image_lib']) {
125
            if (preg_match('#[A-Z]:|\\\\#Ai', __FILE__)) {
126
                $cur_dir     = __DIR__;
127
                $src_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $src_file) . '"';
128
                $new_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $new_file) . '"';
129
            } else {
130
                $src_file_im = @escapeshellarg($src_file);
131
                $new_file_im = @escapeshellarg($new_file);
132
            }
133
            $path           = empty($GLOBALS['xoopsModuleConfig']['path_magick']) ? '' : $GLOBALS['xoopsModuleConfig']['path_magick'] . '/';
134
            $magick_command = $path . 'convert -auto-orient -quality 85 -antialias -sample ' . $newWidth . 'x' . $newHeight . ' ' . $src_file_im . ' +profile "*" ' . str_replace('\\', '/', $new_file_im) . '';
135
136
            @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

136
            /** @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...
137
            if (file_exists($new_file)) {
138
                return true;
139
            }
140
        }
141
142
        if (2 == $GLOBALS['xoopsModuleConfig']['image_lib'] || 0 == $GLOBALS['xoopsModuleConfig']['image_lib']) {
143
            $path = empty($GLOBALS['xoopsModuleConfig']['path_netpbm']) ? '' : $GLOBALS['xoopsModuleConfig']['path_netpbm'] . '/';
144
            if (preg_match('/\.png$/', $source)) {
145
                $cmd = $path . "pngtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "pnmtopng > $new_file";
146
            } elseif (preg_match('/\.(jpg|jpeg)$/', $source)) {
147
                $cmd = $path . "jpegtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "ppmtojpeg -quality=90 > $new_file";
148
            } elseif (preg_match('/\.gif$/', $source)) {
149
                $cmd = $path . "giftopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | ppmquant 256 | " . $path . "ppmtogif > $new_file";
150
            }
151
152
            @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

152
            /** @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...
153
            if (file_exists($new_file)) {
154
                return true;
155
            }
156
        }
157
158
        $type            = $imginfo[2];
159
        $supported_types = [];
160
161
        if (!extension_loaded('gd')) {
162
            return false;
163
        }
164
        if (function_exists('imagegif')) {
165
            $supported_types[] = 1;
166
        }
167
        if (function_exists('imagejpeg')) {
168
            $supported_types[] = 2;
169
        }
170
        if (function_exists('imagepng')) {
171
            $supported_types[] = 3;
172
        }
173
174
        $imageCreateFunction = function_exists('imagecreatetruecolor') ? 'imagecreatetruecolor' : 'imagecreate';
175
176
        if (in_array($type, $supported_types, true)) {
177
            switch ($type) {
178
                case 1:
179
                    if (!function_exists('imagecreatefromgif')) {
180
                        return false;
181
                    }
182
                    $im     = imagecreatefromgif($src_file);
183
                    $new_im = imagecreate($newWidth, $newHeight);
184
                    imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
185
                    imagegif($new_im, $new_file);
186
                    imagedestroy($im);
187
                    imagedestroy($new_im);
188
                    break;
189
                case 2:
190
                    $im     = imagecreatefromjpeg($src_file);
191
                    $new_im = $imageCreateFunction($newWidth, $newHeight);
192
                    imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
193
                    imagejpeg($new_im, $new_file, 90);
194
                    imagedestroy($im);
195
                    imagedestroy($new_im);
196
                    break;
197
                case 3:
198
                    $im     = imagecreatefrompng($src_file);
199
                    $new_im = $imageCreateFunction($newWidth, $newHeight);
200
                    imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
201
                    imagepng($new_im, $new_file);
202
                    imagedestroy($im);
203
                    imagedestroy($new_im);
204
                    break;
205
            }
206
        }
207
208
        return file_exists($new_file);
209
    }
210
}
211