Completed
Push — master ( ab284c...4fdc7c )
by Justin
07:07
created

image_processing.php ➔ image_processing()   F

Complexity

Conditions 27
Paths 3893

Size

Total Lines 134
Code Lines 90

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 27
eloc 90
nc 3893
nop 5
dl 0
loc 134
rs 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
function image_processing($image_input, $image_output, $width = null, $height = null,$imagefield='') {
0 ignored issues
show
introduced by
Expected 1 space between default value and equals sign for argument "$imagefield";
Loading history...
introduced by
Expected 1 space between argument "$imagefield" and equals sign; 0 found
Loading history...
introduced by
Expected 1 space between comma and argument "$imagefield"; 0 found
Loading history...
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'])));
0 ignored issues
show
Bug introduced by
The variable $imagedir does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
27
			$image = esc_attr(basename($_FILES[$imagefield]['name']));
28
			return true;
29
			exit();
0 ignored issues
show
Unused Code introduced by
die; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
30
			break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
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 );
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
64
65
			if($imagetype[2] == IMAGETYPE_PNG) {
66
				imagesavealpha($temp_img,true);
67
				ImageAlphaBlending($temp_img, false);
68
			}
69
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
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 );
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
75
			imagecolortransparent($dst_img, $white);
76
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
92
93
			// Final thumbnail cropped from the center out.
94
			if(!isset($h1))
0 ignored issues
show
Bug introduced by
The variable $h1 seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
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; }
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
103
				break;
104
105
				case IMAGETYPE_GIF:
106
				if(function_exists("ImageGIF")) {
107
					if(@ ImageGIF($dst_img, $image_output) == false) { return false; }
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
108
				} else {
109
					ImageAlphaBlending($dst_img, false);
110
					if(@ ImagePNG($dst_img, $image_output) == false) { return false; }
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
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; }
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
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 );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
introduced by
Filesystem writes are forbidden, you should not be using chmod()
Loading history...
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
137
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
138