Conditions | 4 |
Paths | 8 |
Total Lines | 68 |
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 | protected static function getOffsetBalanced(Imagick $original, $targetWidth, $targetHeight) |
||
47 | { |
||
48 | $size = $original->getImageGeometry(); |
||
49 | |||
50 | $points = []; |
||
51 | |||
52 | $halfWidth = ceil($size['width'] / 2); |
||
53 | $halfHeight = ceil($size['height'] / 2); |
||
54 | |||
55 | // First quadrant |
||
56 | $clone = clone $original; |
||
57 | $clone->cropimage($halfWidth, $halfHeight, 0, 0); |
||
58 | $point = static::getHighestEnergyPoint($clone); |
||
59 | $points[] = ['x' => $point['x'], 'y' => $point['y'], 'sum' => $point['sum']]; |
||
60 | |||
61 | // Second quadrant |
||
62 | $clone = clone $original; |
||
63 | $clone->cropimage($halfWidth, $halfHeight, $halfWidth, 0); |
||
64 | $point = static::getHighestEnergyPoint($clone); |
||
65 | $points[] = ['x' => $point['x'] + $halfWidth, 'y' => $point['y'], 'sum' => $point['sum']]; |
||
66 | |||
67 | // Third quadrant |
||
68 | $clone = clone $original; |
||
69 | $clone->cropimage($halfWidth, $halfHeight, 0, $halfHeight); |
||
70 | $point = static::getHighestEnergyPoint($clone); |
||
71 | $points[] = ['x' => $point['x'], 'y' => $point['y'] + $halfHeight, 'sum' => $point['sum']]; |
||
72 | |||
73 | // Fourth quadrant |
||
74 | $clone = clone $original; |
||
75 | $clone->cropimage($halfWidth, $halfHeight, $halfWidth, $halfHeight); |
||
76 | $point = $point = static::getHighestEnergyPoint($clone); |
||
77 | $points[] = ['x' => $point['x'] + $halfWidth, 'y' => $point['y'] + $halfHeight, 'sum' => $point['sum']]; |
||
78 | |||
79 | // get the totalt sum value so we can find out a mean center point |
||
80 | $totalWeight = array_reduce( |
||
81 | $points, |
||
82 | function ($result, $array) { |
||
83 | return $result + $array['sum']; |
||
84 | } |
||
85 | ); |
||
86 | |||
87 | $centerX = 0; |
||
88 | $centerY = 0; |
||
89 | |||
90 | // Calulate the mean weighted center x and y |
||
91 | $totalPoints = count($points); |
||
92 | for ($idx = 0; $idx < $totalPoints; ++$idx) { |
||
93 | $centerX += $points[$idx]['x'] * ($points[$idx]['sum'] / $totalWeight); |
||
94 | $centerY += $points[$idx]['y'] * ($points[$idx]['sum'] / $totalWeight); |
||
95 | } |
||
96 | |||
97 | // From the weighted center point to the topleft corner of the crop would be |
||
98 | $topleftX = max(0, ($centerX - $targetWidth / 2)); |
||
99 | $topleftY = max(0, ($centerY - $targetHeight / 2)); |
||
100 | |||
101 | // If we don't have enough width for the crop, back up $topleftX until |
||
102 | // we can make the image meet $targetWidth |
||
103 | if (($topleftX + $targetWidth) > $size['width']) { |
||
104 | $topleftX -= ($topleftX + $targetWidth) - $size['width']; |
||
105 | } |
||
106 | // If we don't have enough height for the crop, back up $topleftY until |
||
107 | // we can make the image meet $targetHeight |
||
108 | if (($topleftY + $targetHeight) > $size['height']) { |
||
109 | $topleftY -= ($topleftY + $targetHeight) - $size['height']; |
||
110 | } |
||
111 | |||
112 | return [$topleftX, $topleftY]; |
||
113 | } |
||
114 | |||
164 |