forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdimensions.php
More file actions
185 lines (153 loc) · 7.54 KB
/
dimensions.php
File metadata and controls
185 lines (153 loc) · 7.54 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
<?php
/**
* @group image
* @group media
* @group upload
*/
class Tests_Image_Dimensions extends WP_UnitTestCase {
function test_400x400_no_crop() {
// Landscape: resize 640x480 to fit 400x400: 400x300.
$out = image_resize_dimensions( 640, 480, 400, 400, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 400, 300, 640, 480 ), $out );
// Portrait: resize 480x640 to fit 400x400: 300x400.
$out = image_resize_dimensions( 480, 640, 400, 400, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 300, 400, 480, 640 ), $out );
}
function test_400x0_no_crop() {
// Landscape: resize 640x480 to fit 400w: 400x300.
$out = image_resize_dimensions( 640, 480, 400, 0, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 400, 300, 640, 480 ), $out );
// Portrait: resize 480x640 to fit 400w: 400x533.
$out = image_resize_dimensions( 480, 640, 400, 0, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 400, 533, 480, 640 ), $out );
}
function test_0x400_no_crop() {
// Landscape: resize 640x480 to fit 400h: 533x400.
$out = image_resize_dimensions( 640, 480, 0, 400, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 533, 400, 640, 480 ), $out );
// Portrait: resize 480x640 to fit 400h: 300x400.
$out = image_resize_dimensions( 480, 640, 0, 400, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 300, 400, 480, 640 ), $out );
}
function test_800x800_no_crop() {
// Landscape: resize 640x480 to fit 800x800.
$out = image_resize_dimensions( 640, 480, 800, 800, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertFalse( $out );
// Portrait: resize 480x640 to fit 800x800.
$out = image_resize_dimensions( 480, 640, 800, 800, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertFalse( $out );
}
function test_800x0_no_crop() {
// Landscape: resize 640x480 to fit 800w.
$out = image_resize_dimensions( 640, 480, 800, 0, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertFalse( $out );
// Portrait: resize 480x640 to fit 800w.
$out = image_resize_dimensions( 480, 640, 800, 0, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertFalse( $out );
}
function test_0x800_no_crop() {
// Landscape: resize 640x480 to fit 800h.
$out = image_resize_dimensions( 640, 480, 0, 800, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertFalse( $out );
// Portrait: resize 480x640 to fit 800h.
$out = image_resize_dimensions( 480, 640, 0, 800, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertFalse( $out );
}
// Cropped versions.
function test_400x400_crop() {
// Landscape: crop 640x480 to fit 400x400: 400x400 taken from a 480x480 crop at (80. 0).
$out = image_resize_dimensions( 640, 480, 400, 400, true );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 80, 0, 400, 400, 480, 480 ), $out );
// Portrait: resize 480x640 to fit 400x400: 400x400 taken from a 480x480 crop at (0. 80).
$out = image_resize_dimensions( 480, 640, 400, 400, true );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 80, 400, 400, 480, 480 ), $out );
}
function test_400x0_crop() {
// Landscape: resize 640x480 to fit 400w: 400x300.
$out = image_resize_dimensions( 640, 480, 400, 0, true );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 400, 300, 640, 480 ), $out );
// Portrait: resize 480x640 to fit 400w: 400x533.
$out = image_resize_dimensions( 480, 640, 400, 0, true );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 400, 533, 480, 640 ), $out );
}
function test_0x400_crop() {
// Landscape: resize 640x480 to fit 400h: 533x400.
$out = image_resize_dimensions( 640, 480, 0, 400, true );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 533, 400, 640, 480 ), $out );
// Portrait: resize 480x640 to fit 400h: 300x400.
$out = image_resize_dimensions( 480, 640, 0, 400, true );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 300, 400, 480, 640 ), $out );
}
function test_400x500_crop() {
// Landscape: crop 640x480 to fit 400x500: 400x400 taken from a 480x480 crop at (80. 0).
$out = image_resize_dimensions( 640, 480, 400, 500, true );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 120, 0, 400, 480, 400, 480 ), $out );
// Portrait: resize 480x640 to fit 400x400: 400x400 taken from a 480x480 crop at (0. 80).
$out = image_resize_dimensions( 480, 640, 400, 500, true );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 20, 400, 500, 480, 600 ), $out );
}
function test_640x480() {
// Crop 640x480 to fit 640x480 (no change).
$out = image_resize_dimensions( 640, 480, 640, 480, true );
$this->assertFalse( $out );
// Resize 640x480 to fit 640x480 (no change).
$out = image_resize_dimensions( 640, 480, 640, 480, false );
$this->assertFalse( $out );
// Test with the filter override.
add_filter( 'wp_image_resize_identical_dimensions', '__return_true' );
// Crop 640x480 to fit 640x480 (no change).
$out = image_resize_dimensions( 640, 480, 640, 480, true );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 640, 480, 640, 480 ), $out );
// Resize 640x480 to fit 640x480 (no change).
$out = image_resize_dimensions( 640, 480, 640, 480, false );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 640, 480, 640, 480 ), $out );
remove_filter( 'wp_image_resize_identical_dimensions', '__return_true' );
}
/**
* @ticket 19393
*/
function test_crop_anchors() {
// Landscape: crop 640x480 to fit 400x500: 400x400 taken from a 480x480 crop.
// src_x = 0 (left), src_y = 0 (top).
$out = image_resize_dimensions( 640, 480, 400, 500, array( 'left', 'top' ) );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 400, 480, 400, 480 ), $out );
// Portrait: resize 480x640 to fit 400x400: 400x400 taken from a 480x480 crop.
// src_x = 0 (left), src_y = 0 (top).
$out = image_resize_dimensions( 480, 640, 400, 500, array( 'left', 'top' ) );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 0, 400, 500, 480, 600 ), $out );
// Landscape: crop 640x480 to fit 400x500: 400x400 taken from a 480x480 crop.
// src_x = 240 (left), src_y = 0 (due to landscape crop).
$out = image_resize_dimensions( 640, 480, 400, 500, array( 'right', 'bottom' ) );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 240, 0, 400, 480, 400, 480 ), $out );
// Portrait: resize 480x640 to fit 400x400: 400x400 taken from a 480x480 crop.
// src_x = 0 (due to portrait crop), src_y = 40 (bottom).
$out = image_resize_dimensions( 480, 640, 400, 500, array( 'right', 'bottom' ) );
// dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h.
$this->assertSame( array( 0, 0, 0, 40, 400, 500, 480, 600 ), $out );
}
}