Completed
Push — master ( d7d63e...f91d06 )
by Oscar
02:08
created

Dimmensions::getIntegerValue()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 14
rs 8.8571
cc 5
eloc 7
nc 8
nop 4
1
<?php
2
3
namespace Imagecow\Utils;
4
5
use Imagecow\ImageException;
6
7
/**
8
 * Usefull dimmensions calculations.
9
 */
10
class Dimmensions
11
{
12
    protected static $positionsKeywordsX = [
13
        'left' => '0%',
14
        'center' => '50%',
15
        'right' => '100%',
16
    ];
17
18
    protected static $positionsKeywordsY = [
19
        'top' => '0%',
20
        'middle' => '50%',
21
        'bottom' => '100%',
22
    ];
23
24
    /**
25
     * Calculate the dimensions of a resize.
26
     *
27
     * @param int  $oldWidth
28
     * @param int  $oldHeight
29
     * @param int  $newWidth
30
     * @param int  $newHeight
31
     * @param bool $cover
32
     *
33
     * @return array [width, height]
34
     */
35
    public static function getResizeDimmensions($oldWidth, $oldHeight, $newWidth, $newHeight, $cover = false)
36
    {
37 View Code Duplication
        if (empty($newHeight)) {
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...
38
            return [(int) $newWidth, (int) ceil(($newWidth / $oldWidth) * $oldHeight)];
39
        }
40
41 View Code Duplication
        if (empty($newWidth)) {
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...
42
            return [(int) ceil(($newHeight / $oldHeight) * $oldWidth), (int) $newHeight];
43
        }
44
45
        $scaleWidth = $newWidth / $oldWidth;
46
        $scaleHeight = $newHeight / $oldHeight;
47
48
        if ($cover) {
49 View Code Duplication
            if ($scaleWidth > $scaleHeight) {
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...
50
                return [(int) $newWidth, (int) ceil($scaleWidth * $oldHeight)];
51
            }
52
53 View Code Duplication
            if ($scaleWidth < $scaleHeight) {
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...
54
                return [(int) ceil($scaleHeight * $oldWidth), (int) $newHeight];
55
            }
56
        } else {
57 View Code Duplication
            if ($scaleWidth < $scaleHeight) {
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...
58
                return [(int) $newWidth, (int) ceil($scaleWidth * $oldHeight)];
59
            }
60
61 View Code Duplication
            if ($scaleWidth > $scaleHeight) {
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...
62
                return [(int) ceil($scaleHeight * $oldWidth), (int) $newHeight];
63
            }
64
        }
65
66 View Code Duplication
        if ($scaleWidth < $scaleHeight || ($cover && $scaleWidth > $scaleHeight)) {
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...
67
            return [(int) $newWidth, (int) ceil($scaleWidth * $oldHeight)];
68
        }
69
70 View Code Duplication
        if ($scaleWidth > $scaleHeight || ($cover && $scaleWidth < $scaleHeight)) {
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...
71
            return [(int) ceil($scaleHeight * $oldWidth), (int) $newHeight];
72
        }
73
74
        return [(int) $newWidth, (int) $newHeight];
75
    }
76
77
    /**
78
     * Calculate a dimension value.
79
     *
80
     * @param string     $direction
81
     * @param int|string $value
82
     * @param int        $relatedValue
83
     * @param bool       $position
84
     *
85
     * @return int
86
     */
87
    public static function getIntegerValue($direction, $value, $relatedValue, $position = false)
88
    {
89
        $keywords = ($direction === 'y') ? static::$positionsKeywordsY : static::$positionsKeywordsX;
90
91
        if ($position && isset($keywords[$value])) {
92
            $value = $keywords[$value];
93
        }
94
95
        if (substr($value, -1) === '%') {
96
            return intval(($relatedValue / 100) * floatval(substr($value, 0, -1)));
97
        }
98
99
        return intval($value);
100
    }
101
102
    /**
103
     * Calculates the x/y position.
104
     *
105
     * @param string          $direction (y or x)
106
     * @param string|int|null $position
107
     * @param int             $newValue
108
     * @param int             $oldValue
109
     *
110
     * @return int
111
     */
112
    public static function getPositionValue($direction, $position, $newValue, $oldValue)
113
    {
114
        $split = explode(' ', $position, 2);
115
        $position = $split[0];
116
        $offset = isset($split[1]) ? (int) $split[1] : 0;
117
118
        if (is_numeric($position)) {
119
            return intval($position) + $offset;
120
        }
121
122
        $newCenter = static::getIntegerValue($direction, $position, $newValue, true);
123
        $oldCenter = static::getIntegerValue($direction, $position, $oldValue, true);
124
125
        return $oldCenter - $newCenter + $offset;
126
    }
127
}
128