@@ -33,10 +33,10 @@ class FocusCropDataCalculator
33
33
*/
34
34
public function __construct ($ cropCoordinates , $ focusCoordinates , $ styleWidth , $ styleHeight )
35
35
{
36
- $ this ->cropCoordinates = $ cropCoordinates ;
36
+ $ this ->cropCoordinates = $ cropCoordinates ;
37
37
$ this ->focusCoordinates = $ focusCoordinates ;
38
- $ this ->styleWidth = $ styleWidth ;
39
- $ this ->styleHeight = $ styleHeight ;
38
+ $ this ->styleWidth = $ styleWidth ;
39
+ $ this ->styleHeight = $ styleHeight ;
40
40
}
41
41
42
42
/**
@@ -54,7 +54,7 @@ public function getFocusCropData()
54
54
// The sides of the image will be cropped.
55
55
// 2: The style rectangle fits inside the crop rectangle horizontally.
56
56
// The top and bottom of the image will be cropped.
57
- // 3: The style rectangle fits inside the crop rectangle exactly .
57
+ // 3: The style rectangle fits inside the crop rectangle perfectly .
58
58
// no cropping in required
59
59
//
60
60
// To determine which type of cropping should be used, the aspect-ratio of the image/crop rectangle ($imageAspectRatio)
@@ -66,29 +66,28 @@ public function getFocusCropData()
66
66
list ($ x1 , $ y1 , $ x2 , $ y2 ) = $ this ->cropCoordinates ;
67
67
$ this ->geometry = new CoordinateGeometry ($ x1 , $ y1 , $ x2 , $ y2 );
68
68
69
- $ newWidth = $ this ->geometry ->axisLength ('x ' );
69
+ $ newWidth = $ this ->geometry ->axisLength ('x ' );
70
70
$ newHeight = $ this ->geometry ->axisLength ('y ' );
71
71
72
72
// Find out what type of style crop we are dealing with.
73
- $ imageAspectRatio = $ this ->geometry ->getAspectRatio ();
74
-
73
+ // @TODO: Checkout the geometry calculation.
74
+ // $imageAspectRatio = $this->geometry->getAspectRatio();
75
+ $ imageAspectRatio = $ newWidth / $ newHeight ;
75
76
$ styleAspectRatio = $ this ->styleWidth / $ this ->styleHeight ;
76
77
77
78
if ($ imageAspectRatio > $ styleAspectRatio ) {
78
79
$ axis = 'x ' ;
79
80
}
81
+ else if ($ imageAspectRatio < $ styleAspectRatio ) {
82
+ $ axis = 'y ' ;
83
+ }
80
84
else {
81
- if ($ imageAspectRatio < $ styleAspectRatio ) {
82
- $ axis = 'y ' ;
83
- }
84
- else {
85
- return [
86
- 'width ' => $ newWidth ,
87
- 'height ' => $ newHeight ,
88
- 'x ' => $ this ->cropCoordinates [0 ],
89
- 'y ' => $ this ->cropCoordinates [1 ],
90
- ];
91
- }
85
+ return [
86
+ 'width ' => $ newWidth ,
87
+ 'height ' => $ newHeight ,
88
+ 'x ' => $ this ->cropCoordinates [0 ],
89
+ 'y ' => $ this ->cropCoordinates [1 ],
90
+ ];
92
91
}
93
92
94
93
return $ this ->calculateAxisCropData ($ axis , $ this ->cropCoordinates , $ styleAspectRatio , $ newWidth , $ newHeight );
@@ -105,8 +104,25 @@ public function getFocusCropData()
105
104
*/
106
105
protected function calculateAxisCropData ($ axis = 'x ' , $ cropCoordinates , $ aspectRatio , $ newWidth , $ newHeight )
107
106
{
108
- $ cropWidth = ($ axis == 'y ' ) ? $ newWidth : $ newHeight * $ aspectRatio ;
109
- $ cropHeight = ($ axis == 'y ' ) ? $ newWidth / $ aspectRatio : $ newHeight ;
107
+ if ($ axis !== 'x ' && $ axis !== 'y ' ) {
108
+ throw new \InvalidArgumentException ('$axis can only have a value of x or y. ' . $ axis . ' given ' );
109
+ }
110
+
111
+ if ($ axis == 'x ' ) {
112
+ $ cropHeight = $ newHeight ;
113
+
114
+ // How many times the style height goes into the new height
115
+ $ scaleFactor = $ newHeight / $ this ->styleHeight ;
116
+ $ cropWidth = $ this ->styleWidth * $ scaleFactor ;
117
+ }
118
+ else {
119
+ $ cropWidth = $ newWidth ;
120
+
121
+ // How many times the style height goes into the new height
122
+ $ scaleFactor = $ newWidth / $ this ->styleWidth ;
123
+ $ cropHeight = $ this ->styleHeight * $ scaleFactor ;
124
+ }
125
+ $ data ['scale_factor ' ] = $ scaleFactor ;
110
126
111
127
$ cropXOffset = ($ axis == 'y ' ) ? $ cropCoordinates [0 ] : $ this ->getFloatingOffset (
112
128
'x ' ,
@@ -194,7 +210,7 @@ protected function findFocusOffset($axis = 'x', $cropLength)
194
210
// Offsetting on either the x or the y axis.
195
211
// Subtract the crop rectangle.
196
212
$ focusNear = $ this ->getFocusPointForAxis ($ axis , 'near ' );
197
- $ focusFar = $ this ->getFocusPointForAxis ($ axis , 'far ' );
213
+ $ focusFar = $ this ->getFocusPointForAxis ($ axis , 'far ' );
198
214
199
215
$ focusLength = $ focusFar - $ focusNear ;
200
216
$ focusCenter = round (($ focusNear + $ focusFar ) / 2 );
@@ -232,7 +248,7 @@ protected function getOptimalOffset(array $validOffsets)
232
248
if (!empty ($ validOffsets )) {
233
249
asort ($ validOffsets );
234
250
$ offsets = array_keys ($ validOffsets );
235
- $ offset = reset ($ offsets );
251
+ $ offset = reset ($ offsets );
236
252
}
237
253
238
254
return $ offset ;
@@ -248,8 +264,8 @@ protected function getOptimalOffset(array $validOffsets)
248
264
*/
249
265
protected function getValidOffsets ($ focusNear , $ focusFar , $ cropLength , $ imageLength )
250
266
{
251
- $ nearGap = $ focusNear ;
252
- $ farGap = $ imageLength - $ focusFar ;
267
+ $ nearGap = $ focusNear ;
268
+ $ farGap = $ imageLength - $ focusFar ;
253
269
$ offFactor = $ nearGap / $ farGap ;
254
270
255
271
// Will need the maximum and minimum offset also.
@@ -262,12 +278,12 @@ protected function getValidOffsets($focusNear, $focusFar, $cropLength, $imageLen
262
278
// Need a factor of near / far to compare to offFactor.
263
279
// Closest to that wins.
264
280
$ near = $ focusNear - $ i ;
265
- $ far = ($ i + $ cropLength ) - $ focusFar ;
281
+ $ far = ($ i + $ cropLength ) - $ focusFar ;
266
282
if ($ near != 0 && $ far != 0 ) {
267
283
$ optimalFactor = ($ near / $ far ) / $ offFactor ;
268
284
$ optimalFactor = abs ($ optimalFactor );
269
285
270
- $ theTest = abs ($ optimalFactor - 1 );
286
+ $ theTest = abs ($ optimalFactor - 1 );
271
287
$ validOffsets [$ i ] = $ theTest ;
272
288
}
273
289
}
0 commit comments