Conditions | 30 |
Paths | 8006 |
Total Lines | 119 |
Code Lines | 84 |
Lines | 16 |
Ratio | 13.45 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
101 | function newbbCreateThumbnail($source, $thumb_width) |
||
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(); |
||
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); |
||
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); |
||
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: |
|
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: |
|
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 |
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: