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
|
33 |
|
public function respectUpscale(&$current, $orig) |
259
|
|
|
{ |
260
|
33 |
|
if (!$this->upscale && $current > $orig) { |
261
|
6 |
|
$this->log("# Disallowed upscale of $orig to $current"); |
262
|
6 |
|
$current = $orig; |
263
|
6 |
|
} |
264
|
33 |
|
} |
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
|
|
|
// Ignores respectUpscale by intention |
557
|
7 |
|
$this->log(" Stretch, leave as is"); |
558
|
|
|
|
559
|
45 |
|
} elseif ($rs === CImageResizer::CROP_TO_FIT && $both) { |
560
|
|
|
|
561
|
|
|
// Crop to fit image in box |
562
|
|
|
// Ignores respectUpscale by intention |
563
|
22 |
|
$this->log(" Crop to fit, ratio target=$ratio, source=$ar"); |
564
|
|
|
|
565
|
22 |
|
if ($ratio > $ar) { |
566
|
8 |
|
$ch = $sw / $ratio; |
567
|
8 |
|
$cy = ($sh - $ch) / 2; |
568
|
22 |
|
} elseif ($ratio < $ar) { |
569
|
8 |
|
$cw = $sh * $ratio; |
570
|
8 |
|
$cx = ($sw - $cw) / 2; |
571
|
8 |
|
} |
572
|
|
|
|
573
|
22 |
|
$this->log(" Parts cx=$cx, cy=$cy, cw=$cw, ch=$ch"); |
574
|
|
|
|
575
|
38 |
|
} elseif ($rs === CImageResizer::FILL_TO_FIT && $both) { |
576
|
|
|
|
577
|
|
|
// Fill to fit image in box |
578
|
16 |
|
$this->log(" Fill to fit, ratio target=$ratio, source=$ar"); |
579
|
16 |
|
$dw = $tw; |
580
|
16 |
|
$dh = $th; |
581
|
|
|
|
582
|
16 |
|
if ($ratio > $ar) { |
583
|
5 |
|
$dw = $th * $ar; |
584
|
5 |
|
$dh = $th; |
585
|
16 |
|
} elseif ($ratio < $ar) { |
586
|
6 |
|
$dw = $tw; |
587
|
6 |
|
$dh = $tw / $ar; |
588
|
6 |
|
} |
589
|
|
|
|
590
|
16 |
|
$this->respectUpscale($dw, $sw); |
591
|
16 |
|
$this->respectUpscale($dh, $sh); |
592
|
16 |
|
$dx = ($tw - $dw) / 2; |
593
|
16 |
|
$dy = ($th - $dh) / 2; |
594
|
|
|
|
595
|
16 |
|
$this->log(" Destination area dx=$dx, dy=$dy, dw=$dw, dh=$dh"); |
596
|
|
|
|
597
|
16 |
|
} |
598
|
|
|
|
599
|
|
|
// All done, sum it up |
600
|
65 |
|
$dw = is_null($dw) ? $tw : $dw; |
601
|
65 |
|
$dh = is_null($dh) ? $th : $dh; |
602
|
|
|
|
603
|
65 |
|
$this->targetWidth = round($tw); |
604
|
65 |
|
$this->targetHeight = round($th); |
605
|
65 |
|
$this->destinationX = round($dx); |
606
|
65 |
|
$this->destinationY = round($dy); |
607
|
65 |
|
$this->destinationWidth = round($dw); |
608
|
65 |
|
$this->destinationHeight = round($dh); |
609
|
65 |
|
$this->cropX = round($cx); |
610
|
65 |
|
$this->cropY = round($cy); |
611
|
65 |
|
$this->cropWidth = round($cw); |
612
|
65 |
|
$this->cropHeight = round($ch); |
613
|
|
|
|
614
|
65 |
|
$this->log(" Target dimension (after) {$this->targetWidth}x{$this->targetHeight}."); |
615
|
65 |
|
$this->log(" Crop area {$this->cropX}x{$this->cropY} by {$this->cropWidth}x{$this->cropHeight}."); |
616
|
65 |
|
$this->log(" Destination area {$this->destinationX}x{$this->destinationY} by {$this->destinationWidth}x{$this->destinationHeight}."); |
617
|
|
|
|
618
|
|
|
|
619
|
|
|
/* |
620
|
|
|
|
621
|
|
|
// Check if there is an area to crop off |
622
|
|
|
if (isset($this->area)) { |
623
|
|
|
$this->offset['top'] = round($this->area['top'] / 100 * $this->srcHeight); |
624
|
|
|
$this->offset['right'] = round($this->area['right'] / 100 * $this->srcWidth); |
625
|
|
|
$this->offset['bottom'] = round($this->area['bottom'] / 100 * $this->srcHeight); |
626
|
|
|
$this->offset['left'] = round($this->area['left'] / 100 * $this->srcWidth); |
627
|
|
|
$this->offset['width'] = $this->srcWidth - $this->offset['left'] - $this->offset['right']; |
628
|
|
|
$this->offset['height'] = $this->srcHeight - $this->offset['top'] - $this->offset['bottom']; |
629
|
|
|
$this->srcWidth = $this->offset['width']; |
630
|
|
|
$this->srcHeight = $this->offset['height']; |
631
|
|
|
$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']}%."); |
632
|
|
|
$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."); |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
|
636
|
|
|
// Check if crop is set |
637
|
|
|
if ($this->crop) { |
638
|
|
|
$width = $this->crop['width'] = $this->crop['width'] <= 0 ? $this->srcWidth + $this->crop['width'] : $this->crop['width']; |
639
|
|
|
$height = $this->crop['height'] = $this->crop['height'] <= 0 ? $this->srcHeight + $this->crop['height'] : $this->crop['height']; |
640
|
|
|
|
641
|
|
|
if ($this->crop['start_x'] == 'left') { |
642
|
|
|
$this->crop['start_x'] = 0; |
643
|
|
|
} elseif ($this->crop['start_x'] == 'right') { |
644
|
|
|
$this->crop['start_x'] = $this->srcWidth - $width; |
645
|
|
|
} elseif ($this->crop['start_x'] == 'center') { |
646
|
|
|
$this->crop['start_x'] = round($this->srcWidth / 2) - round($width / 2); |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
if ($this->crop['start_y'] == 'top') { |
650
|
|
|
$this->crop['start_y'] = 0; |
651
|
|
|
} elseif ($this->crop['start_y'] == 'bottom') { |
652
|
|
|
$this->crop['start_y'] = $this->srcHeight - $height; |
653
|
|
|
} elseif ($this->crop['start_y'] == 'center') { |
654
|
|
|
$this->crop['start_y'] = round($this->srcHeight / 2) - round($height / 2); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
$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."); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
|
661
|
|
|
|
662
|
|
|
// Crop, ensure to set new width and height |
663
|
|
|
if ($this->crop) { |
664
|
|
|
$this->log(" Crop."); |
665
|
|
|
$this->targetWidth = round(isset($this->targetWidth) |
666
|
|
|
? $this->targetWidth |
667
|
|
|
: $this->crop['width']); |
668
|
|
|
$this->targetHeight = round(isset($this->targetHeight) |
669
|
|
|
? $this->targetHeight |
670
|
|
|
: $this->crop['height']); |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
*/ |
674
|
|
|
|
675
|
65 |
|
return $this; |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
|
679
|
|
|
|
680
|
|
|
/** |
681
|
|
|
* Get source width. |
682
|
|
|
* |
683
|
|
|
* @return integer as source width |
684
|
|
|
*/ |
685
|
1 |
|
public function getSourceWidth() |
686
|
|
|
{ |
687
|
1 |
|
return $this->srcWidth; |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
|
691
|
|
|
|
692
|
|
|
/** |
693
|
|
|
* Get source height. |
694
|
|
|
* |
695
|
|
|
* @return integer as source height |
696
|
|
|
*/ |
697
|
1 |
|
public function getSourceHeight() |
698
|
|
|
{ |
699
|
1 |
|
return $this->srcHeight; |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
|
703
|
|
|
|
704
|
|
|
/** |
705
|
|
|
* Get target width. |
706
|
|
|
* |
707
|
|
|
* @return integer as target width |
708
|
|
|
*/ |
709
|
93 |
|
public function getTargetWidth() |
710
|
|
|
{ |
711
|
93 |
|
return $this->targetWidth; |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
|
715
|
|
|
|
716
|
|
|
/** |
717
|
|
|
* Get target height. |
718
|
|
|
* |
719
|
|
|
* @return integer as target height |
720
|
|
|
*/ |
721
|
93 |
|
public function getTargetHeight() |
722
|
|
|
{ |
723
|
93 |
|
return $this->targetHeight; |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
|
727
|
|
|
|
728
|
|
|
/** |
729
|
|
|
* Get destination x. |
730
|
|
|
* |
731
|
|
|
* @return integer as destination x |
732
|
|
|
*/ |
733
|
16 |
|
public function getDestinationX() |
734
|
|
|
{ |
735
|
16 |
|
return $this->destinationX; |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
|
739
|
|
|
|
740
|
|
|
/** |
741
|
|
|
* Get destination y. |
742
|
|
|
* |
743
|
|
|
* @return integer as destination y |
744
|
|
|
*/ |
745
|
16 |
|
public function getDestinationY() |
746
|
|
|
{ |
747
|
16 |
|
return $this->destinationY; |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
|
751
|
|
|
|
752
|
|
|
/** |
753
|
|
|
* Get destination width. |
754
|
|
|
* |
755
|
|
|
* @return integer as destination width |
756
|
|
|
*/ |
757
|
16 |
|
public function getDestinationWidth() |
758
|
|
|
{ |
759
|
16 |
|
return $this->destinationWidth; |
760
|
|
|
} |
761
|
|
|
|
762
|
|
|
|
763
|
|
|
|
764
|
|
|
/** |
765
|
|
|
* Get destination height. |
766
|
|
|
* |
767
|
|
|
* @return integer as destination height |
768
|
|
|
*/ |
769
|
16 |
|
public function getDestinationHeight() |
770
|
|
|
{ |
771
|
16 |
|
return $this->destinationHeight; |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
|
775
|
|
|
|
776
|
|
|
/** |
777
|
|
|
* Get crop position x. |
778
|
|
|
* |
779
|
|
|
* @return integer |
780
|
|
|
*/ |
781
|
42 |
|
public function getCropX() |
782
|
|
|
{ |
783
|
42 |
|
return $this->cropX; |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
|
787
|
|
|
|
788
|
|
|
/** |
789
|
|
|
* Get crop position y. |
790
|
|
|
* |
791
|
|
|
* @return integer |
792
|
|
|
*/ |
793
|
42 |
|
public function getCropY() |
794
|
|
|
{ |
795
|
42 |
|
return $this->cropY; |
796
|
|
|
} |
797
|
|
|
|
798
|
|
|
|
799
|
|
|
|
800
|
|
|
/** |
801
|
|
|
* Get crop width. |
802
|
|
|
* |
803
|
|
|
* @return integer |
804
|
|
|
*/ |
805
|
42 |
|
public function getCropWidth() |
806
|
|
|
{ |
807
|
42 |
|
return $this->cropWidth; |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
|
811
|
|
|
|
812
|
|
|
/** |
813
|
|
|
* Get crop height. |
814
|
|
|
* |
815
|
|
|
* @return integer |
816
|
|
|
*/ |
817
|
42 |
|
public function getCropHeight() |
818
|
|
|
{ |
819
|
42 |
|
return $this->cropHeight; |
820
|
|
|
} |
821
|
|
|
} |
822
|
|
|
|
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.