Completed
Push — master ( 80eb51...d7d63e )
by Oscar
7s
created

Dimmensions::getPositionFromReferenceValue()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
rs 7.7777
cc 8
eloc 13
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 $positionsKeywords = [
13
        'top' => '0%',
14
        'left' => '0%',
15
        'middle' => '50%',
16
        'center' => '50%',
17
        'right' => '100%',
18
        'bottom' => '100%',
19
    ];
20
21
    /**
22
     * Calculate the dimensions of a resize.
23
     *
24
     * @param int  $oldWidth
25
     * @param int  $oldHeight
26
     * @param int  $newWidth
27
     * @param int  $newHeight
28
     * @param bool $cover
29
     *
30
     * @return array [width, height]
31
     */
32
    public static function getResizeDimmensions($oldWidth, $oldHeight, $newWidth, $newHeight, $cover = false)
33
    {
34 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...
35
            return [(int) $newWidth, (int) ceil(($newWidth / $oldWidth) * $oldHeight)];
36
        }
37
38 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...
39
            return [(int) ceil(($newHeight / $oldHeight) * $oldWidth), (int) $newHeight];
40
        }
41
42
        $scaleWidth = $newWidth / $oldWidth;
43
        $scaleHeight = $newHeight / $oldHeight;
44
45
        if ($cover) {
46 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...
47
                return [(int) $newWidth, (int) ceil($scaleWidth * $oldHeight)];
48
            }
49
50 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...
51
                return [(int) ceil($scaleHeight * $oldWidth), (int) $newHeight];
52
            }
53
        } else {
54 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...
55
                return [(int) $newWidth, (int) ceil($scaleWidth * $oldHeight)];
56
            }
57
58 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...
59
                return [(int) ceil($scaleHeight * $oldWidth), (int) $newHeight];
60
            }
61
        }
62
63 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...
64
            return [(int) $newWidth, (int) ceil($scaleWidth * $oldHeight)];
65
        }
66
67 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...
68
            return [(int) ceil($scaleHeight * $oldWidth), (int) $newHeight];
69
        }
70
71
        return [(int) $newWidth, (int) $newHeight];
72
    }
73
74
    /**
75
     * Calculate a dimension value.
76
     *
77
     * @param int|string $value
78
     * @param int        $relatedValue
79
     * @param bool       $position
80
     *
81
     * @return int
82
     */
83
    public static function getIntegerValue($value, $relatedValue, $position = false)
84
    {
85
        if ($position && isset(static::$positionsKeywords[$value])) {
86
            $value = static::$positionsKeywords[$value];
87
        }
88
89
        if (substr($value, -1) === '%') {
90
            return intval(($relatedValue / 100) * floatval(substr($value, 0, -1)));
91
        }
92
93
        return intval($value);
94
    }
95
96
    /**
97
     * Calculate a dimension value.
98
     *
99
     * @param int|string $value
100
     * @param int        $relatedValue
101
     * @param bool       $position
102
     *
103
     * @return string
104
     */
105
    public static function getPercentageValue($value, $relatedValue, $position = false)
106
    {
107
        if ($position && isset(static::$positionsKeywords[$value])) {
108
            return static::$positionsKeywords[$value];
109
        }
110
111
        if (substr($value, -1) === '%') {
112
            return $value;
113
        }
114
115
        if (is_numeric($value)) {
116
            return empty($value) ? '0%' : (($value / $relatedValue) * 100).'%';
117
        }
118
119
        throw new ImageException("Invalid position: {$value}");
120
    }
121
122
    /**
123
     * Calculates the x/y position.
124
     *
125
     * @param string|int|null $position
126
     * @param int             $newValue
127
     * @param int             $oldValue
128
     *
129
     * @return int
130
     */
131
    public static function getPositionValue($position, $newValue, $oldValue)
132
    {
133
        if (is_numeric($position)) {
134
            return intval($position);
135
        }
136
137
        $newCenter = static::getIntegerValue($position, $newValue, true);
138
        $oldCenter = static::getIntegerValue($position, $oldValue, true);
139
140
        return $oldCenter - $newCenter;
141
    }
142
143
    /**
144
     * Calculates the x/y position.
145
     *
146
     * @param string|int|null $position
147
     * @param int             $original
148
     * @param int             $reference
149
     * @param int             $padding
150
     *
151
     * @return int
152
     */
153
    public static function getPositionFromReferenceValue($position, $original, $reference, $padding = 0)
154
    {
155
        if (is_numeric($position)) {
156
            $value = intval($position);
157
        } else {
158
            $value = static::getIntegerValue($position, $original, true);
159
        }
160
161
        $padding = static::getIntegerValue($padding, $original, true);
162
163
        if (($position === 'top') || ($position === 'left')) {
164
            $value = $padding;
165
        } elseif (($position === 'bottom') || ($position === 'right')) {
166
            $value -= $reference + $padding;
167
        } elseif (($position === 'middle') || ($position === 'center')) {
168
            $value = ($original / 2) - ($reference / 2);
169
        }
170
171
        return $value;
172
    }
173
}
174