Conditions | 16 |
Paths | 160 |
Total Lines | 94 |
Code Lines | 58 |
Lines | 0 |
Ratio | 0 % |
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 |
||
46 | public function execute($image, $amount, $radius, $threshold) |
||
47 | { |
||
48 | // Attempt to calibrate the parameters to Photoshop: |
||
49 | if ($amount > 500) { |
||
50 | $amount = 500; |
||
51 | } |
||
52 | |||
53 | $amount = $amount * 0.016; |
||
54 | |||
55 | if ($radius > 50) { |
||
56 | $radius = 50; |
||
57 | } |
||
58 | |||
59 | $radius = $radius * 2; |
||
60 | |||
61 | if ($threshold > 255) { |
||
62 | $threshold = 255; |
||
63 | } |
||
64 | |||
65 | $radius = abs(round($radius)); // Only integers make sense. |
||
66 | |||
67 | if ($radius == 0) { |
||
68 | return $image; |
||
69 | } |
||
70 | |||
71 | // Gaussian blur matrix |
||
72 | $matrix = array( |
||
73 | array(1, 2, 1), |
||
74 | array(2, 4, 2), |
||
75 | array(1, 2, 1) |
||
76 | ); |
||
77 | |||
78 | $blurred = $image->applyConvolution($matrix, 16, 0); |
||
79 | |||
80 | if ($threshold > 0) { |
||
81 | // Calculate the difference between the blurred pixels and the original |
||
82 | // and set the pixels |
||
83 | for ($x = 0; $x < $image->getWidth(); $x++) { |
||
84 | for ($y = 0; $y < $image->getHeight(); $y++) { |
||
85 | $rgbOrig = $image->getRGBAt($x, $y); |
||
86 | $rOrig = $rgbOrig["red"]; |
||
87 | $gOrig = $rgbOrig["green"]; |
||
88 | $bOrig = $rgbOrig["blue"]; |
||
89 | |||
90 | $rgbBlur = $blurred->getRGBAt($x, $y); |
||
91 | $rBlur = $rgbBlur["red"]; |
||
92 | $gBlur = $rgbBlur["green"]; |
||
93 | $bBlur = $rgbBlur["blue"]; |
||
94 | |||
95 | // When the masked pixels differ less from the original |
||
96 | // than the threshold specifies, they are set to their original value. |
||
97 | $rNew = (abs($rOrig - $rBlur) >= $threshold) |
||
98 | ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig)) |
||
99 | : $rOrig; |
||
100 | $gNew = (abs($gOrig - $gBlur) >= $threshold) |
||
101 | ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig)) |
||
102 | : $gOrig; |
||
103 | $bNew = (abs($bOrig - $bBlur) >= $threshold) |
||
104 | ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig)) |
||
105 | : $bOrig; |
||
106 | $rgbNew = array("red" => $rNew, "green" => $gNew, "blue" => $bNew, "alpha" => 0); |
||
107 | |||
108 | if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) { |
||
109 | $image->setRGBAt($x, $y, $rgbNew); |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | } else { |
||
114 | $w = $image->getWidth(); |
||
115 | $h = $image->getHeight(); |
||
116 | |||
117 | for ($x = 0; $x < $w; $x++) { |
||
118 | for ($y = 0; $y < $h; $y++) { |
||
119 | $rgbOrig = $image->getRGBAt($x, $y); |
||
120 | $rOrig = $rgbOrig["red"]; |
||
121 | $gOrig = $rgbOrig["green"]; |
||
122 | $bOrig = $rgbOrig["blue"]; |
||
123 | |||
124 | $rgbBlur = $blurred->getRGBAt($x, $y); |
||
125 | $rBlur = $rgbBlur["red"]; |
||
126 | $gBlur = $rgbBlur["green"]; |
||
127 | $bBlur = $rgbBlur["blue"]; |
||
128 | |||
129 | $rNew = static::bit(($amount * ($rOrig - $rBlur)) + $rOrig); |
||
130 | $gNew = static::bit(($amount * ($gOrig - $gBlur)) + $gOrig); |
||
131 | $bNew = static::bit(($amount * ($bOrig - $bBlur)) + $bOrig); |
||
132 | $rgbNew = array("red" => $rNew, "green" => $gNew, "blue" => $bNew, "alpha" => 0); |
||
133 | |||
134 | $image->setRGBAt($x, $y, $rgbNew); |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | return $image; |
||
140 | } |
||
155 |