|
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) { |
|
|
|
|
|
|
48
|
|
|
return $this->resizeToWidth($width, $height, $newwidth); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (!$newwidth) { |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: