Test Failed
Push — resize ( fbaf05...026e01 )
by Mikael
02:27
created

CImageResizer::getCropX()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
namespace Mos\CImage;
4
5
/**
6
 * Resize and crop images.
7
 *
8
 * @author  Mikael Roos [email protected]
9
 * @example http://dbwebb.se/opensource/cimage
10
 * @link    https://github.com/mosbth/cimage
11
 */
12
class CImageResizer
13
{
14
    /**
15
     * Log function.
16
     */
17
    private $log;
18
19
20
21
    /**
22
     * Source image dimensions, calculated from loaded image.
23
     */
24
    private $srcWidth;
25
    private $srcHeight;
26
27
28
29
    /**
30
     * Set as expected target image/canvas dimensions.
31
     */
32
    private $targetWidth;
33
    private $targetHeight;
34
35
36
37
    /**
38
     * Where should the image go on the canvas.
39
     */
40
    private $destinationX;
41
    private $destinationY;
42
    private $destinationWidth;
43
    private $destinationHeight;
44
45
46
47
    /**
48
     * Which parts to crop from the source.
49
     */
50
    private $cropX;
51
    private $cropY;
52
    private $cropWidth;
53
    private $cropHeight;
54
55
56
57
    /**
58
     * Change target height & width when different dpr, dpr 2 means double
59
     * image dimensions.
60
     */
61
    private $dpr = null;
62
63
64
65
    /**
66
     * Set aspect ratio for the target image.
67
     */
68
    private $aspectRatio;
69
70
71
72
    /**
73
     * Array with details on how to crop.
74
     * Array contains xxxxxx
75
     */
76
    public $crop;
77
    public $cropOrig; // Save original value?
78
79
80
81
    /**
82
     * Area to use for target image, crop out parts not in area.
83
     * Array with top, right, bottom, left percentage values to crop out.
84
     */
85
    private $area;
86
87
88
89
    /**
90
     * Pixel offset in source image to decide which part of image is used.
91
     * Array with top, right, bottom, left percentage values to crop out.
92
     */
93
    private $offset;
94
95
96
97
    /**
98
     * Resize strategy, image should keep its original ratio.
99
     */
100
    const KEEP_RATIO = 1;
101
102
103
104
    /**
105
     * Resize strategy, image should crop and fill area.
106
     */
107
    const CROP_TO_FIT = 2;
108
109
110
111
    /**
112
     * Resize strategy, image should fit in area and fill remains.
113
     */
114
    const FILL_TO_FIT = 3;
115
116
117
118
    /**
119
     * Resize strategy, image should stretch to fit in area.
120
     */
121
    const STRETCH = 4;
122
123
124
125
    /**
126
     * The currently selected resize strategy.
127
     */
128
    private $resizeStrategy = self::KEEP_RATIO;
129
130
131
132
    /**
133
     * Allow upscale of smaller images by default, set to false to disallow.
134
     */
135
    private $upscale = true;
136
137
138
139
    /**
140
     * Constructor, set log function to use for verbose logging or null
141
     * to disable logging.
142 113
     *
143
     * @param callable $log function to call for logging.
144 113
     */
145 113
    public function __construct($log = null)
146
    {
147
        $this->log = $log;
148
    }
149
150
151
152
    /**
153
     * Log string using logger.
154 104
     *
155
     * @param string $str to log.
156 104
     */
157 1
    public function log($str)
158 1
    {
159 104
        if ($this->log) {
160
            call_user_func($this->log, $str);
161
        }
162
    }
163
164
165
166
    /**
167
     * Set source dimensions.
168
     *
169
     * @param integer $width  of source image.
170
     * @param integer $height of source image.
171
     *
172
     * @throws Exception
173 94
     *
174
     * @return $this
175 94
     */
176 94
    public function setSource($width, $height)
177 94
    {
178
        $this->srcWidth  = $width;
179 94
        $this->srcHeight = $height;
180
        $this->log("# Source image dimension: {$this->srcWidth}x{$this->srcHeight}.");
181
182
        return $this;
183
    }
184
185
186
187
    /**
188
     * Get resize strategy as string.
189 70
     *
190
     * @return string
191 70
     */
192 70
    public function getResizeStrategyAsString()
193 21
    {
194
        switch ($this->resizeStrategy) {
195
            case self::KEEP_RATIO:
196 49
                return "KEEP_RATIO";
197 23
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

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.

Loading history...
198
199
            case self::CROP_TO_FIT:
200 26
                return "CROP_TO_FIT";
201 17
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

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.

Loading history...
202
203
            case self::FILL_TO_FIT:
204 9
                return "FILL_TO_FIT";
205 8
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

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.

Loading history...
206
207
            case self::STRETCH:
208 1
                return "STRETCH";
209 1
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

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.

Loading history...
210 1
211
            default:
212
                return "UNKNOWN";
213
        }
214
    }
215
216
217
218
     /**
219
      * Set resize strategy as KEEP_RATIO, CROP_TO_FIT or FILL_TO_FIT.
220
      *
221
      * @param integer $strategy
222 70
      *
223
      * @return $this
224 70
      */
225 70
    public function setResizeStrategy($strategy)
226
    {
227 70
        $this->resizeStrategy = $strategy;
228
        $this->log("# Resize strategy is " . $this->getResizeStrategyAsString());
229
230
        return $this;
231
    }
232
233
234
235
    /**
236
     * Allow or disallow upscale smaller images.
237
     *
238
     * @param boolean $upscale
239 22
     *
240
     * @return $this
241 22
     */
242 22
    public function allowUpscale($upscale)
243
    {
244 22
        $this->upscale = $upscale;
245
        $this->log("# Allow upscale is $this->upscale.");
246
247
        return $this;
248
    }
249
250
251
252
    /**
253
     * Check if a value is upscaled or not by compare $current to $orig,
254
     * if $current is larger than $orig then check if upscaled is allowed.
255
     *
256
     * @param float &$current value to check and change.
257
     * @param float  $orig    value to check against.
258 33
     *
259
     * @return boolean true if upscaled changed values, else false.
260 33
     */
261 6
    public function respectUpscale(&$current, $orig)
262 6
    {
263 6
        if (!$this->upscale && $current > $orig) {
264 33
            $this->log("# Disallowed upscale to $current ($orig)");
265
            $current = $orig;
266
            return true;
267
        }
268
269
        return false;
270
    }
271
272
273
274
    /**
275
     * Set base for requested width and height.
276
     *
277
     * @param numeric|null $width  as requested target width
278 98
     * @param numeric|null $height as requested target height
279
     *
280 98
     * @throws Exception
281
     *
282 98
     * @return $this
283 98
     */
284
    public function setBaseWidthHeight($width = null, $height = null)
285
    {
286 98
        $this->log("# Set base for width and height.");
287 2
288 2
        $this->targetWidth  = $width;
289 2
        $this->targetHeight = $height;
290
291
        // Width specified as %
292 98 View Code Duplication
        if ($this->targetWidth[strlen($this->targetWidth)-1] == '%') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
293 2
            $this->targetWidth = $this->srcWidth * substr($this->targetWidth, 0, -1) / 100;
294 2
            $this->log(" Setting new width based on $width to {$this->targetWidth}.");
295 2
        }
296
297 98
        // Height specified as %
298 1 View Code Duplication
        if ($this->targetHeight[strlen($this->targetHeight)-1] == '%') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
299
            $this->targetHeight = $this->srcHeight * substr($this->targetHeight, 0, -1) / 100;
300
            $this->log(" Setting new height based on $height to {$this->targetHeight}.");
301 97
        }
302 1
303
        if (!(is_null($this->targetWidth) || is_numeric($this->targetWidth))) {
304
            throw new Exception('Width not numeric');
305 96
        }
306
307 96
        if (!(is_null($this->targetHeight) || is_numeric($this->targetHeight))) {
308
            throw new Exception('Height not numeric');
309
        }
310
311
        $this->log(" Requested target dimension as: {$this->targetWidth}x{$this->targetHeight}.");
312
313
        return $this;
314
    }
315
316
317
318
     /**
319
      * Set base for requested aspect ratio.
320
      *
321 15
      * @param float|null $aspectRatio as requested aspect ratio
322
      *
323 15
      * @throws Exception
324
      *
325 15
      * @return $this
326
      */
327 15 View Code Duplication
    public function setBaseAspecRatio($aspectRatio = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
328 1
    {
329
        $this->log("# Set base for aspect ratio.");
330
331 14
        $this->aspectRatio = $aspectRatio;
332
333 14
        if (!(is_null($this->aspectRatio) || is_numeric($this->aspectRatio))) {
334
            throw new Exception("Aspect ratio out of range");
335
        }
336
337
        $this->log(" Requested aspectRatio={$this->aspectRatio}.");
338
339
        return $this;
340
    }
341
342
343
344
    /**
345
     * Set base for requested device pixel ratio.
346
     *
347 14
     * @param float $dpr as requested density pixel rate
348
     *
349 14
     * @throws Exception
350
     *
351 14
     * @return $this
352
     */
353 14 View Code Duplication
    public function setBaseDevicePixelRate($dpr = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
354 1
    {
355
        $this->log("# Set base for device pixel rate.");
356
357 13
        $this->dpr = $dpr;
358
359 13
        if (!(is_null($dpr) || (is_numeric($this->dpr) && $this->dpr > 0))) {
360
            throw new Exception("Device pixel rate out of range");
361
        }
362
363
        $this->log(" Requested dpr={$this->dpr}.");
364
365
        return $this;
366
    }
367
368
369
370
    /**
371
     * Calculate target width and height by considering the selected
372 26
     * aspect ratio.
373
     *
374 26
     * @throws Exception
375
     *
376 26
     * @return $this
377 14
     */
378
    public function prepareByConsiderAspectRatio()
379
    {
380
        $this->log(" Prepare by aspect ratio {$this->aspectRatio}.");
381 12
382
        if (is_null($this->aspectRatio)) {
383 3
            return $this;
384 3
        }
385 2
386
        // Both null, use source as base for target
387 3
        if (is_null($this->targetWidth) && is_null($this->targetHeight)) {
388 3
            $this->targetWidth = ($this->aspectRatio >= 1)
389 3
                ? $this->srcWidth
390
                : null;
391 3
392
            $this->targetHeight = ($this->aspectRatio >= 1)
393 3
                ? null
394
                : $this->srcHeight;
395
396 12
            $this->log("  Using source as base {$this->targetWidth}x{$this->targetHeight}");
397
        }
398 3
399 3
        // Both or either set, calculate the other
400 3
        if (isset($this->targetWidth) && isset($this->targetHeight)) {
401
            $this->targetWidth = ($this->aspectRatio >= 1)
402 3
                ? $this->targetWidth
403 3
                : $this->targetHeight * $this->aspectRatio;
404 3
405
            $this->targetHeight = ($this->aspectRatio >= 1)
406 3
                ? $this->targetWidth / $this->aspectRatio
407
                : $this->targetHeight;
408 12
409
            $this->log("  New target width height {$this->targetWidth}x{$this->targetHeight}");
410 5
        } elseif (isset($this->targetWidth)) {
411 5
            $this->targetHeight = $this->targetWidth / $this->aspectRatio;
412
            $this->log("  New target height x{$this->targetHeight}");
413 9
        } elseif (isset($this->targetHeight)) {
414
            $this->targetWidth = $this->targetHeight * $this->aspectRatio;
415 4
            $this->log("  New target width {$this->targetWidth}x");
416 4
        }
417
418 4
        return $this;
419
    }
420 12
421
422
423
    /**
424
     * Calculate target width and height by considering the selected
425
     * dpr.
426
     *
427
     * @throws Exception
428
     *
429
     * @return $this
430
     */
431
    public function prepareByConsiderDpr()
432
    {
433 26
        $this->log(" Prepare by dpr={$this->dpr}.");
434
435 26
        if (is_null($this->dpr)) {
436
            return $this;
437 26
        }
438 14
439
        // If both not set, use source as base
440
        if (is_null($this->targetWidth) && is_null($this->targetHeight)) {
441
            $this->targetWidth  = $this->srcWidth;
442 12
            $this->targetHeight = $this->srcHeight;
443 3
        }
444 3
445 3
        if (isset($this->targetWidth)) {
446
            $this->targetWidth = $this->targetWidth * $this->dpr;
447 12
            $this->log("  Update target width to {$this->targetWidth}.");
448 9
        }
449 9
450 9
        if (isset($this->targetHeight)) {
451
            $this->targetHeight = $this->targetHeight * $this->dpr;
452 12
            $this->log("  Update target height to {$this->targetHeight}.");
453 9
        }
454 9
455 9
        return $this;
456
    }
457 12
458
459
460
     /**
461
      * Calculate target width and height and do sanity checks on constraints.
462
      * After this method the $targetWidth and $targetHeight will have
463
      * the expected dimensions on the target image.
464
      *
465
      * @throws Exception
466
      *
467
      * @return $this
468
      */
469
    public function prepareTargetDimensions()
470
    {
471 26
        $this->log("# Prepare target dimension (before): {$this->targetWidth}x{$this->targetHeight}.");
472
473 26
        $this->prepareByConsiderAspectRatio()
474
             ->prepareByConsiderDpr();
475 26
476 26
        $this->log(" Prepare target dimension (after): {$this->targetWidth}x{$this->targetHeight}.");
477
478 26
        return $this;
479
    }
480 26
481
482
483
     /**
484
      * Calculate new width and height of image.
485
      *
486
      * @return $this
487
      */
488
    public function calculateTargetWidthAndHeight()
489
    {
490 65
        $this->log("# Calculate new width and height.");
491
        $this->log(" Source size {$this->srcWidth}x{$this->srcHeight}.");
492 65
        $this->log(" Target dimension (before) {$this->targetWidth}x{$this->targetHeight}.");
493 65
494 65
        // Set default values to crop area to be whole source image
495
        $sw = $this->srcWidth;
496
        $sh = $this->srcHeight;
497 65
        $ar = $sw / $sh;
498 65
        $tw = $this->targetWidth;
499 65
        $th = $this->targetHeight;
500 65
        $dx = 0;
501 65
        $dy = 0;
502 65
        $dw = null;
503 65
        $dh = null;
504 65
        $cx = 0;
505 65
        $cy = 0;
506 65
        $cw = $sw;
507 65
        $ch = $sh;
508 65
        $rs = $this->resizeStrategy;
509 65
        $both = isset($tw) && isset($th);
510 65
        $ratio = $both ? $tw / $th : null;
511 65
512 65
        if (is_null($tw) && is_null($th)) {
513
            // No tw/th use sw/sh
514 65
            $tw = $sw;
515
            $th = $sh;
516
            $this->log("  New tw x th {$tw}x{$th}");
517 3
        } elseif (isset($tw) && is_null($th)) {
518 3
            // Keep aspect ratio, make th based on tw
519 3
            $this->respectUpscale($tw, $sw);
520
            $th = $tw / $ar;
521 65
            $this->log("  New th x{$th}");
522
        } elseif (is_null($tw) && isset($th)) {
523
            // Keep aspect ratio, make tw based on th
524 4
            $this->respectUpscale($th, $sh);
525 4
            $tw = $th * $ar;
526 4
            $this->log("  New tw {$tw}x");
527
        } elseif ($rs === CImageResizer::KEEP_RATIO && $both) {
528 62
            // Keep aspect ratio, make fit in box not larger than tw/th
529
            $this->log("  Keep ratio, ratio target=$ratio, source=$ar");
530
            
531 4
            if ($ratio > $ar) {
532 4
                $this->respectUpscale($th, $sh);
533 4
                $tw = $th * $ar;
534
                $this->log("   New tw {$tw}x");
535 58
            } elseif ($ratio < $ar) {
536
                $this->respectUpscale($tw, $sw);
537
                $th = $tw / $ar;
538 9
                $this->log("   New th x{$th}");
539
            } else {
540 9
                $this->respectUpscale($tw, $sw);
541 2
                $this->respectUpscale($th, $sh);
542 2
            }
543 2
        } elseif ($rs === CImageResizer::STRETCH && $both) {
544 9
            // Stretch to fit, leave as is
545 2
            $this->log("  Stretch");
546 2
547 2
            // respectUpscale
548 2
            $dw = $tw;
549 5
            $dh = $th;
550 5
            $this->respectUpscale($dw, $sw);
551
            $this->respectUpscale($dh, $sh);
552
            $dx = ($tw - $dw) / 2;
553 54
            $dy = ($th - $dh) / 2;
554
555
            $this->log("   Destination area dx=$dx, dy=$dy, dw=$dw, dh=$dh");
556
        } elseif ($rs === CImageResizer::CROP_TO_FIT && $both) {
557 7
            // Crop to fit image in box
558
            $this->log("  Crop to fit, ratio target=$ratio, source=$ar");
559 45
560
            // Respect upscale
561
            $dw = $tw;
562
            $dh = $th;
563 22
            $this->respectUpscale($dw, $sw);
564
            $this->respectUpscale($dh, $sh);
565 22
            $dx = ($tw - $dw) / 2;
566 8
            $dy = ($th - $dh) / 2;
567 8
568 22
            // Manage landscape/portrait
569 8
            if ($ratio > $ar) {
570 8
                $ch = $sw / $ratio;
571 8
                $cy = ($sh - $ch) / 2;
572
                $this->log("   Crop by cy=$cy ch=$ch");
573 22
            } elseif ($ratio < $ar) {
574
                $cw = $sh * $ratio;
575 38
                $cx = ($sw - $cw) / 2;
576
                $this->log("   Crop by cx=$cx cw=$cw");
577
            }
578 16
579 16
            // Update crop when no upscale
580 16
            if (!$this->upscale && $dx) {
581
                $cy = $th < $sh ? ($sh - $th) / 2 : 0;
582 16
                $ch = $dh;
583 5
            }
584 5
            if (!$this->upscale && $dy) {
585 16
                $cx = $tw < $sw ? ($sw - $tw) / 2 : 0;
586 6
                $cw = $dw;
587 6
            }
588 6
589
            $this->log("   Parts cx=$cx, cy=$cy, cw=$cw, ch=$ch");
590 16
            $this->log("   Destination area dx=$dx, dy=$dy, dw=$dw, dh=$dh");
591 16
        } elseif ($rs === CImageResizer::FILL_TO_FIT && $both) {
592 16
            // Fill to fit image in box
593 16
            $this->log("  Fill to fit, ratio target=$ratio, source=$ar");
594
            $dw = $tw;
595 16
            $dh = $th;
596
597 16
            // Manage landscape/portrait
598
            if ($ratio > $ar) {
599
                $dw = $th * $ar;
600 65
                $dh = $th;
601 65
            } elseif ($ratio < $ar) {
602
                $dw = $tw;
603 65
                $dh = $tw / $ar;
604 65
            }
605 65
606 65
            $this->respectUpscale($dw, $sw);
607 65
            $this->respectUpscale($dh, $sh);
608 65
            $dx = ($tw - $dw) / 2;
609 65
            $dy = ($th - $dh) / 2;
610 65
611 65
            $this->log("   Destination area dx=$dx, dy=$dy, dw=$dw, dh=$dh");
612 65
        }
613
614 65
        // All done, sum it up
615 65
        $dw = is_null($dw) ? $tw : $dw;
616 65
        $dh = is_null($dh) ? $th : $dh;
617
618
        $this->targetWidth       = round($tw);
619
        $this->targetHeight      = round($th);
620
        $this->destinationX      = round($dx);
621
        $this->destinationY      = round($dy);
622
        $this->destinationWidth  = round($dw);
623
        $this->destinationHeight = round($dh);
624
        $this->cropX             = round($cx);
625
        $this->cropY             = round($cy);
626
        $this->cropWidth         = round($cw);
627
        $this->cropHeight        = round($ch);
628
629
        $str = <<<EOD
630
 Target dimension (after) {$this->targetWidth}x{$this->targetHeight}.
631
  Crop area {$this->cropX}x{$this->cropY} by {$this->cropWidth}x{$this->cropHeight}.
632
  Destination area {$this->destinationX}x{$this->destinationY} by {$this->destinationWidth}x{$this->destinationHeight}.
633
EOD;
634
        $this->log($str);
635
636
/*
637
638
        // Check if there is an area to crop off
639
        if (isset($this->area)) {
640
            $this->offset['top']    = round($this->area['top'] / 100 * $this->srcHeight);
641
            $this->offset['right']  = round($this->area['right'] / 100 * $this->srcWidth);
642
            $this->offset['bottom'] = round($this->area['bottom'] / 100 * $this->srcHeight);
643
            $this->offset['left']   = round($this->area['left'] / 100 * $this->srcWidth);
644
            $this->offset['width']  = $this->srcWidth - $this->offset['left'] - $this->offset['right'];
645
            $this->offset['height'] = $this->srcHeight - $this->offset['top'] - $this->offset['bottom'];
646
            $this->srcWidth  = $this->offset['width'];
647
            $this->srcHeight = $this->offset['height'];
648
            $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']}%.");
649
            $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.");
650
        }
651
652
653
        // Check if crop is set
654
        if ($this->crop) {
655
            $width  = $this->crop['width']  = $this->crop['width'] <= 0 ? $this->srcWidth + $this->crop['width'] : $this->crop['width'];
656
            $height = $this->crop['height'] = $this->crop['height'] <= 0 ? $this->srcHeight + $this->crop['height'] : $this->crop['height'];
657
658
            if ($this->crop['start_x'] == 'left') {
659
                $this->crop['start_x'] = 0;
660
            } elseif ($this->crop['start_x'] == 'right') {
661
                $this->crop['start_x'] = $this->srcWidth - $width;
662
            } elseif ($this->crop['start_x'] == 'center') {
663
                $this->crop['start_x'] = round($this->srcWidth / 2) - round($width / 2);
664
            }
665
666
            if ($this->crop['start_y'] == 'top') {
667
                $this->crop['start_y'] = 0;
668
            } elseif ($this->crop['start_y'] == 'bottom') {
669
                $this->crop['start_y'] = $this->srcHeight - $height;
670
            } elseif ($this->crop['start_y'] == 'center') {
671
                $this->crop['start_y'] = round($this->srcHeight / 2) - round($height / 2);
672
            }
673
674
            $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.");
675 65
        }
676
677
678
679
        // Crop, ensure to set new width and height
680
        if ($this->crop) {
681
            $this->log(" Crop.");
682
            $this->targetWidth = round(isset($this->targetWidth)
683
               ? $this->targetWidth
684
               : $this->crop['width']);
685 1
            $this->targetHeight = round(isset($this->targetHeight)
686
               ? $this->targetHeight
687 1
               : $this->crop['height']);
688
        }
689
690
*/
691
692
        return $this;
693
    }
694
695
696
697 1
    /**
698
     * Get source width.
699 1
     *
700
     * @return integer as source width
701
     */
702
    public function getSourceWidth()
703
    {
704
        return $this->srcWidth;
705
    }
706
707
708
709 93
    /**
710
     * Get source height.
711 93
     *
712
     * @return integer as source height
713
     */
714
    public function getSourceHeight()
715
    {
716
        return $this->srcHeight;
717
    }
718
719
720
721 93
    /**
722
     * Get target width.
723 93
     *
724
     * @return integer as target width
725
     */
726
    public function getTargetWidth()
727
    {
728
        return $this->targetWidth;
729
    }
730
731
732
733 16
    /**
734
     * Get target height.
735 16
     *
736
     * @return integer as target height
737
     */
738
    public function getTargetHeight()
739
    {
740
        return $this->targetHeight;
741
    }
742
743
744
745 16
    /**
746
     * Get destination x.
747 16
     *
748
     * @return integer as destination x
749
     */
750
    public function getDestinationX()
751
    {
752
        return $this->destinationX;
753
    }
754
755
756
757 16
    /**
758
     * Get destination y.
759 16
     *
760
     * @return integer as destination y
761
     */
762
    public function getDestinationY()
763
    {
764
        return $this->destinationY;
765
    }
766
767
768
769 16
    /**
770
     * Get destination width.
771 16
     *
772
     * @return integer as destination width
773
     */
774
    public function getDestinationWidth()
775
    {
776
        return $this->destinationWidth;
777
    }
778
779
780
781 42
    /**
782
     * Get destination height.
783 42
     *
784
     * @return integer as destination height
785
     */
786
    public function getDestinationHeight()
787
    {
788
        return $this->destinationHeight;
789
    }
790
791
792
793 42
    /**
794
     * Get crop position x.
795 42
     *
796
     * @return integer
797
     */
798
    public function getCropX()
799
    {
800
        return $this->cropX;
801
    }
802
803
804
805 42
    /**
806
    * Get crop position y.
807 42
    *
808
    * @return integer
809
    */
810
    public function getCropY()
811
    {
812
        return $this->cropY;
813
    }
814
815
816
817 42
    /**
818
    * Get crop width.
819 42
    *
820
    * @return integer
821
    */
822
    public function getCropWidth()
823
    {
824
        return $this->cropWidth;
825
    }
826
827
828
829
    /**
830
     * Get crop height.
831
     *
832
     * @return integer
833
     */
834
    public function getCropHeight()
835
    {
836
        return $this->cropHeight;
837
    }
838
}
839