|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Resize and crop images. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Mikael Roos [email protected] |
|
6
|
|
|
* @example http://dbwebb.se/opensource/cimage |
|
7
|
|
|
* @link https://github.com/mosbth/cimage |
|
8
|
|
|
*/ |
|
9
|
|
|
class CImageResizer |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Log function. |
|
13
|
|
|
*/ |
|
14
|
|
|
private $log; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Source image dimensions, calculated from loaded image. |
|
20
|
|
|
*/ |
|
21
|
|
|
private $srcWidth; |
|
22
|
|
|
private $srcHeight; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Set as expected target image/canvas dimensions. |
|
28
|
|
|
*/ |
|
29
|
|
|
private $targetWidth; |
|
30
|
|
|
private $targetHeight; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Where should the image go on the canvas. |
|
36
|
|
|
*/ |
|
37
|
|
|
private $destinationX; |
|
38
|
|
|
private $destinationY; |
|
39
|
|
|
private $destinationWidth; |
|
40
|
|
|
private $destinationHeight; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Which parts to crop from the source. |
|
46
|
|
|
*/ |
|
47
|
|
|
private $cropX; |
|
48
|
|
|
private $cropY; |
|
49
|
|
|
private $cropWidth; |
|
50
|
|
|
private $cropHeight; |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Change target height & width when different dpr, dpr 2 means double |
|
56
|
|
|
* image dimensions. |
|
57
|
|
|
*/ |
|
58
|
|
|
private $dpr = null; |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set aspect ratio for the target image. |
|
64
|
|
|
*/ |
|
65
|
|
|
private $aspectRatio; |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Array with details on how to crop. |
|
71
|
|
|
* Array contains xxxxxx |
|
72
|
|
|
*/ |
|
73
|
|
|
public $crop; |
|
74
|
|
|
public $cropOrig; // Save original value? |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Area to use for target image, crop out parts not in area. |
|
80
|
|
|
* Array with top, right, bottom, left percentage values to crop out. |
|
81
|
|
|
*/ |
|
82
|
|
|
private $area; |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Pixel offset in source image to decide which part of image is used. |
|
88
|
|
|
* Array with top, right, bottom, left percentage values to crop out. |
|
89
|
|
|
*/ |
|
90
|
|
|
private $offset; |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Resize strategy, image should keep its original ratio. |
|
96
|
|
|
*/ |
|
97
|
|
|
const KEEP_RATIO = 1; |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Resize strategy, image should crop and fill area. |
|
103
|
|
|
*/ |
|
104
|
|
|
const CROP_TO_FIT = 2; |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Resize strategy, image should fit in area and fill remains. |
|
110
|
|
|
*/ |
|
111
|
|
|
const FILL_TO_FIT = 3; |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Resize strategy, image should stretch to fit in area. |
|
117
|
|
|
*/ |
|
118
|
|
|
const STRETCH = 4; |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* The currently selected resize strategy. |
|
124
|
|
|
*/ |
|
125
|
|
|
private $resizeStrategy = self::KEEP_RATIO; |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Allow upscale of smaller images by default, set to false to disallow. |
|
131
|
|
|
*/ |
|
132
|
|
|
private $upscale = true; |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Constructor, set log function to use for verbose logging or null |
|
138
|
|
|
* to disable logging. |
|
139
|
|
|
* |
|
140
|
|
|
* @param callable $log function to call for logging. |
|
141
|
|
|
*/ |
|
142
|
113 |
|
public function __construct($log = null) |
|
143
|
|
|
{ |
|
144
|
113 |
|
$this->log = $log; |
|
145
|
113 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Log string using logger. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $str to log. |
|
153
|
|
|
*/ |
|
154
|
104 |
|
public function log($str) |
|
155
|
|
|
{ |
|
156
|
104 |
|
if ($this->log) { |
|
157
|
1 |
|
call_user_func($this->log, $str); |
|
158
|
1 |
|
} |
|
159
|
104 |
|
} |
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Set source dimensions. |
|
165
|
|
|
* |
|
166
|
|
|
* @param integer $width of source image. |
|
167
|
|
|
* @param integer $height of source image. |
|
168
|
|
|
* |
|
169
|
|
|
* @throws Exception |
|
170
|
|
|
* |
|
171
|
|
|
* @return $this |
|
172
|
|
|
*/ |
|
173
|
94 |
|
public function setSource($width, $height) |
|
174
|
|
|
{ |
|
175
|
94 |
|
$this->srcWidth = $width; |
|
176
|
94 |
|
$this->srcHeight = $height; |
|
177
|
94 |
|
$this->log("# Source image dimension: {$this->srcWidth}x{$this->srcHeight}."); |
|
178
|
|
|
|
|
179
|
94 |
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Get resize strategy as string. |
|
186
|
|
|
* |
|
187
|
|
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
70 |
|
public function getResizeStrategyAsString() |
|
190
|
|
|
{ |
|
191
|
70 |
|
switch ($this->resizeStrategy) { |
|
192
|
70 |
|
case self::KEEP_RATIO: |
|
193
|
21 |
|
return "KEEP_RATIO"; |
|
194
|
|
|
break; |
|
|
|
|
|
|
195
|
|
|
|
|
196
|
49 |
|
case self::CROP_TO_FIT: |
|
197
|
23 |
|
return "CROP_TO_FIT"; |
|
198
|
|
|
break; |
|
|
|
|
|
|
199
|
|
|
|
|
200
|
26 |
|
case self::FILL_TO_FIT: |
|
201
|
17 |
|
return "FILL_TO_FIT"; |
|
202
|
|
|
break; |
|
|
|
|
|
|
203
|
|
|
|
|
204
|
9 |
|
case self::STRETCH: |
|
205
|
8 |
|
return "STRETCH"; |
|
206
|
|
|
break; |
|
|
|
|
|
|
207
|
|
|
|
|
208
|
1 |
|
default: |
|
209
|
1 |
|
return "UNKNOWN"; |
|
210
|
1 |
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
|
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Set resize strategy as KEEP_RATIO, CROP_TO_FIT or FILL_TO_FIT. |
|
217
|
|
|
* |
|
218
|
|
|
* @param integer $strategy |
|
219
|
|
|
* |
|
220
|
|
|
* @return $this |
|
221
|
|
|
*/ |
|
222
|
70 |
|
public function setResizeStrategy($strategy) |
|
223
|
|
|
{ |
|
224
|
70 |
|
$this->resizeStrategy = $strategy; |
|
225
|
70 |
|
$this->log("# Resize strategy is " . $this->getResizeStrategyAsString()); |
|
226
|
|
|
|
|
227
|
70 |
|
return $this; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Allow or disallow upscale smaller images. |
|
234
|
|
|
* |
|
235
|
|
|
* @param boolean $upscale |
|
236
|
|
|
* |
|
237
|
|
|
* @return $this |
|
238
|
|
|
*/ |
|
239
|
22 |
|
public function allowUpscale($upscale) |
|
240
|
|
|
{ |
|
241
|
22 |
|
$this->upscale = $upscale; |
|
242
|
22 |
|
$this->log("# Allow upscale is $this->upscale."); |
|
243
|
|
|
|
|
244
|
22 |
|
return $this; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Check if a value is upscaled or not by compare $current to $orig, |
|
251
|
|
|
* if $current is larger than $orig then check if upscaled is allowed. |
|
252
|
|
|
* |
|
253
|
|
|
* @param float &$current value to check and change. |
|
254
|
|
|
* @param float $orig value to check against. |
|
255
|
|
|
* |
|
256
|
|
|
* @return float as the value respected by the upscale setting. |
|
257
|
|
|
*/ |
|
258
|
40 |
|
public function respectUpscale(&$current, $orig) |
|
259
|
|
|
{ |
|
260
|
40 |
|
if (!$this->upscale && $current > $orig) { |
|
261
|
6 |
|
$this->log("# Disallowed upscale of $orig to $current"); |
|
262
|
6 |
|
$current = $orig; |
|
263
|
6 |
|
} |
|
264
|
40 |
|
} |
|
265
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Set base for requested width and height. |
|
270
|
|
|
* |
|
271
|
|
|
* @param numeric|null $width as requested target width |
|
272
|
|
|
* @param numeric|null $height as requested target height |
|
273
|
|
|
* |
|
274
|
|
|
* @throws Exception |
|
275
|
|
|
* |
|
276
|
|
|
* @return $this |
|
277
|
|
|
*/ |
|
278
|
98 |
|
public function setBaseWidthHeight($width = null, $height = null) |
|
279
|
|
|
{ |
|
280
|
98 |
|
$this->log("# Set base for width and height."); |
|
281
|
|
|
|
|
282
|
98 |
|
$this->targetWidth = $width; |
|
283
|
98 |
|
$this->targetHeight = $height; |
|
284
|
|
|
|
|
285
|
|
|
// Width specified as % |
|
286
|
98 |
View Code Duplication |
if ($this->targetWidth[strlen($this->targetWidth)-1] == '%') { |
|
|
|
|
|
|
287
|
2 |
|
$this->targetWidth = $this->srcWidth * substr($this->targetWidth, 0, -1) / 100; |
|
288
|
2 |
|
$this->log(" Setting new width based on $width to {$this->targetWidth}."); |
|
289
|
2 |
|
} |
|
290
|
|
|
|
|
291
|
|
|
// Height specified as % |
|
292
|
98 |
View Code Duplication |
if ($this->targetHeight[strlen($this->targetHeight)-1] == '%') { |
|
|
|
|
|
|
293
|
2 |
|
$this->targetHeight = $this->srcHeight * substr($this->targetHeight, 0, -1) / 100; |
|
294
|
2 |
|
$this->log(" Setting new height based on $height to {$this->targetHeight}."); |
|
295
|
2 |
|
} |
|
296
|
|
|
|
|
297
|
98 |
|
if (!(is_null($this->targetWidth) || is_numeric($this->targetWidth))) { |
|
298
|
1 |
|
throw new Exception('Width not numeric'); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
97 |
|
if (!(is_null($this->targetHeight) || is_numeric($this->targetHeight))) { |
|
302
|
1 |
|
throw new Exception('Height not numeric'); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
96 |
|
$this->log(" Requested target dimension as: {$this->targetWidth}x{$this->targetHeight}."); |
|
306
|
|
|
|
|
307
|
96 |
|
return $this; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
|
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Set base for requested aspect ratio. |
|
314
|
|
|
* |
|
315
|
|
|
* @param float|null $aspectRatio as requested aspect ratio |
|
316
|
|
|
* |
|
317
|
|
|
* @throws Exception |
|
318
|
|
|
* |
|
319
|
|
|
* @return $this |
|
320
|
|
|
*/ |
|
321
|
15 |
View Code Duplication |
public function setBaseAspecRatio($aspectRatio = null) |
|
|
|
|
|
|
322
|
|
|
{ |
|
323
|
15 |
|
$this->log("# Set base for aspect ratio."); |
|
324
|
|
|
|
|
325
|
15 |
|
$this->aspectRatio = $aspectRatio; |
|
326
|
|
|
|
|
327
|
15 |
|
if (!(is_null($this->aspectRatio) || is_numeric($this->aspectRatio))) { |
|
328
|
1 |
|
throw new Exception("Aspect ratio out of range"); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
14 |
|
$this->log(" Requested aspectRatio={$this->aspectRatio}."); |
|
332
|
|
|
|
|
333
|
14 |
|
return $this; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
|
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Set base for requested device pixel ratio. |
|
340
|
|
|
* |
|
341
|
|
|
* @param float $dpr as requested density pixel rate |
|
342
|
|
|
* |
|
343
|
|
|
* @throws Exception |
|
344
|
|
|
* |
|
345
|
|
|
* @return $this |
|
346
|
|
|
*/ |
|
347
|
14 |
View Code Duplication |
public function setBaseDevicePixelRate($dpr = null) |
|
|
|
|
|
|
348
|
|
|
{ |
|
349
|
14 |
|
$this->log("# Set base for device pixel rate."); |
|
350
|
|
|
|
|
351
|
14 |
|
$this->dpr = $dpr; |
|
352
|
|
|
|
|
353
|
14 |
|
if (!(is_null($dpr) || (is_numeric($this->dpr) && $this->dpr > 0))) { |
|
354
|
1 |
|
throw new Exception("Device pixel rate out of range"); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
13 |
|
$this->log(" Requested dpr={$this->dpr}."); |
|
358
|
|
|
|
|
359
|
13 |
|
return $this; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
|
|
363
|
|
|
|
|
364
|
|
|
/** |
|
365
|
|
|
* Calculate target width and height by considering the selected |
|
366
|
|
|
* aspect ratio. |
|
367
|
|
|
* |
|
368
|
|
|
* @throws Exception |
|
369
|
|
|
* |
|
370
|
|
|
* @return $this |
|
371
|
|
|
*/ |
|
372
|
26 |
|
public function prepareByConsiderAspectRatio() |
|
373
|
|
|
{ |
|
374
|
26 |
|
$this->log(" Prepare by aspect ratio {$this->aspectRatio}."); |
|
375
|
|
|
|
|
376
|
26 |
|
if (is_null($this->aspectRatio)) { |
|
377
|
14 |
|
return $this; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
// Both null, use source as base for target |
|
381
|
12 |
|
if (is_null($this->targetWidth) && is_null($this->targetHeight)) { |
|
382
|
|
|
|
|
383
|
3 |
|
$this->targetWidth = ($this->aspectRatio >= 1) |
|
384
|
3 |
|
? $this->srcWidth |
|
385
|
2 |
|
: null; |
|
386
|
|
|
|
|
387
|
3 |
|
$this->targetHeight = ($this->aspectRatio >= 1) |
|
388
|
3 |
|
? null |
|
389
|
3 |
|
: $this->srcHeight; |
|
390
|
|
|
|
|
391
|
3 |
|
$this->log(" Using source as base {$this->targetWidth}x{$this->targetHeight}"); |
|
392
|
|
|
|
|
393
|
3 |
|
} |
|
394
|
|
|
|
|
395
|
|
|
// Both or either set, calculate the other |
|
396
|
12 |
|
if (isset($this->targetWidth) && isset($this->targetHeight)) { |
|
397
|
|
|
|
|
398
|
3 |
|
$this->targetWidth = ($this->aspectRatio >= 1) |
|
399
|
3 |
|
? $this->targetWidth |
|
400
|
3 |
|
: $this->targetHeight * $this->aspectRatio; |
|
401
|
|
|
|
|
402
|
3 |
|
$this->targetHeight = ($this->aspectRatio >= 1) |
|
403
|
3 |
|
? $this->targetWidth / $this->aspectRatio |
|
404
|
3 |
|
: $this->targetHeight; |
|
405
|
|
|
|
|
406
|
3 |
|
$this->log(" New target width height {$this->targetWidth}x{$this->targetHeight}"); |
|
407
|
|
|
|
|
408
|
12 |
|
} elseif (isset($this->targetWidth)) { |
|
409
|
|
|
|
|
410
|
5 |
|
$this->targetHeight = $this->targetWidth / $this->aspectRatio; |
|
411
|
5 |
|
$this->log(" New target height x{$this->targetHeight}"); |
|
412
|
|
|
|
|
413
|
9 |
|
} elseif (isset($this->targetHeight)) { |
|
414
|
|
|
|
|
415
|
4 |
|
$this->targetWidth = $this->targetHeight * $this->aspectRatio; |
|
416
|
4 |
|
$this->log(" New target width {$this->targetWidth}x"); |
|
417
|
|
|
|
|
418
|
4 |
|
} |
|
419
|
|
|
|
|
420
|
12 |
|
return $this; |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
|
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* Calculate target width and height by considering the selected |
|
427
|
|
|
* dpr. |
|
428
|
|
|
* |
|
429
|
|
|
* @throws Exception |
|
430
|
|
|
* |
|
431
|
|
|
* @return $this |
|
432
|
|
|
*/ |
|
433
|
26 |
|
public function prepareByConsiderDpr() |
|
434
|
|
|
{ |
|
435
|
26 |
|
$this->log(" Prepare by dpr={$this->dpr}."); |
|
436
|
|
|
|
|
437
|
26 |
|
if (is_null($this->dpr)) { |
|
438
|
14 |
|
return $this; |
|
439
|
|
|
} |
|
440
|
|
|
|
|
441
|
|
|
// If both not set, use source as base |
|
442
|
12 |
|
if (is_null($this->targetWidth) && is_null($this->targetHeight)) { |
|
443
|
3 |
|
$this->targetWidth = $this->srcWidth; |
|
444
|
3 |
|
$this->targetHeight = $this->srcHeight; |
|
445
|
3 |
|
} |
|
446
|
|
|
|
|
447
|
12 |
|
if (isset($this->targetWidth)) { |
|
448
|
9 |
|
$this->targetWidth = $this->targetWidth * $this->dpr; |
|
449
|
9 |
|
$this->log(" Update target width to {$this->targetWidth}."); |
|
450
|
9 |
|
} |
|
451
|
|
|
|
|
452
|
12 |
|
if (isset($this->targetHeight)) { |
|
453
|
9 |
|
$this->targetHeight = $this->targetHeight * $this->dpr; |
|
454
|
9 |
|
$this->log(" Update target height to {$this->targetHeight}."); |
|
455
|
9 |
|
} |
|
456
|
|
|
|
|
457
|
12 |
|
return $this; |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
|
|
461
|
|
|
|
|
462
|
|
|
/** |
|
463
|
|
|
* Calculate target width and height and do sanity checks on constraints. |
|
464
|
|
|
* After this method the $targetWidth and $targetHeight will have |
|
465
|
|
|
* the expected dimensions on the target image. |
|
466
|
|
|
* |
|
467
|
|
|
* @throws Exception |
|
468
|
|
|
* |
|
469
|
|
|
* @return $this |
|
470
|
|
|
*/ |
|
471
|
26 |
|
public function prepareTargetDimensions() |
|
472
|
|
|
{ |
|
473
|
26 |
|
$this->log("# Prepare target dimension (before): {$this->targetWidth}x{$this->targetHeight}."); |
|
474
|
|
|
|
|
475
|
26 |
|
$this->prepareByConsiderAspectRatio() |
|
476
|
26 |
|
->prepareByConsiderDpr(); |
|
477
|
|
|
|
|
478
|
26 |
|
$this->log(" Prepare target dimension (after): {$this->targetWidth}x{$this->targetHeight}."); |
|
479
|
|
|
|
|
480
|
26 |
|
return $this; |
|
481
|
|
|
} |
|
482
|
|
|
|
|
483
|
|
|
|
|
484
|
|
|
|
|
485
|
|
|
/** |
|
486
|
|
|
* Calculate new width and height of image. |
|
487
|
|
|
* |
|
488
|
|
|
* @return $this |
|
489
|
|
|
*/ |
|
490
|
65 |
|
public function calculateTargetWidthAndHeight() |
|
491
|
|
|
{ |
|
492
|
65 |
|
$this->log("# Calculate new width and height."); |
|
493
|
65 |
|
$this->log(" Source size {$this->srcWidth}x{$this->srcHeight}."); |
|
494
|
65 |
|
$this->log(" Target dimension (before) {$this->targetWidth}x{$this->targetHeight}."); |
|
495
|
|
|
|
|
496
|
|
|
// Set default values to crop area to be whole source image |
|
497
|
65 |
|
$sw = $this->srcWidth; |
|
498
|
65 |
|
$sh = $this->srcHeight; |
|
499
|
65 |
|
$ar = $sw / $sh; |
|
500
|
65 |
|
$tw = $this->targetWidth; |
|
501
|
65 |
|
$th = $this->targetHeight; |
|
502
|
65 |
|
$dx = 0; |
|
503
|
65 |
|
$dy = 0; |
|
504
|
65 |
|
$dw = null; |
|
505
|
65 |
|
$dh = null; |
|
506
|
65 |
|
$cx = 0; |
|
507
|
65 |
|
$cy = 0; |
|
508
|
65 |
|
$cw = $sw; |
|
509
|
65 |
|
$ch = $sh; |
|
510
|
65 |
|
$rs = $this->resizeStrategy; |
|
511
|
65 |
|
$both = isset($tw) && isset($th); |
|
512
|
65 |
|
$ratio = $both ? $tw / $th : null; |
|
513
|
|
|
|
|
514
|
65 |
|
if (is_null($tw) && is_null($th)) { |
|
515
|
|
|
|
|
516
|
|
|
// No tw/th use sw/sh |
|
517
|
3 |
|
$tw = $sw; |
|
518
|
3 |
|
$th = $sh; |
|
519
|
3 |
|
$this->log(" New tw x th {$tw}x{$th}"); |
|
520
|
|
|
|
|
521
|
65 |
|
} elseif (isset($tw) && is_null($th)) { |
|
522
|
|
|
|
|
523
|
|
|
// Keep aspect ratio, make th based on tw |
|
524
|
4 |
|
$this->respectUpscale($tw, $sw); |
|
525
|
4 |
|
$th = $tw / $ar; |
|
526
|
4 |
|
$this->log(" New th x{$th}"); |
|
527
|
|
|
|
|
528
|
62 |
|
} elseif (is_null($tw) && isset($th)) { |
|
529
|
|
|
|
|
530
|
|
|
// Keep aspect ratio, make tw based on th |
|
531
|
4 |
|
$this->respectUpscale($th, $sh); |
|
532
|
4 |
|
$tw = $th * $ar; |
|
533
|
4 |
|
$this->log(" New tw {$tw}x"); |
|
534
|
|
|
|
|
535
|
58 |
|
} elseif ($rs === CImageResizer::KEEP_RATIO && $both) { |
|
536
|
|
|
|
|
537
|
|
|
// Keep aspect ratio, make fit in box not larger than tw/th |
|
538
|
9 |
|
$this->log(" Keep ratio, ratio target=$ratio, source=$ar"); |
|
539
|
|
|
|
|
540
|
9 |
|
if ($ratio > $ar) { |
|
541
|
2 |
|
$this->respectUpscale($th, $sh); |
|
542
|
2 |
|
$tw = $th * $ar; |
|
543
|
2 |
|
$this->log(" New tw {$tw}x"); |
|
544
|
9 |
|
} elseif ($ratio < $ar) { |
|
545
|
2 |
|
$this->respectUpscale($tw, $sw); |
|
546
|
2 |
|
$th = $tw / $ar; |
|
547
|
2 |
|
$this->log(" New th x{$th}"); |
|
548
|
2 |
|
} else { |
|
549
|
5 |
|
$this->respectUpscale($tw, $sw); |
|
550
|
5 |
|
$this->respectUpscale($th, $sh); |
|
551
|
|
|
} |
|
552
|
|
|
|
|
553
|
54 |
|
} elseif ($rs === CImageResizer::STRETCH && $both) { |
|
554
|
|
|
|
|
555
|
|
|
// Stretch to fit, leave as is |
|
556
|
7 |
|
$this->log(" Stretch, leave as is"); |
|
557
|
7 |
|
$this->respectUpscale($tw, $sw); |
|
558
|
7 |
|
$this->respectUpscale($th, $sh); |
|
559
|
|
|
|
|
560
|
45 |
|
} elseif ($rs === CImageResizer::CROP_TO_FIT && $both) { |
|
561
|
|
|
|
|
562
|
|
|
// Crop to fit image in box |
|
563
|
|
|
// Ignores respectUpscale by intention |
|
564
|
22 |
|
$this->log(" Crop to fit, ratio target=$ratio, source=$ar"); |
|
565
|
|
|
|
|
566
|
22 |
|
if ($ratio > $ar) { |
|
567
|
8 |
|
$ch = $sw / $ratio; |
|
568
|
8 |
|
$cy = ($sh - $ch) / 2; |
|
569
|
22 |
|
} elseif ($ratio < $ar) { |
|
570
|
8 |
|
$cw = $sh * $ratio; |
|
571
|
8 |
|
$cx = ($sw - $cw) / 2; |
|
572
|
8 |
|
} |
|
573
|
|
|
|
|
574
|
22 |
|
$this->log(" Parts cx=$cx, cy=$cy, cw=$cw, ch=$ch"); |
|
575
|
|
|
|
|
576
|
38 |
|
} elseif ($rs === CImageResizer::FILL_TO_FIT && $both) { |
|
577
|
|
|
|
|
578
|
|
|
// Fill to fit image in box |
|
579
|
16 |
|
$this->log(" Fill to fit, ratio target=$ratio, source=$ar"); |
|
580
|
16 |
|
$dw = $tw; |
|
581
|
16 |
|
$dh = $th; |
|
582
|
|
|
|
|
583
|
16 |
|
if ($ratio > $ar) { |
|
584
|
5 |
|
$dw = $th * $ar; |
|
585
|
5 |
|
$dh = $th; |
|
586
|
16 |
|
} elseif ($ratio < $ar) { |
|
587
|
6 |
|
$dw = $tw; |
|
588
|
6 |
|
$dh = $tw / $ar; |
|
589
|
6 |
|
} |
|
590
|
|
|
|
|
591
|
16 |
|
$this->respectUpscale($dw, $sw); |
|
592
|
16 |
|
$this->respectUpscale($dh, $sh); |
|
593
|
16 |
|
$dx = ($tw - $dw) / 2; |
|
594
|
16 |
|
$dy = ($th - $dh) / 2; |
|
595
|
|
|
|
|
596
|
16 |
|
$this->log(" Destination area dx=$dx, dy=$dy, dw=$dw, dh=$dh"); |
|
597
|
|
|
|
|
598
|
16 |
|
} |
|
599
|
|
|
|
|
600
|
|
|
// All done, sum it up |
|
601
|
65 |
|
$dw = is_null($dw) ? $tw : $dw; |
|
602
|
65 |
|
$dh = is_null($dh) ? $th : $dh; |
|
603
|
|
|
|
|
604
|
65 |
|
$this->targetWidth = round($tw); |
|
605
|
65 |
|
$this->targetHeight = round($th); |
|
606
|
65 |
|
$this->destinationX = round($dx); |
|
607
|
65 |
|
$this->destinationY = round($dy); |
|
608
|
65 |
|
$this->destinationWidth = round($dw); |
|
609
|
65 |
|
$this->destinationHeight = round($dh); |
|
610
|
65 |
|
$this->cropX = round($cx); |
|
611
|
65 |
|
$this->cropY = round($cy); |
|
612
|
65 |
|
$this->cropWidth = round($cw); |
|
613
|
65 |
|
$this->cropHeight = round($ch); |
|
614
|
|
|
|
|
615
|
65 |
|
$this->log(" Target dimension (after) {$this->targetWidth}x{$this->targetHeight}."); |
|
616
|
65 |
|
$this->log(" Crop area {$this->cropX}x{$this->cropY} by {$this->cropWidth}x{$this->cropHeight}."); |
|
617
|
65 |
|
$this->log(" Destination area {$this->destinationX}x{$this->destinationY} by {$this->destinationWidth}x{$this->destinationHeight}."); |
|
618
|
|
|
|
|
619
|
|
|
|
|
620
|
|
|
/* |
|
621
|
|
|
|
|
622
|
|
|
// Check if there is an area to crop off |
|
623
|
|
|
if (isset($this->area)) { |
|
624
|
|
|
$this->offset['top'] = round($this->area['top'] / 100 * $this->srcHeight); |
|
625
|
|
|
$this->offset['right'] = round($this->area['right'] / 100 * $this->srcWidth); |
|
626
|
|
|
$this->offset['bottom'] = round($this->area['bottom'] / 100 * $this->srcHeight); |
|
627
|
|
|
$this->offset['left'] = round($this->area['left'] / 100 * $this->srcWidth); |
|
628
|
|
|
$this->offset['width'] = $this->srcWidth - $this->offset['left'] - $this->offset['right']; |
|
629
|
|
|
$this->offset['height'] = $this->srcHeight - $this->offset['top'] - $this->offset['bottom']; |
|
630
|
|
|
$this->srcWidth = $this->offset['width']; |
|
631
|
|
|
$this->srcHeight = $this->offset['height']; |
|
632
|
|
|
$this->log("The offset for the area to use is top {$this->area['top']}%, right {$this->area['right']}%, bottom {$this->area['bottom']}%, left {$this->area['left']}%."); |
|
633
|
|
|
$this->log("The offset for the area to use is top {$this->offset['top']}px, right {$this->offset['right']}px, bottom {$this->offset['bottom']}px, left {$this->offset['left']}px, width {$this->offset['width']}px, height {$this->offset['height']}px."); |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
|
|
637
|
|
|
// Check if crop is set |
|
638
|
|
|
if ($this->crop) { |
|
639
|
|
|
$width = $this->crop['width'] = $this->crop['width'] <= 0 ? $this->srcWidth + $this->crop['width'] : $this->crop['width']; |
|
640
|
|
|
$height = $this->crop['height'] = $this->crop['height'] <= 0 ? $this->srcHeight + $this->crop['height'] : $this->crop['height']; |
|
641
|
|
|
|
|
642
|
|
|
if ($this->crop['start_x'] == 'left') { |
|
643
|
|
|
$this->crop['start_x'] = 0; |
|
644
|
|
|
} elseif ($this->crop['start_x'] == 'right') { |
|
645
|
|
|
$this->crop['start_x'] = $this->srcWidth - $width; |
|
646
|
|
|
} elseif ($this->crop['start_x'] == 'center') { |
|
647
|
|
|
$this->crop['start_x'] = round($this->srcWidth / 2) - round($width / 2); |
|
648
|
|
|
} |
|
649
|
|
|
|
|
650
|
|
|
if ($this->crop['start_y'] == 'top') { |
|
651
|
|
|
$this->crop['start_y'] = 0; |
|
652
|
|
|
} elseif ($this->crop['start_y'] == 'bottom') { |
|
653
|
|
|
$this->crop['start_y'] = $this->srcHeight - $height; |
|
654
|
|
|
} elseif ($this->crop['start_y'] == 'center') { |
|
655
|
|
|
$this->crop['start_y'] = round($this->srcHeight / 2) - round($height / 2); |
|
656
|
|
|
} |
|
657
|
|
|
|
|
658
|
|
|
$this->log(" Crop area is width {$width}px, height {$height}px, start_x {$this->crop['start_x']}px, start_y {$this->crop['start_y']}px."); |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
|
|
662
|
|
|
|
|
663
|
|
|
// Crop, ensure to set new width and height |
|
664
|
|
|
if ($this->crop) { |
|
665
|
|
|
$this->log(" Crop."); |
|
666
|
|
|
$this->targetWidth = round(isset($this->targetWidth) |
|
667
|
|
|
? $this->targetWidth |
|
668
|
|
|
: $this->crop['width']); |
|
669
|
|
|
$this->targetHeight = round(isset($this->targetHeight) |
|
670
|
|
|
? $this->targetHeight |
|
671
|
|
|
: $this->crop['height']); |
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
*/ |
|
675
|
|
|
|
|
676
|
65 |
|
return $this; |
|
677
|
|
|
} |
|
678
|
|
|
|
|
679
|
|
|
|
|
680
|
|
|
|
|
681
|
|
|
/** |
|
682
|
|
|
* Get source width. |
|
683
|
|
|
* |
|
684
|
|
|
* @return integer as source width |
|
685
|
|
|
*/ |
|
686
|
1 |
|
public function getSourceWidth() |
|
687
|
|
|
{ |
|
688
|
1 |
|
return $this->srcWidth; |
|
689
|
|
|
} |
|
690
|
|
|
|
|
691
|
|
|
|
|
692
|
|
|
|
|
693
|
|
|
/** |
|
694
|
|
|
* Get source height. |
|
695
|
|
|
* |
|
696
|
|
|
* @return integer as source height |
|
697
|
|
|
*/ |
|
698
|
1 |
|
public function getSourceHeight() |
|
699
|
|
|
{ |
|
700
|
1 |
|
return $this->srcHeight; |
|
701
|
|
|
} |
|
702
|
|
|
|
|
703
|
|
|
|
|
704
|
|
|
|
|
705
|
|
|
/** |
|
706
|
|
|
* Get target width. |
|
707
|
|
|
* |
|
708
|
|
|
* @return integer as target width |
|
709
|
|
|
*/ |
|
710
|
93 |
|
public function getTargetWidth() |
|
711
|
|
|
{ |
|
712
|
93 |
|
return $this->targetWidth; |
|
713
|
|
|
} |
|
714
|
|
|
|
|
715
|
|
|
|
|
716
|
|
|
|
|
717
|
|
|
/** |
|
718
|
|
|
* Get target height. |
|
719
|
|
|
* |
|
720
|
|
|
* @return integer as target height |
|
721
|
|
|
*/ |
|
722
|
93 |
|
public function getTargetHeight() |
|
723
|
|
|
{ |
|
724
|
93 |
|
return $this->targetHeight; |
|
725
|
|
|
} |
|
726
|
|
|
|
|
727
|
|
|
|
|
728
|
|
|
|
|
729
|
|
|
/** |
|
730
|
|
|
* Get destination x. |
|
731
|
|
|
* |
|
732
|
|
|
* @return integer as destination x |
|
733
|
|
|
*/ |
|
734
|
16 |
|
public function getDestinationX() |
|
735
|
|
|
{ |
|
736
|
16 |
|
return $this->destinationX; |
|
737
|
|
|
} |
|
738
|
|
|
|
|
739
|
|
|
|
|
740
|
|
|
|
|
741
|
|
|
/** |
|
742
|
|
|
* Get destination y. |
|
743
|
|
|
* |
|
744
|
|
|
* @return integer as destination y |
|
745
|
|
|
*/ |
|
746
|
16 |
|
public function getDestinationY() |
|
747
|
|
|
{ |
|
748
|
16 |
|
return $this->destinationY; |
|
749
|
|
|
} |
|
750
|
|
|
|
|
751
|
|
|
|
|
752
|
|
|
|
|
753
|
|
|
/** |
|
754
|
|
|
* Get destination width. |
|
755
|
|
|
* |
|
756
|
|
|
* @return integer as destination width |
|
757
|
|
|
*/ |
|
758
|
16 |
|
public function getDestinationWidth() |
|
759
|
|
|
{ |
|
760
|
16 |
|
return $this->destinationWidth; |
|
761
|
|
|
} |
|
762
|
|
|
|
|
763
|
|
|
|
|
764
|
|
|
|
|
765
|
|
|
/** |
|
766
|
|
|
* Get destination height. |
|
767
|
|
|
* |
|
768
|
|
|
* @return integer as destination height |
|
769
|
|
|
*/ |
|
770
|
16 |
|
public function getDestinationHeight() |
|
771
|
|
|
{ |
|
772
|
16 |
|
return $this->destinationHeight; |
|
773
|
|
|
} |
|
774
|
|
|
|
|
775
|
|
|
|
|
776
|
|
|
|
|
777
|
|
|
/** |
|
778
|
|
|
* Get crop position x. |
|
779
|
|
|
* |
|
780
|
|
|
* @return integer |
|
781
|
|
|
*/ |
|
782
|
42 |
|
public function getCropX() |
|
783
|
|
|
{ |
|
784
|
42 |
|
return $this->cropX; |
|
785
|
|
|
} |
|
786
|
|
|
|
|
787
|
|
|
|
|
788
|
|
|
|
|
789
|
|
|
/** |
|
790
|
|
|
* Get crop position y. |
|
791
|
|
|
* |
|
792
|
|
|
* @return integer |
|
793
|
|
|
*/ |
|
794
|
42 |
|
public function getCropY() |
|
795
|
|
|
{ |
|
796
|
42 |
|
return $this->cropY; |
|
797
|
|
|
} |
|
798
|
|
|
|
|
799
|
|
|
|
|
800
|
|
|
|
|
801
|
|
|
/** |
|
802
|
|
|
* Get crop width. |
|
803
|
|
|
* |
|
804
|
|
|
* @return integer |
|
805
|
|
|
*/ |
|
806
|
42 |
|
public function getCropWidth() |
|
807
|
|
|
{ |
|
808
|
42 |
|
return $this->cropWidth; |
|
809
|
|
|
} |
|
810
|
|
|
|
|
811
|
|
|
|
|
812
|
|
|
|
|
813
|
|
|
/** |
|
814
|
|
|
* Get crop height. |
|
815
|
|
|
* |
|
816
|
|
|
* @return integer |
|
817
|
|
|
*/ |
|
818
|
42 |
|
public function getCropHeight() |
|
819
|
|
|
{ |
|
820
|
42 |
|
return $this->cropHeight; |
|
821
|
|
|
} |
|
822
|
|
|
} |
|
823
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.