Dimensions::resizeTo()   C
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 38
rs 6.7272
cc 7
eloc 22
nc 6
nop 4
1
<?php
2
namespace Gwa\Image;
3
4
/**
5
 * Calculates new image dimensions
6
 */
7
class Dimensions
8
{
9
    /**
10
     * @param int $maxwidth
11
     * @param int $maxheight
12
     * @return \stdClass
13
     */
14
    public function resizeToWithin($width, $height, $maxwidth, $maxheight)
15
    {
16
        if ($width <= $maxwidth && $height <= $maxheight) {
17
            return null;
18
        }
19
20
        // calculate ratio based on widths
21
        $ratio = $maxwidth / $width;
22
        if ($height * $ratio > $maxheight) {
23
            // new height greater than maximum
24
            $ratio = $maxheight / $height;
25
        }
26
27
        $dimensions = new \stdClass;
28
        $dimensions->width = max(1, round($width * $ratio));
29
        $dimensions->height = max(1, round($height * $ratio));
30
31
        return $dimensions;
32
    }
33
34
    /**
35
     * @param int $width
36
     * @param int $height
37
     * @param int|null $newwidth
38
     * @param int|null $newheight
39
     * @return \stdClass|null
40
     */
41
    public function resizeTo($width, $height, $newwidth, $newheight)
42
    {
43
        if ($width === $newwidth && $height === $newheight) {
44
            return null;
45
        }
46
47
        if (!$newheight) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $newheight of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
48
            return $this->resizeToWidth($width, $height, $newwidth);
49
        }
50
51
        if (!$newwidth) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $newwidth of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
52
            return $this->resizeToHeight($width, $height, $newheight);
53
        }
54
55
        $ratio = $newwidth / $width;
56
57
        $dimensions = new \stdClass;
58
        $dimensions->overhang = false;
59
        $dimensions->width = $newwidth;
60
        $dimensions->height = $newheight;
61
62
        if ($height * $ratio < $newheight) {
63
            // - height is too small
64
            // - resize to height, and crop horizontal overhang
65
            $ratio = $newheight / $height;
66
            $dimensions->width = max(1, round($width * $ratio));
67
            $dimensions->height = $newheight;
68
            $dimensions->overhang = true;
69
        } elseif ($height * $ratio > $newheight) {
70
            // - height is too large
71
            // - resize to width, and crop vertical overhang
72
            $dimensions->width = $newwidth;
73
            $dimensions->height = max(1, round($height * $ratio));
74
            $dimensions->overhang = true;
75
        }
76
77
        return $dimensions;
78
    }
79
80
    /**
81
     * @param int $width
82
     * @param int $height
83
     * @param int $newwidth
84
     * @return \stdClass|null
85
     */
86 View Code Duplication
    private function resizeToWidth($width, $height, $newwidth)
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...
87
    {
88
        if ($width === $newwidth) {
89
            return null;
90
        }
91
92
        $ratio = $newwidth / $width;
93
94
        $dimensions = new \stdClass;
95
        $dimensions->width = $newwidth;
96
        $dimensions->height = max(1, round($height * $ratio));
97
        $dimensions->overhang = false;
98
99
        return $dimensions;
100
    }
101
102
    /**
103
     * @param int $width
104
     * @param int $height
105
     * @param int $newheight
106
     * @return \stdClass
107
     */
108 View Code Duplication
    private function resizeToHeight($width, $height, $newheight)
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...
109
    {
110
        if ($height === $newheight) {
111
            return null;
112
        }
113
114
        $ratio = $newheight / $height;
115
116
        $dimensions = new \stdClass;
117
        $dimensions->width = max(1, round($width * $ratio));
118
        $dimensions->height = $newheight;
119
        $dimensions->overhang = false;
120
121
        return $dimensions;
122
    }
123
}
124