Completed
Branch master (9dda00)
by Michael
04:54 queued 02:33
created

functions.image.php ➔ newbb_createThumbnail()   F

Complexity

Conditions 30
Paths 8006

Size

Total Lines 119
Code Lines 84

Duplication

Lines 16
Ratio 13.45 %

Importance

Changes 0
Metric Value
cc 30
eloc 84
nc 8006
nop 2
dl 16
loc 119
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
//  ------------------------------------------------------------------------ //
4
//                XOOPS - PHP Content Management System                      //
5
//                  Copyright (c) 2000-2016 XOOPS.org                        //
6
//                       <http://xoops.org/>                             //
7
//  ------------------------------------------------------------------------ //
8
//  This program is free software; you can redistribute it and/or modify     //
9
//  it under the terms of the GNU General Public License as published by     //
10
//  the Free Software Foundation; either version 2 of the License, or        //
11
//  (at your option) any later version.                                      //
12
//                                                                           //
13
//  You may not change or alter any portion of this comment or credits       //
14
//  of supporting developers from this source code or any supporting         //
15
//  source code which is considered copyrighted (c) material of the          //
16
//  original comment or credit authors.                                      //
17
//                                                                           //
18
//  This program is distributed in the hope that it will be useful,          //
19
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21
//  GNU General Public License for more details.                             //
22
//                                                                           //
23
//  You should have received a copy of the GNU General Public License        //
24
//  along with this program; if not, write to the Free Software              //
25
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26
//  ------------------------------------------------------------------------ //
27
//  Author: phppp (D.J., [email protected])                                  //
28
//  URL: http://xoops.org                                                    //
29
//  Project: Article Project                                                 //
30
//  ------------------------------------------------------------------------ //
31
32
if (!defined('NEWBB_FUNCTIONS_IMAGE')) {
33
    define('NEWBB_FUNCTIONS_IMAGE', true);
34
35
    /**
36
     * @param $source
37
     * @return string
38
     */
39
    function newbbAttachmentImage($source)
0 ignored issues
show
Coding Style introduced by
newbbAttachmentImage uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
40
    {
41
        $img_path   = $GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments']);
42
        $img_url    = XOOPS_URL . '/' . $GLOBALS['xoopsModuleConfig']['dir_attachments'];
43
        $thumb_path = $img_path . '/thumbs';
44
        $thumb_url  = $img_url . '/thumbs';
45
46
        $thumb     = $thumb_path . '/' . $source;
47
        $image     = $img_path . '/' . $source;
48
        $thumb_url = $thumb_url . '/' . $source;
49
        $image_url = $img_url . '/' . $source;
50
51
        $imginfo  = @getimagesize($image);
52
        $img_info = (count($imginfo) > 0) ? $imginfo[0] . 'X' . $imginfo[1] . ' px' : '';
53
54
        if ($GLOBALS['xoopsModuleConfig']['max_image_width'] > 0
55
            && $GLOBALS['xoopsModuleConfig']['max_image_height'] > 0) {
56
            if ($imginfo[0] > $GLOBALS['xoopsModuleConfig']['max_image_width']
57
                || $imginfo[1] > $GLOBALS['xoopsModuleConfig']['max_image_height']) {
58
                //if (!file_exists($thumb_path.'/'.$source) && $imginfo[0] > $GLOBALS['xoopsModuleConfig']['max_img_width']) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% 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...
59
                if (!file_exists($thumb_path . '/' . $source)) {
60
                    newbbCreateThumbnail($source, $GLOBALS['xoopsModuleConfig']['max_image_width']);
61
                }
62
            }
63
64
            if ($imginfo[0] > $GLOBALS['xoopsModuleConfig']['max_image_width']
65
                || $imginfo[1] > $GLOBALS['xoopsModuleConfig']['max_image_height']) {
66
                $pseudo_width  = $GLOBALS['xoopsModuleConfig']['max_image_width'];
67
                $pseudo_height = $GLOBALS['xoopsModuleConfig']['max_image_width'] * ($imginfo[1] / $imginfo[0]);
68
                $pseudo_size   = "width='" . $pseudo_width . "px' height='" . $pseudo_height . "px'";
69
            }
70
            // irmtfan to fix Undefined variable: pseudo_height
71
            if (!empty($pseudo_height) && $GLOBALS['xoopsModuleConfig']['max_image_height'] > 0
72
                && $pseudo_height > $GLOBALS['xoopsModuleConfig']['max_image_height']) {
73
                $pseudo_height = $GLOBALS['xoopsModuleConfig']['max_image_height'];
74
                $pseudo_width  = $GLOBALS['xoopsModuleConfig']['max_image_height'] * ($imginfo[0] / $imginfo[1]);
75
                $pseudo_size   = "width='" . $pseudo_width . "px' height='" . $pseudo_height . "px'";
76
            }
77
        }
78
79
        if (file_exists($thumb)) {
80
            $attachmentImage = '<a href="' . $image_url . '" title="' . $source . ' ' . $img_info . '" target="_blank">';
81
            $attachmentImage .= '<img src="' . $thumb_url . '" alt="' . $source . ' ' . $img_info . '" />';
82
            $attachmentImage .= '</a>';
83
        } elseif (!empty($pseudo_size)) {
84
            $attachmentImage = '<a href="' . $image_url . '" title="' . $source . ' ' . $img_info . '" target="_blank">';
85
            $attachmentImage .= '<img src="' . $image_url . '" ' . $pseudo_size . ' alt="' . $source . ' ' . $img_info . '" />';
86
            $attachmentImage .= '</a>';
87
        } elseif (file_exists($image)) {
88
            $attachmentImage = '<img src="' . $image_url . '" alt="' . $source . ' ' . $img_info . '" />';
89
        } else {
90
            $attachmentImage = '';
91
        }
92
93
        return $attachmentImage;
94
    }
95
96
    /**
97
     * @param $source
98
     * @param $thumb_width
99
     * @return bool
100
     */
101
    function newbbCreateThumbnail($source, $thumb_width)
0 ignored issues
show
Coding Style introduced by
newbbCreateThumbnail uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
102
    {
103
        $cmd = '';
104
        $img_path   = $GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments']);
105
        $thumb_path = $img_path . '/thumbs';
106
        $src_file   = $img_path . '/' . $source;
107
        $new_file   = $thumb_path . '/' . $source;
108
        //$imageLibs = newbb_getImageLibs();
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...
109
110
        if (!filesize($src_file) || !is_readable($src_file)) {
111
            return false;
112
        }
113
114
        if (!is_dir($thumb_path) || !is_writable($thumb_path)) {
115
            return false;
116
        }
117
118
        $imginfo = @getimagesize($src_file);
119
120
        if (null === $imginfo) {
121
            return false;
122
        }
123
        if ($imginfo[0] < $thumb_width) {
124
            return false;
125
        }
126
127
        $newWidth  = (int)min($imginfo[0], $thumb_width);
128
        $newHeight = (int)($imginfo[1] * $newWidth / $imginfo[0]);
129
130
        if ($GLOBALS['xoopsModuleConfig']['image_lib'] == 1 || $GLOBALS['xoopsModuleConfig']['image_lib'] == 0) {
131
            if (preg_match("#[A-Z]:|\\\\#Ai", __FILE__)) {
132
                $cur_dir     = __DIR__;
133
                $src_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $src_file) . '"';
134
                $new_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $new_file) . '"';
135
            } else {
136
                $src_file_im = @escapeshellarg($src_file);
137
                $new_file_im = @escapeshellarg($new_file);
138
            }
139
            $path           = empty($GLOBALS['xoopsModuleConfig']['path_magick']) ? '' : $GLOBALS['xoopsModuleConfig']['path_magick'] . '/';
140
            $magick_command = $path . 'convert -quality 85 -antialias -sample ' . $newWidth . 'x' . $newHeight . ' ' . $src_file_im . ' +profile "*" ' . str_replace('\\', '/', $new_file_im) . '';
141
142
            @passthru($magick_command);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
143
            if (file_exists($new_file)) {
144
                return true;
145
            }
146
        }
147
148
        if ($GLOBALS['xoopsModuleConfig']['image_lib'] == 2 || $GLOBALS['xoopsModuleConfig']['image_lib'] == 0) {
149
            $path = empty($GLOBALS['xoopsModuleConfig']['path_netpbm']) ? '' : $GLOBALS['xoopsModuleConfig']['path_netpbm'] . '/';
150
            if (preg_match("/\.png/", $source)) {
151
                $cmd = $path . "pngtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "pnmtopng > $new_file";
152
            } elseif (preg_match("/\.(jpg|jpeg)/", $source)) {
153
                $cmd = $path . "jpegtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "ppmtojpeg -quality=90 > $new_file";
154
            } elseif (preg_match("/\.gif/", $source)) {
155
                $cmd = $path . "giftopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | ppmquant 256 | " . $path . "ppmtogif > $new_file";
156
            }
157
158
            @exec($cmd, $output, $retval);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

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...
159
            if (file_exists($new_file)) {
160
                return true;
161
            }
162
        }
163
164
        $type            = $imginfo[2];
165
        $supported_types = [];
166
167
        if (!extension_loaded('gd')) {
168
            return false;
169
        }
170
        if (function_exists('imagegif')) {
171
            $supported_types[] = 1;
172
        }
173
        if (function_exists('imagejpeg')) {
174
            $supported_types[] = 2;
175
        }
176
        if (function_exists('imagepng')) {
177
            $supported_types[] = 3;
178
        }
179
180
        $imageCreateFunction = function_exists('imagecreatetruecolor') ? 'imagecreatetruecolor' : 'imagecreate';
181
182
        if (in_array($type, $supported_types)) {
183
            switch ($type) {
184
                case 1:
185
                    if (!function_exists('imagecreatefromgif')) {
186
                        return false;
187
                    }
188
                    $im     = imagecreatefromgif($src_file);
189
                    $new_im = imagecreate($newWidth, $newHeight);
190
                    imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
191
                    imagegif($new_im, $new_file);
192
                    imagedestroy($im);
193
                    imagedestroy($new_im);
194
                    break;
195 View Code Duplication
                case 2:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
                    $im     = imagecreatefromjpeg($src_file);
197
                    $new_im = $imageCreateFunction($newWidth, $newHeight);
198
                    imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
199
                    imagejpeg($new_im, $new_file, 90);
200
                    imagedestroy($im);
201
                    imagedestroy($new_im);
202
                    break;
203 View Code Duplication
                case 3:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
                    $im     = imagecreatefrompng($src_file);
205
                    $new_im = $imageCreateFunction($newWidth, $newHeight);
206
                    imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]);
207
                    imagepng($new_im, $new_file);
208
                    imagedestroy($im);
209
                    imagedestroy($new_im);
210
                    break;
211
            }
212
        }
213
214
        if (file_exists($new_file)) {
215
            return true;
216
        } else {
217
            return false;
218
        }
219
    }
220
}
221