Conditions | 5 |
Paths | 8 |
Total Lines | 71 |
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 |
||
71 | private function getOffsetBalancedForImage(\Imagick $image, $targetWidth, $targetHeight) |
||
72 | { |
||
73 | $size = $image->getImageGeometry(); |
||
74 | |||
75 | $points = array(); |
||
76 | |||
77 | $halfWidth = ceil($size['width']/2); |
||
78 | $halfHeight = ceil($size['height']/2); |
||
79 | |||
80 | // First quadrant |
||
81 | $clone = clone($image); |
||
82 | $clone->cropimage($halfWidth, $halfHeight, 0, 0); |
||
83 | $point = $this->getHighestEnergyPoint($clone); |
||
84 | $points[] = array('x' => $point['x'], 'y' => $point['y'], 'sum' => $point['sum']); |
||
85 | |||
86 | // Second quadrant |
||
87 | $clone = clone($image); |
||
88 | $clone->cropimage($halfWidth, $halfHeight, $halfWidth, 0); |
||
89 | $point = $this->getHighestEnergyPoint($clone); |
||
90 | $points[] = array('x' => $point['x']+$halfWidth, 'y' => $point['y'], 'sum' => $point['sum']); |
||
91 | |||
92 | // Third quadrant |
||
93 | $clone = clone($image); |
||
94 | $clone->cropimage($halfWidth, $halfHeight, 0, $halfHeight); |
||
95 | $point = $this->getHighestEnergyPoint($clone); |
||
96 | $points[] = array('x' => $point['x'], 'y' => $point['y']+$halfHeight, 'sum' => $point['sum']); |
||
97 | |||
98 | // Fourth quadrant |
||
99 | $clone = clone($image); |
||
100 | $clone->cropimage($halfWidth, $halfHeight, $halfWidth, $halfHeight); |
||
101 | $point = $point = $this->getHighestEnergyPoint($clone); |
||
102 | $points[] = array('x' => $point['x']+$halfWidth, 'y' => $point['y']+$halfHeight, 'sum' => $point['sum']); |
||
103 | |||
104 | // get the totalt sum value so we can find out a mean center point |
||
105 | $totalWeight = array_reduce( |
||
106 | $points, |
||
107 | function ($result, $array) { |
||
108 | return $result + $array['sum']; |
||
109 | } |
||
110 | ); |
||
111 | |||
112 | $centerX = 0; |
||
113 | $centerY = 0; |
||
114 | |||
115 | // If we found a center point, made the calculations to found the coords |
||
116 | if ($totalWeight) { |
||
117 | // Calulate the mean weighted center x and y |
||
118 | $totalPoints = count($points); |
||
119 | for ($idx=0; $idx < $totalPoints; $idx++) { |
||
120 | $centerX += $points[$idx]['x'] * ($points[$idx]['sum'] / $totalWeight); |
||
121 | $centerY += $points[$idx]['y'] * ($points[$idx]['sum'] / $totalWeight); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | // From the weighted center point to the topleft corner of the crop would be |
||
126 | $topleftX = max(0, ($centerX - $targetWidth / 2)); |
||
127 | $topleftY = max(0, ($centerY - $targetHeight / 2)); |
||
128 | |||
129 | // If we don't have enough width for the crop, back up $topleftX until |
||
130 | // we can make the image meet $targetWidth |
||
131 | if ($topleftX + $targetWidth > $size['width']) { |
||
132 | $topleftX -= ($topleftX+$targetWidth) - $size['width']; |
||
133 | } |
||
134 | // If we don't have enough height for the crop, back up $topleftY until |
||
135 | // we can make the image meet $targetHeight |
||
136 | if ($topleftY+$targetHeight > $size['height']) { |
||
137 | $topleftY -= ($topleftY+$targetHeight) - $size['height']; |
||
138 | } |
||
139 | |||
140 | return array('x'=>$topleftX, 'y'=>$topleftY); |
||
141 | } |
||
142 | |||
190 |