1
+ <?php
2
+
3
+ include 'src/ImageResize.php ' ;
4
+
5
+ use \Eventviva \ImageResize ;
6
+
7
+ class ImageResizeTest extends PHPUnit_Framework_TestCase
8
+ {
9
+
10
+ private $ image_types = array (
11
+ 'gif ' ,
12
+ 'jpeg ' ,
13
+ 'png '
14
+ );
15
+
16
+ private $ unsupported_image = 'Qk08AAAAAAAAADYAAAAoAAAAAQAAAAEAAAABABAAAAAAAAYAAAASCwAAEgsAAAAAAAAAAAAA/38AAAAA ' ;
17
+
18
+ public function testLoadGif () {
19
+ $ image = $ this ->createImage (1 , 1 , 'gif ' );
20
+ $ resize = new ImageResize ($ image );
21
+
22
+ $ this ->assertEquals (IMAGETYPE_GIF , $ resize ->source_type );
23
+ }
24
+
25
+ public function testLoadJpg () {
26
+ $ image = $ this ->createImage (1 , 1 , 'jpeg ' );
27
+ $ resize = new ImageResize ($ image );
28
+
29
+ $ this ->assertEquals (IMAGETYPE_JPEG , $ resize ->source_type );
30
+ }
31
+
32
+ public function testLoadPng () {
33
+ $ image = $ this ->createImage (1 , 1 , 'png ' );
34
+ $ resize = new ImageResize ($ image );
35
+
36
+ $ this ->assertEquals (IMAGETYPE_PNG , $ resize ->source_type );
37
+ }
38
+
39
+ /**
40
+ * @expectedException Exception
41
+ * @expectedExceptionMessage getimagesize(): Filename cannot be empty
42
+ */
43
+ public function testLoadNoFile () {
44
+ new ImageResize (null );
45
+ }
46
+
47
+ /**
48
+ * @expectedException Exception
49
+ * @expectedExceptionMessage Could not read
50
+ */
51
+ public function testLoadUnsupportedFile () {
52
+ new ImageResize (__FILE__ );
53
+ }
54
+
55
+ /**
56
+ * @expectedException Exception
57
+ * @expectedExceptionMessage Unsupported image type
58
+ */
59
+ public function testLoadUnsupportedImage () {
60
+ $ filename = $ this ->getTempFile ();
61
+
62
+ $ image = fopen ($ filename , 'w ' );
63
+ fwrite ($ image , base64_decode ($ this ->unsupported_image ));
64
+ fclose ($ image );
65
+
66
+ new ImageResize ($ filename );
67
+ }
68
+
69
+ public function testResizeToHeight () {
70
+ $ image = $ this ->createImage (200 , 100 , 'png ' );
71
+ $ resize = new ImageResize ($ image );
72
+
73
+ $ resize ->resizeToHeight (50 );
74
+
75
+ $ this ->assertEquals (100 , $ resize ->getDestWidth ());
76
+ $ this ->assertEquals (50 , $ resize ->getDestHeight ());
77
+ }
78
+
79
+ public function testResizeToWidth () {
80
+ $ image = $ this ->createImage (200 , 100 , 'png ' );
81
+ $ resize = new ImageResize ($ image );
82
+
83
+ $ resize ->resizeToWidth (100 );
84
+
85
+ $ this ->assertEquals (100 , $ resize ->getDestWidth ());
86
+ $ this ->assertEquals (50 , $ resize ->getDestHeight ());
87
+ }
88
+
89
+ public function testScale () {
90
+ $ image = $ this ->createImage (200 , 100 , 'png ' );
91
+ $ resize = new ImageResize ($ image );
92
+
93
+ $ resize ->scale (50 );
94
+
95
+ $ this ->assertEquals (100 , $ resize ->getDestWidth ());
96
+ $ this ->assertEquals (50 , $ resize ->getDestHeight ());
97
+ }
98
+
99
+ public function testResize () {
100
+ $ image = $ this ->createImage (200 , 100 , 'png ' );
101
+ $ resize = new ImageResize ($ image );
102
+
103
+ $ resize ->resize (50 , 50 );
104
+
105
+ $ this ->assertEquals (50 , $ resize ->getDestWidth ());
106
+ $ this ->assertEquals (50 , $ resize ->getDestHeight ());
107
+ }
108
+
109
+ public function testResizeLargerNotAllowed () {
110
+ $ image = $ this ->createImage (200 , 100 , 'png ' );
111
+ $ resize = new ImageResize ($ image );
112
+
113
+ $ resize ->resize (400 , 200 );
114
+
115
+ $ this ->assertEquals (200 , $ resize ->getDestWidth ());
116
+ $ this ->assertEquals (100 , $ resize ->getDestHeight ());
117
+ }
118
+
119
+ public function testCrop () {
120
+ $ image = $ this ->createImage (200 , 100 , 'png ' );
121
+ $ resize = new ImageResize ($ image );
122
+
123
+ $ resize ->crop (50 , 50 );
124
+
125
+ $ this ->assertEquals (50 , $ resize ->getDestWidth ());
126
+ $ this ->assertEquals (50 , $ resize ->getDestHeight ());
127
+ }
128
+
129
+ public function testCropLargerNotAllowed () {
130
+ $ image = $ this ->createImage (200 , 100 , 'png ' );
131
+ $ resize = new ImageResize ($ image );
132
+
133
+ $ resize ->crop (500 , 500 );
134
+
135
+ $ this ->assertEquals (200 , $ resize ->getDestWidth ());
136
+ $ this ->assertEquals (100 , $ resize ->getDestHeight ());
137
+ }
138
+
139
+ public function testSaveGif () {
140
+ $ image = $ this ->createImage (200 , 100 , 'gif ' );
141
+
142
+ $ resize = new ImageResize ($ image );
143
+
144
+ $ filename = $ this ->getTempFile ();
145
+
146
+ $ resize ->save ($ filename );
147
+
148
+ $ this ->assertEquals (IMAGETYPE_GIF , exif_imagetype ($ filename ));
149
+ }
150
+
151
+ public function testSaveJpg () {
152
+ $ image = $ this ->createImage (200 , 100 , 'jpeg ' );
153
+
154
+ $ resize = new ImageResize ($ image );
155
+
156
+ $ filename = $ this ->getTempFile ();
157
+
158
+ $ resize ->save ($ filename );
159
+
160
+ $ this ->assertEquals (IMAGETYPE_JPEG , exif_imagetype ($ filename ));
161
+ }
162
+
163
+ public function testSavePng () {
164
+ $ image = $ this ->createImage (200 , 100 , 'png ' );
165
+
166
+ $ resize = new ImageResize ($ image );
167
+
168
+ $ filename = $ this ->getTempFile ();
169
+
170
+ $ resize ->save ($ filename );
171
+
172
+ $ this ->assertEquals (IMAGETYPE_PNG , exif_imagetype ($ filename ));
173
+ }
174
+
175
+ public function testSaveChmod () {
176
+ $ image = $ this ->createImage (200 , 100 , 'png ' );
177
+
178
+ $ resize = new ImageResize ($ image );
179
+
180
+ $ filename = $ this ->getTempFile ();
181
+
182
+ $ resize ->save ($ filename , null , null , 0600 );
183
+
184
+ $ this ->assertEquals (600 , substr (decoct (fileperms ($ filename )), 3 ));
185
+ }
186
+
187
+ public function testOutputGif () {
188
+ $ image = $ this ->createImage (200 , 100 , 'gif ' );
189
+
190
+ $ resize = new ImageResize ($ image );
191
+
192
+ ob_start ();
193
+
194
+ // supressing header errors
195
+ @$ resize ->output ();
196
+
197
+ $ image_contents = ob_get_clean ();
198
+
199
+ $ info = finfo_open ();
200
+
201
+ $ type = finfo_buffer ($ info , $ image_contents , FILEINFO_MIME_TYPE );
202
+
203
+ $ this ->assertEquals ('image/gif ' , $ type );
204
+ }
205
+
206
+ public function testOutputJpg () {
207
+ $ image = $ this ->createImage (200 , 100 , 'jpeg ' );
208
+
209
+ $ resize = new ImageResize ($ image );
210
+
211
+ ob_start ();
212
+
213
+ // supressing header errors
214
+ @$ resize ->output ();
215
+
216
+ $ image_contents = ob_get_clean ();
217
+
218
+ $ info = finfo_open ();
219
+
220
+ $ type = finfo_buffer ($ info , $ image_contents , FILEINFO_MIME_TYPE );
221
+
222
+ $ this ->assertEquals ('image/jpeg ' , $ type );
223
+ }
224
+
225
+ public function testOutputPng () {
226
+ $ image = $ this ->createImage (200 , 100 , 'png ' );
227
+
228
+ $ resize = new ImageResize ($ image );
229
+
230
+ ob_start ();
231
+
232
+ // supressing header errors
233
+ @$ resize ->output ();
234
+
235
+ $ image_contents = ob_get_clean ();
236
+
237
+ $ info = finfo_open ();
238
+
239
+ $ type = finfo_buffer ($ info , $ image_contents , FILEINFO_MIME_TYPE );
240
+
241
+ $ this ->assertEquals ('image/png ' , $ type );
242
+ }
243
+
244
+ private function createImage ($ width , $ height , $ type ) {
245
+ if (!in_array ($ type , $ this ->image_types )) {
246
+ throw new \Exception ('Unsupported image type ' );
247
+ }
248
+
249
+ $ image = imagecreatetruecolor ($ width , $ height );
250
+
251
+ $ filename = $ this ->getTempFile ();
252
+
253
+ $ output_function = 'image ' . $ type ;
254
+ $ output_function ($ image , $ filename );
255
+
256
+ return $ filename ;
257
+ }
258
+
259
+ private function getTempFile () {
260
+ return tempnam (sys_get_temp_dir (), 'resize_test_image ' );
261
+ }
262
+
263
+ }
264
+
265
+ ?>
0 commit comments