forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsize.php
More file actions
212 lines (160 loc) · 6.46 KB
/
size.php
File metadata and controls
212 lines (160 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* @group image
* @group media
* @group upload
*/
class Tests_Image_Size extends WP_UnitTestCase {
function test_constrain_dims_zero() {
// No constraint - should have no effect.
$out = wp_constrain_dimensions( 640, 480, 0, 0 );
$this->assertSame( array( 640, 480 ), $out );
$out = wp_constrain_dimensions( 640, 480 );
$this->assertSame( array( 640, 480 ), $out );
$out = wp_constrain_dimensions( 0, 0, 0, 0 );
$this->assertSame( array( 0, 0 ), $out );
$out = wp_constrain_dimensions( 465, 700, 177, 177 );
$this->assertSame( array( 118, 177 ), $out );
}
function test_constrain_dims_smaller() {
// Image size is smaller than the constraint - no effect.
$out = wp_constrain_dimensions( 500, 600, 1024, 768 );
$this->assertSame( array( 500, 600 ), $out );
$out = wp_constrain_dimensions( 500, 600, 0, 768 );
$this->assertSame( array( 500, 600 ), $out );
$out = wp_constrain_dimensions( 500, 600, 1024, 0 );
$this->assertSame( array( 500, 600 ), $out );
}
function test_constrain_dims_equal() {
// Image size is equal to the constraint - no effect.
$out = wp_constrain_dimensions( 1024, 768, 1024, 768 );
$this->assertSame( array( 1024, 768 ), $out );
$out = wp_constrain_dimensions( 1024, 768, 0, 768 );
$this->assertSame( array( 1024, 768 ), $out );
$out = wp_constrain_dimensions( 1024, 768, 1024, 0 );
$this->assertSame( array( 1024, 768 ), $out );
}
function test_constrain_dims_larger() {
// Image size is larger than the constraint - result should be constrained.
$out = wp_constrain_dimensions( 1024, 768, 500, 600 );
$this->assertSame( array( 500, 375 ), $out );
$out = wp_constrain_dimensions( 1024, 768, 0, 600 );
$this->assertSame( array( 800, 600 ), $out );
$out = wp_constrain_dimensions( 1024, 768, 500, 0 );
$this->assertSame( array( 500, 375 ), $out );
// Also try a portrait oriented image.
$out = wp_constrain_dimensions( 300, 800, 500, 600 );
$this->assertSame( array( 225, 600 ), $out );
$out = wp_constrain_dimensions( 300, 800, 0, 600 );
$this->assertSame( array( 225, 600 ), $out );
$out = wp_constrain_dimensions( 300, 800, 200, 0 );
$this->assertSame( array( 200, 533 ), $out );
}
function test_constrain_dims_boundary() {
// One dimension is larger than the constraint, one smaller - result should be constrained.
$out = wp_constrain_dimensions( 1024, 768, 500, 800 );
$this->assertSame( array( 500, 375 ), $out );
$out = wp_constrain_dimensions( 1024, 768, 2000, 700 );
$this->assertSame( array( 933, 700 ), $out );
// Portrait.
$out = wp_constrain_dimensions( 768, 1024, 800, 500 );
$this->assertSame( array( 375, 500 ), $out );
$out = wp_constrain_dimensions( 768, 1024, 2000, 700 );
$this->assertSame( array( 525, 700 ), $out );
}
/**
* @expectedDeprecated wp_shrink_dimensions
*/
function test_shrink_dimensions_default() {
$out = wp_shrink_dimensions( 640, 480 );
$this->assertSame( array( 128, 96 ), $out );
$out = wp_shrink_dimensions( 480, 640 );
$this->assertSame( array( 72, 96 ), $out );
}
/**
* @expectedDeprecated wp_shrink_dimensions
*/
function test_shrink_dimensions_smaller() {
// Image size is smaller than the constraint - no effect.
$out = wp_shrink_dimensions( 500, 600, 1024, 768 );
$this->assertSame( array( 500, 600 ), $out );
$out = wp_shrink_dimensions( 600, 500, 1024, 768 );
$this->assertSame( array( 600, 500 ), $out );
}
/**
* @expectedDeprecated wp_shrink_dimensions
*/
function test_shrink_dimensions_equal() {
// Image size is equal to the constraint - no effect.
$out = wp_shrink_dimensions( 500, 600, 500, 600 );
$this->assertSame( array( 500, 600 ), $out );
$out = wp_shrink_dimensions( 600, 500, 600, 500 );
$this->assertSame( array( 600, 500 ), $out );
}
/**
* @expectedDeprecated wp_shrink_dimensions
*/
function test_shrink_dimensions_larger() {
// Image size is larger than the constraint - result should be constrained.
$out = wp_shrink_dimensions( 1024, 768, 500, 600 );
$this->assertSame( array( 500, 375 ), $out );
$out = wp_shrink_dimensions( 300, 800, 500, 600 );
$this->assertSame( array( 225, 600 ), $out );
}
/**
* @expectedDeprecated wp_shrink_dimensions
*/
function test_shrink_dimensions_boundary() {
// One dimension is larger than the constraint, one smaller - result should be constrained.
$out = wp_shrink_dimensions( 1024, 768, 500, 800 );
$this->assertSame( array( 500, 375 ), $out );
$out = wp_shrink_dimensions( 1024, 768, 2000, 700 );
$this->assertSame( array( 933, 700 ), $out );
// Portrait.
$out = wp_shrink_dimensions( 768, 1024, 800, 500 );
$this->assertSame( array( 375, 500 ), $out );
$out = wp_shrink_dimensions( 768, 1024, 2000, 700 );
$this->assertSame( array( 525, 700 ), $out );
}
function test_constrain_size_for_editor_thumb() {
$out = image_constrain_size_for_editor( 600, 400, 'thumb' );
$this->assertSame( array( 150, 100 ), $out );
$out = image_constrain_size_for_editor( 64, 64, 'thumb' );
$this->assertSame( array( 64, 64 ), $out );
}
function test_constrain_size_for_editor_medium() {
// Default max width is 500, no constraint on height.
global $content_width;
$_content_width = $content_width;
$content_width = 0;
update_option( 'medium_size_w', 500 );
update_option( 'medium_size_h', 0 );
$out = image_constrain_size_for_editor( 600, 400, 'medium' );
$this->assertSame( array( 500, 333 ), $out );
$out = image_constrain_size_for_editor( 400, 600, 'medium' );
$this->assertSame( array( 400, 600 ), $out );
$out = image_constrain_size_for_editor( 64, 64, 'medium' );
$this->assertSame( array( 64, 64 ), $out );
// $content_width should be ignored.
$content_width = 350;
$out = image_constrain_size_for_editor( 600, 400, 'medium' );
$this->assertSame( array( 500, 333 ), $out );
$content_width = $_content_width;
}
function test_constrain_size_for_editor_full() {
global $content_width;
$_content_width = $content_width;
$content_width = 400;
$out = image_constrain_size_for_editor( 600, 400, 'full' );
$this->assertSame( array( 600, 400 ), $out );
$out = image_constrain_size_for_editor( 64, 64, 'full' );
$this->assertSame( array( 64, 64 ), $out );
// $content_width default is 500.
$content_width = 0;
$out = image_constrain_size_for_editor( 600, 400, 'full' );
$this->assertSame( array( 600, 400 ), $out );
$out = image_constrain_size_for_editor( 64, 64, 'full' );
$this->assertSame( array( 64, 64 ), $out );
$content_width = $_content_width;
}
}