| Conditions | 27 |
| Paths | 3893 |
| Total Lines | 134 |
| Code Lines | 90 |
| Lines | 0 |
| Ratio | 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 |
||
| 2 | function image_processing($image_input, $image_output, $width = null, $height = null,$imagefield='') { |
||
| 3 | |||
| 4 | /* |
||
| 5 | * this handles all resizing of images that results in a file being saved, if no width and height is supplied, then it just copies the image |
||
| 6 | */ |
||
| 7 | $imagetype = getimagesize($image_input); |
||
| 8 | if(file_exists($image_input) && is_numeric($height) && is_numeric($width) && function_exists('imagecreatefrompng') && (($height != $imagetype[1]) && ($width != $imagetype[0]))) { |
||
| 9 | switch($imagetype[2]) { |
||
| 10 | case IMAGETYPE_JPEG: |
||
| 11 | $src_img = imagecreatefromjpeg($image_input); |
||
| 12 | $pass_imgtype = true; |
||
| 13 | break; |
||
| 14 | |||
| 15 | case IMAGETYPE_GIF: |
||
| 16 | $src_img = imagecreatefromgif($image_input); |
||
| 17 | $pass_imgtype = true; |
||
| 18 | break; |
||
| 19 | |||
| 20 | case IMAGETYPE_PNG: |
||
| 21 | $src_img = imagecreatefrompng($image_input); |
||
| 22 | $pass_imgtype = true; |
||
| 23 | break; |
||
| 24 | |||
| 25 | default: |
||
| 26 | move_uploaded_file($image_input, ($imagedir.basename($_FILES[$imagefield]['name']))); |
||
| 27 | $image = esc_attr(basename($_FILES[$imagefield]['name'])); |
||
| 28 | return true; |
||
| 29 | exit(); |
||
| 30 | break; |
||
| 31 | } |
||
| 32 | |||
| 33 | if($pass_imgtype === true) { |
||
| 34 | $source_w = imagesx($src_img); |
||
| 35 | $source_h = imagesy($src_img); |
||
| 36 | |||
| 37 | //Temp dimensions to crop image properly |
||
| 38 | $temp_w = $width; |
||
| 39 | $temp_h = $height; |
||
| 40 | // if the image is wider than it is high and at least as wide as the target width. |
||
| 41 | if (($source_h <= $source_w)) { |
||
| 42 | if ($height < $width ) { |
||
| 43 | $temp_h = ($width / $source_w) * $source_h; |
||
| 44 | } else { |
||
| 45 | $temp_w = ($height / $source_h) * $source_w; |
||
| 46 | } |
||
| 47 | } else { |
||
| 48 | $temp_h = ($width / $source_w) * $source_h; |
||
| 49 | } |
||
| 50 | |||
| 51 | // Create temp resized image |
||
| 52 | $temp_img = ImageCreateTrueColor( $temp_w, $temp_h ); |
||
| 53 | $bgcolor = ImageColorAllocate( $temp_img, 255, 255, 255 ); |
||
| 54 | ImageFilledRectangle( $temp_img, 0, 0, $width, $height, $bgcolor ); |
||
| 55 | ImageAlphaBlending( $temp_img, TRUE ); |
||
| 56 | if($imagetype[2] == IMAGETYPE_PNG) { |
||
| 57 | imagesavealpha($temp_img,true); |
||
| 58 | ImageAlphaBlending($temp_img, false); |
||
| 59 | } |
||
| 60 | |||
| 61 | // resize keeping the perspective |
||
| 62 | Imagecopyresampled( $temp_img, $src_img, 0, 0, 0, 0, $temp_w, $temp_h, $source_w, $source_h ); |
||
| 63 | |||
| 64 | |||
| 65 | if($imagetype[2] == IMAGETYPE_PNG) { |
||
| 66 | imagesavealpha($temp_img,true); |
||
| 67 | ImageAlphaBlending($temp_img, false); |
||
| 68 | } |
||
| 69 | |||
| 70 | |||
| 71 | $dst_img = ImageCreateTrueColor($width,$height); |
||
| 72 | $white = ImageColorAllocate( $dst_img, 255, 255, 255 ); |
||
| 73 | ImageFilledRectangle( $dst_img, 0, 0, $width, $height, $white ); |
||
| 74 | ImageAlphaBlending($dst_img, TRUE ); |
||
| 75 | imagecolortransparent($dst_img, $white); |
||
| 76 | |||
| 77 | |||
| 78 | // X & Y Offset to crop image properly |
||
| 79 | if($temp_w < $width) { |
||
| 80 | $w1 = ($width/2) - ($temp_w/2); |
||
| 81 | } else if($temp_w == $width) { |
||
| 82 | $w1 = 0; |
||
| 83 | } else { |
||
| 84 | $w1 = ($width/2) - ($temp_w/2); |
||
| 85 | } |
||
| 86 | |||
| 87 | if($imagetype[2] == IMAGETYPE_PNG) { |
||
| 88 | imagesavealpha($dst_img,true); |
||
| 89 | ImageAlphaBlending($dst_img, false); |
||
| 90 | } |
||
| 91 | |||
| 92 | |||
| 93 | // Final thumbnail cropped from the center out. |
||
| 94 | if(!isset($h1)) |
||
| 95 | $h1 = 0; |
||
| 96 | ImageCopy( $dst_img, $temp_img, $w1, $h1, 0, 0, $temp_w, $temp_h ); |
||
| 97 | |||
| 98 | $image_quality = wpsc_image_quality(); |
||
| 99 | |||
| 100 | switch($imagetype[2]) { |
||
| 101 | case IMAGETYPE_JPEG: |
||
| 102 | if(@ ImageJPEG($dst_img, $image_output, $image_quality) == false) { return false; } |
||
| 103 | break; |
||
| 104 | |||
| 105 | case IMAGETYPE_GIF: |
||
| 106 | if(function_exists("ImageGIF")) { |
||
| 107 | if(@ ImageGIF($dst_img, $image_output) == false) { return false; } |
||
| 108 | } else { |
||
| 109 | ImageAlphaBlending($dst_img, false); |
||
| 110 | if(@ ImagePNG($dst_img, $image_output) == false) { return false; } |
||
| 111 | } |
||
| 112 | break; |
||
| 113 | |||
| 114 | case IMAGETYPE_PNG: |
||
| 115 | imagesavealpha($dst_img,true); |
||
| 116 | ImageAlphaBlending($dst_img, false); |
||
| 117 | if(@ ImagePNG($dst_img, $image_output) == false) { return false; } |
||
| 118 | break; |
||
| 119 | } |
||
| 120 | usleep(50000); //wait 0.05 of of a second to process and save the new image |
||
| 121 | imagedestroy($dst_img); |
||
| 122 | //$image_output |
||
| 123 | |||
| 124 | $stat = stat( dirname( $image_output )); |
||
| 125 | $perms = $stat['mode'] & 0000666; |
||
| 126 | @ chmod( $image_output, $perms ); |
||
| 127 | return true; |
||
| 128 | } |
||
| 129 | } else { |
||
| 130 | copy($image_input, $image_output); |
||
| 131 | $image = esc_attr(basename($_FILES[$imagefield]['name'])); |
||
| 132 | return $image; |
||
| 133 | } |
||
| 134 | return false; |
||
| 135 | } |
||
| 136 | |||
| 138 |