Conditions | 26 |
Paths | 7336 |
Total Lines | 100 |
Code Lines | 73 |
Lines | 50 |
Ratio | 50 % |
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 |
||
218 | public function resizeThumbnail() |
||
219 | { |
||
220 | // Get image size and scale ratio |
||
221 | $scale = min($this->img_width / $this->_img_info[0], $this->img_height / $this->_img_info[1]); |
||
222 | // If the image is larger than the max shrink it |
||
223 | $newWidth = $this->img_width; |
||
224 | $newHeight = $this->img_height; |
||
225 | if ($scale < 1 && 1 == $this->img_aspect) { |
||
226 | $newWidth = floor($scale * $this->_img_info[0]); |
||
227 | $newHeight = floor($scale * $this->_img_info[1]); |
||
228 | } |
||
229 | $newWidth = ($newWidth > $this->_img_info[0]) ? $this->_img_info[0] : $newWidth; |
||
230 | $newHeight = ($newHeight > $this->_img_info[0]) ? $this->_img_info[0] : $newHeight; |
||
231 | |||
232 | $savefile = "{$newWidth}x{$newHeight}_{$this->_imgName}"; |
||
233 | $this->_save_image = "{$this->_save_path}/{$savefile}"; |
||
234 | |||
235 | if (0 == $this->img_update && file_exists($this->_save_image)) { |
||
236 | return $this->_return_fullpath == 1 ? $this->_source_url . "/{$this->_img_savepath}/{$savefile}" : "{$this->_img_savepath}/{$savefile}"; |
||
237 | } |
||
238 | |||
239 | switch ($this->_image_type) { |
||
240 | case 'im': |
||
241 | if (!empty($GLOBALS['xoopsModuleConfig']['path_magick']) && is_dir($GLOBALS['xoopsModuleConfig']['path_magick'])) { |
||
242 | if (preg_match("#[A-Z]:|\\\\#Ai", __FILE__)) { |
||
243 | $cur_dir = __DIR__; |
||
244 | $src_file_im = '"' . $cur_dir . '\\' . strtr($this->_source_image, '/', '\\') . '"'; |
||
245 | $new_file_im = '"' . $cur_dir . '\\' . strtr($this->_save_image, '/', '\\') . '"'; |
||
246 | } else { |
||
247 | $src_file_im = escapeshellarg($this->_source_image); |
||
248 | $new_file_im = escapeshellarg($this->_save_image); |
||
249 | } |
||
250 | $magick_command = $GLOBALS['xoopsModuleConfig']['path_magick'] |
||
251 | . '/convert -quality {$GLOBALS["xoopsModuleConfig"]["imagequality"]} -antialias -sample {$newWidth}x{$newHeight} {$src_file_im} +profile "*" ' . str_replace('\\', |
||
252 | '/', |
||
253 | $new_file_im) |
||
254 | . ''; |
||
255 | passthru($magick_command); |
||
256 | |||
257 | return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; |
||
258 | } else { |
||
259 | return false; |
||
260 | } |
||
261 | |||
262 | break; |
||
263 | |||
264 | case 'gd1': |
||
265 | case 'gd2': |
||
266 | default : |
||
267 | |||
268 | $imageCreateFunction = (function_exists('imagecreatetruecolor') && 'gd2' === $this->_image_type) ? 'imagecreatetruecolor' : 'imagecreate'; |
||
269 | $imageCopyfunction = (function_exists('ImageCopyResampled') && 'gd2' === $this->_image_type) ? 'imagecopyresampled' : 'imagecopyresized'; |
||
270 | |||
271 | switch ($this->_img_info[2]) { |
||
272 | View Code Duplication | case 1: |
|
273 | // GIF image |
||
274 | $img = function_exists('imagecreatefromgif') ? imagecreatefromgif($this->_source_image) : imagecreatefrompng($this->_source_image); |
||
275 | $tmp_img = $imageCreateFunction($newWidth, $newHeight); |
||
276 | $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); |
||
277 | if (function_exists('imagegif')) { |
||
278 | imagegif($tmp_img, $this->_save_image); |
||
279 | } else { |
||
280 | imagepng($tmp_img, $this->_save_image); |
||
281 | } |
||
282 | imagedestroy($tmp_img); |
||
283 | break; |
||
284 | |||
285 | View Code Duplication | case 2: |
|
286 | // echo $this->_save_image; |
||
287 | $img = function_exists('imagecreatefromjpeg') ? imagecreatefromjpeg($this->_source_image) : imagecreatefrompng($this->_source_image); |
||
288 | $tmp_img = $imageCreateFunction($newWidth, $newHeight); |
||
289 | $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); |
||
290 | if (function_exists('imagejpeg')) { |
||
291 | imagejpeg($tmp_img, $this->_save_image, $this->img_quality); |
||
292 | } else { |
||
293 | imagepng($tmp_img, $this->_save_image); |
||
294 | } |
||
295 | imagedestroy($tmp_img); |
||
296 | break; |
||
297 | |||
298 | case 3: |
||
299 | // PNG image |
||
300 | $img = imagecreatefrompng($this->_source_image); |
||
301 | $tmp_img = $imageCreateFunction($newWidth, $newHeight); |
||
302 | $imageCopyfunction($tmp_img, $img, 0, 0, 0, 0, $newWidth, $newHeight, $this->_img_info[0], $this->_img_info[1]); |
||
303 | imagepng($tmp_img, $this->_save_image); |
||
304 | imagedestroy($tmp_img); |
||
305 | break; |
||
306 | default: |
||
307 | return false; |
||
308 | } |
||
309 | if (1 == $this->_return_fullpath) { |
||
310 | return $this->_source_url . "/{$this->_img_savepath}/{$savefile}"; |
||
311 | } else { |
||
312 | return "{$this->_img_savepath}/{$savefile}"; |
||
313 | } |
||
314 | break; |
||
315 | } |
||
316 | // return FALSE; |
||
317 | } |
||
318 | |||
383 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.