| Conditions | 30 |
| Paths | 8006 |
| Total Lines | 120 |
| Code Lines | 82 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 declare(strict_types=1); |
||
| 91 | function sf_createThumbnail($source, $thumb_width) |
||
| 92 | { |
||
| 93 | /** @var Smartfaq\Helper $helper */ |
||
| 94 | $helper = Smartfaq\Helper::getInstance(); |
||
| 95 | |||
| 96 | $img_path = XOOPS_ROOT_PATH . '/' . $helper->getConfig('dir_attachments'); |
||
| 97 | $thumb_path = $img_path . '/thumbs'; |
||
| 98 | $src_file = $img_path . '/' . $source; |
||
| 99 | $new_file = $thumb_path . '/' . $source; |
||
| 100 | //$imageLibs = sf_getImageLibs(); |
||
| 101 | |||
| 102 | if (!filesize($src_file) || !is_readable($src_file)) { |
||
| 103 | return false; |
||
| 104 | } |
||
| 105 | |||
| 106 | if (!is_dir($thumb_path) || !is_writable($thumb_path)) { |
||
| 107 | return false; |
||
| 108 | } |
||
| 109 | |||
| 110 | $imginfo = @getimagesize($src_file); |
||
| 111 | |||
| 112 | if (null === $imginfo) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | if ($imginfo[0] < $thumb_width) { |
||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | $newWidth = (int)min($imginfo[0], $thumb_width); |
||
| 120 | $newHeight = (int)($imginfo[1] * $newWidth / $imginfo[0]); |
||
| 121 | |||
| 122 | if (1 == $helper->getConfig('image_lib') || 0 == $helper->getConfig('image_lib')) { |
||
| 123 | if (preg_match('#[A-Z]:|\\\\#Ai', __FILE__)) { |
||
| 124 | $cur_dir = __DIR__; |
||
| 125 | $src_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $src_file) . '"'; |
||
| 126 | $new_file_im = '"' . $cur_dir . '\\' . str_replace('/', '\\', $new_file) . '"'; |
||
| 127 | } else { |
||
| 128 | $src_file_im = @escapeshellarg($src_file); |
||
| 129 | $new_file_im = @escapeshellarg($new_file); |
||
| 130 | } |
||
| 131 | $path = empty($helper->getConfig('path_magick')) ? '' : $helper->getConfig('path_magick') . '/'; |
||
| 132 | $magick_command = $path . 'convert -auto-orient -quality 85 -antialias -sample ' . $newWidth . 'x' . $newHeight . ' ' . $src_file_im . ' +profile "*" ' . str_replace('\\', '/', $new_file_im); |
||
| 133 | |||
| 134 | @passthru($magick_command); |
||
| 135 | if (file_exists($new_file)) { |
||
| 136 | return true; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | if (2 == $helper->getConfig('image_lib') || 0 == $helper->getConfig('image_lib')) { |
||
| 141 | $path = empty($helper->getConfig('path_netpbm')) ? '' : $helper->getConfig('path_netpbm') . '/'; |
||
| 142 | if (preg_match('/\.png$/i', $source)) { |
||
| 143 | $cmd = $path . "pngtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "pnmtopng > $new_file"; |
||
| 144 | } elseif (preg_match('/\.(jpg|jpeg)$/i', $source)) { |
||
| 145 | $cmd = $path . "jpegtopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | " . $path . "ppmtojpeg -quality=90 > $new_file"; |
||
| 146 | } elseif (preg_match('/\.gif$/i', $source)) { |
||
| 147 | $cmd = $path . "giftopnm $src_file | " . $path . "pnmscale -xysize $newWidth $newHeight | ppmquant 256 | " . $path . "ppmtogif > $new_file"; |
||
| 148 | } |
||
| 149 | |||
| 150 | @exec($cmd, $output, $retval); |
||
| 151 | if (file_exists($new_file)) { |
||
| 152 | return true; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $type = $imginfo[2]; |
||
| 157 | $supported_types = []; |
||
| 158 | |||
| 159 | if (!extension_loaded('gd')) { |
||
| 160 | return false; |
||
| 161 | } |
||
| 162 | if (function_exists('imagegif')) { |
||
| 163 | $supported_types[] = 1; |
||
| 164 | } |
||
| 165 | if (function_exists('imagejpeg')) { |
||
| 166 | $supported_types[] = 2; |
||
| 167 | } |
||
| 168 | if (function_exists('imagepng')) { |
||
| 169 | $supported_types[] = 3; |
||
| 170 | } |
||
| 171 | |||
| 172 | $imageCreateFunction = function_exists('imagecreatetruecolor') ? 'imagecreatetruecolor' : 'imagecreate'; |
||
| 173 | |||
| 174 | if (in_array($type, $supported_types, true)) { |
||
| 175 | switch ($type) { |
||
| 176 | case 1: |
||
| 177 | if (!function_exists('imagecreatefromgif')) { |
||
| 178 | return false; |
||
| 179 | } |
||
| 180 | $im = imagecreatefromgif($src_file); |
||
| 181 | $new_im = imagecreate($newWidth, $newHeight); |
||
| 182 | imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]); |
||
| 183 | imagegif($new_im, $new_file); |
||
| 184 | imagedestroy($im); |
||
| 185 | imagedestroy($new_im); |
||
| 186 | break; |
||
| 187 | case 2: |
||
| 188 | $im = imagecreatefromjpeg($src_file); |
||
| 189 | $new_im = $imageCreateFunction($newWidth, $newHeight); |
||
| 190 | imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]); |
||
| 191 | imagejpeg($new_im, $new_file, 90); |
||
| 192 | imagedestroy($im); |
||
| 193 | imagedestroy($new_im); |
||
| 194 | break; |
||
| 195 | case 3: |
||
| 196 | $im = imagecreatefrompng($src_file); |
||
| 197 | $new_im = $imageCreateFunction($newWidth, $newHeight); |
||
| 198 | imagecopyresized($new_im, $im, 0, 0, 0, 0, $newWidth, $newHeight, $imginfo[0], $imginfo[1]); |
||
| 199 | imagepng($new_im, $new_file); |
||
| 200 | imagedestroy($im); |
||
| 201 | imagedestroy($new_im); |
||
| 202 | break; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | if (file_exists($new_file)) { |
||
| 207 | return true; |
||
| 208 | } |
||
| 209 | |||
| 210 | return false; |
||
| 211 | } |
||
| 214 |