| Conditions | 42 |
| Paths | 1378 |
| Total Lines | 199 |
| Code Lines | 113 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 124 | function processAction($action, $relative, $fullpath) |
||
| 125 | { |
||
| 126 | $params = ''; |
||
| 127 | |||
| 128 | if(isset($_GET['params'])) |
||
| 129 | $params = $_GET['params']; |
||
| 130 | |||
| 131 | $values = explode(',',$params,4); |
||
| 132 | $saveFile = $this->getSaveFileName($values[0]); |
||
| 133 | |||
| 134 | $img = Image_Transform::factory(IMAGE_CLASS); |
||
| 135 | $img->load($fullpath); |
||
| 136 | |||
| 137 | switch ($action) |
||
| 138 | { |
||
| 139 | case 'replace': |
||
| 140 | |||
| 141 | // 'ImageManager.php' handled the uploaded file, it's now on the server. |
||
| 142 | // If maximum size is specified, constrain image to it. |
||
| 143 | $dimensionsIndex = isset($_REQUEST['uploadSize']) ? $_REQUEST['uploadSize'] : 0; |
||
| 144 | if ($this->manager->config['maxWidth'][$dimensionsIndex] > 0 && $this->manager->config['maxHeight'][$dimensionsIndex] > 0 && ($img->img_x > $this->manager->config['maxWidth'][$dimensionsIndex] || $img->img_y > $this->manager->config['maxHeight'][$dimensionsIndex])) |
||
| 145 | { |
||
| 146 | $percentage = min($this->manager->config['maxWidth'][$dimensionsIndex]/$img->img_x, $this->manager->config['maxHeight'][$dimensionsIndex]/$img->img_y); |
||
| 147 | $img->scale($percentage); |
||
| 148 | } |
||
| 149 | |||
| 150 | break; |
||
| 151 | |||
| 152 | case 'watermark': |
||
| 153 | |||
| 154 | // loading target image |
||
| 155 | $functionName = 'ImageCreateFrom' . $img->type; |
||
| 156 | if(function_exists($functionName)) |
||
| 157 | { |
||
| 158 | $imageResource = $functionName($fullpath); |
||
| 159 | } |
||
| 160 | else |
||
| 161 | { |
||
| 162 | echo "<script>alert(\"Error when loading '" . basename($fullpath) . "' - Loading '" . $img->type . "' files not supported\");</script>"; |
||
| 163 | return false; |
||
| 164 | } |
||
| 165 | |||
| 166 | // loading watermark |
||
| 167 | $watermarkFullPath = $_GET['watermarkFullPath']; |
||
| 168 | $watermarkImageType = strtolower(substr($watermarkFullPath, strrpos($watermarkFullPath, ".") + 1)); |
||
| 169 | if ($watermarkImageType == "jpg") { $watermarkImageType = "jpeg"; } |
||
| 170 | if ($watermarkImageType == "tif") { $watermarkImageType = "tiff"; } |
||
| 171 | $functionName = 'ImageCreateFrom' . $watermarkImageType; |
||
| 172 | if(function_exists($functionName)) |
||
| 173 | { |
||
| 174 | $watermarkResource = $functionName($watermarkFullPath); |
||
| 175 | } |
||
| 176 | else |
||
| 177 | { |
||
| 178 | echo "<script>alert(\"Error when loading '" . basename($watermarkFullPath) . "' - Loading '" . $img->type . "' files not supported\");</script>"; |
||
| 179 | return false; |
||
| 180 | } |
||
| 181 | |||
| 182 | $numberOfColors = imagecolorstotal($watermarkResource); |
||
| 183 | |||
| 184 | $watermarkX = isset($_GET['watermarkX']) ? $_GET['watermarkX'] : -1; |
||
| 185 | $watermarkY = isset($_GET['watermarkY']) ? $_GET['watermarkY'] : -1; |
||
| 186 | $opacity = $_GET['opacity']; |
||
| 187 | |||
| 188 | // PNG24 watermark on GIF target needs special handling |
||
| 189 | // PNG24 watermark with alpha transparency on other targets need also this handling |
||
| 190 | if ($watermarkImageType == "png" && $numberOfColors == 0 && ($img->type == "gif" || $opacity < 100)) |
||
| 191 | { |
||
| 192 | require_once('Classes/api.watermark.php'); |
||
| 193 | |||
| 194 | $watermarkAPI = new watermark(); |
||
| 195 | $imageResource = $watermarkAPI->create_watermark($imageResource, $watermarkResource, $opacity, $watermarkX, $watermarkY); |
||
| 196 | } |
||
| 197 | // PNG24 watermark without alpha transparency on other targets than GIF can use 'imagecopy' |
||
| 198 | elseif ($watermarkImageType == "png" && $numberOfColors == 0 && $opacity == 100) |
||
| 199 | { |
||
| 200 | $watermark_width = imagesx($watermarkResource); |
||
| 201 | $watermark_height = imagesy($watermarkResource); |
||
| 202 | |||
| 203 | imagecopy($imageResource, $watermarkResource, $watermarkX, $watermarkY, 0, 0, $watermark_width, $watermark_height); |
||
| 204 | } |
||
| 205 | // Other watermarks can be appllied no swet on all targets |
||
| 206 | else |
||
| 207 | { |
||
| 208 | $watermark_width = imagesx($watermarkResource); |
||
| 209 | $watermark_height = imagesy($watermarkResource); |
||
| 210 | |||
| 211 | imagecopymerge($imageResource, $watermarkResource, $watermarkX, $watermarkY, 0, 0, $watermark_width, $watermark_height, $opacity); |
||
| 212 | } |
||
| 213 | |||
| 214 | break; |
||
| 215 | |||
| 216 | case 'crop': |
||
| 217 | $img->crop(intval($values[0]),intval($values[1]), |
||
| 218 | intval($values[2]),intval($values[3])); |
||
| 219 | break; |
||
| 220 | case 'scale': |
||
| 221 | $img->resize(intval($values[0]),intval($values[1])); |
||
| 222 | break; |
||
| 223 | case 'rotate': |
||
| 224 | $img->rotate(floatval($values[0])); |
||
| 225 | break; |
||
| 226 | case 'flip': |
||
| 227 | if ($values[0] == 'hoz') |
||
| 228 | $img->flip(true); |
||
| 229 | else if($values[0] == 'ver') |
||
| 230 | $img->flip(false); |
||
| 231 | break; |
||
| 232 | case 'save': |
||
| 233 | if(!is_null($saveFile)) |
||
| 234 | { |
||
| 235 | $quality = intval($values[1]); |
||
| 236 | if($quality <0) $quality = 85; |
||
| 237 | $newSaveFile = $this->makeRelative($relative, $saveFile); |
||
| 238 | $oldSaveFile = $newSaveFile; |
||
| 239 | |||
| 240 | if ($this->manager->config['allow_newFileName'] && $this->manager->config['allow_overwrite'] == false) |
||
| 241 | { |
||
| 242 | // check whether a file already exist and if there is, create a variant of the filename |
||
| 243 | $newName = $this->getUniqueFilename($newSaveFile); |
||
| 244 | //get unique filename just returns the filename, so |
||
| 245 | //we need to make the relative path again. |
||
| 246 | $newSaveFile = $this->makeRelative($relative, $newName); |
||
| 247 | } |
||
| 248 | |||
| 249 | // forced new name? |
||
| 250 | if ($oldSaveFile != $newSaveFile) |
||
| 251 | { |
||
| 252 | $this->forcedNewName = $newName; |
||
| 253 | } |
||
| 254 | else |
||
| 255 | { |
||
| 256 | $this->forcedNewName = false; |
||
| 257 | } |
||
| 258 | |||
| 259 | $newSaveFullpath = $this->manager->getFullPath($newSaveFile); |
||
| 260 | $img->save($newSaveFullpath, $values[0], $quality); |
||
| 261 | if(is_file($newSaveFullpath)) |
||
| 262 | $this->filesaved = 1; |
||
| 263 | else |
||
| 264 | $this->filesaved = -1; |
||
| 265 | } |
||
| 266 | break; |
||
| 267 | } |
||
| 268 | |||
| 269 | //create the tmp image file |
||
| 270 | $filename = $this->createUnique($fullpath); |
||
| 271 | $newRelative = $this->makeRelative($relative, $filename); |
||
| 272 | $newFullpath = $this->manager->getFullPath($newRelative); |
||
| 273 | $newURL = $this->manager->getFileURL($newRelative); |
||
| 274 | |||
| 275 | // when uploaded and not resized, rename and don't save |
||
| 276 | if ($action == "replace" && $percentage <= 0) |
||
| 277 | { |
||
| 278 | rename($fullpath, $newFullpath); |
||
| 279 | } |
||
| 280 | // when watermarked, save to new filename |
||
| 281 | elseif ($action == "watermark") |
||
| 282 | { |
||
| 283 | // save image |
||
| 284 | $functionName = 'image' . $img->type; |
||
| 285 | if(function_exists($functionName)) |
||
| 286 | { |
||
| 287 | if($type=='jpeg') |
||
| 288 | $functionName($imageResource, $newFullpath, 100); |
||
| 289 | else |
||
| 290 | $functionName($imageResource, $newFullpath); |
||
| 291 | } |
||
| 292 | else |
||
| 293 | { |
||
| 294 | echo "<script>alert(\"Error when saving '" . basename($newFullpath) . "' - Saving '" . $img->type . "' files not supported\");</script>"; |
||
| 295 | return false; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | else |
||
| 299 | { |
||
| 300 | //save the file. |
||
| 301 | $img->save($newFullpath); |
||
| 302 | $img->free(); |
||
| 303 | } |
||
| 304 | |||
| 305 | // when uploaded was resized and saved, remove original |
||
| 306 | if ($action == "replace" && $percentage > 0) |
||
| 307 | { |
||
| 308 | unlink($fullpath); |
||
| 309 | } |
||
| 310 | |||
| 311 | //get the image information |
||
| 312 | $imgInfo = @getimagesize($newFullpath); |
||
| 313 | |||
| 314 | $image['src'] = $newURL; |
||
| 315 | $image['dimensions'] = $imgInfo[3]; |
||
| 316 | $image['width'] = $imgInfo[0]; |
||
| 317 | $image['height'] = $imgInfo[1]; |
||
| 318 | $image['file'] = $newRelative; |
||
| 319 | $image['fullpath'] = $newFullpath; |
||
| 320 | |||
| 321 | |||
| 322 | Return $image; |
||
| 323 | |||
| 587 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.