PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.2.5
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.2.5
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / rosell-dk / webp-convert / tests / Convert / Converters / GdTest.php

GdTest.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.2.5, at vendor/rosell-dk/webp-convert/tests/Convert/Converters/GdTest.php

287 lines 9.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WebPConvert\Tests\Convert\Converters;
3
4
5 use WebPConvert\Tests\Convert\Exposers\GdExposer;
6 use WebPConvert\Convert\Converters\Gd;
7 use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException;
8 use WebPConvert\Convert\Exceptions\ConversionFailedException;
9
10 use PHPUnit\Framework\TestCase;
11
12 class GdTest extends TestCase
13 {
14
15
16 public static function getImageFolder()
17 {
18 return realpath(__DIR__ . '/../../images');
19 }
20
21 public static function getImagePath($image)
22 {
23 return self::getImageFolder() . '/' . $image;
24 }
25
26 public function testConvert()
27 {
28 ConverterTestHelper::runAllConvertTests($this, 'Gd');
29 }
30
31 private function createGd($src)
32 {
33 $source = self::getImagePath($src);
34 $this->assertTrue(file_exists($source), 'source does not exist:' . $source);
35
36 return new Gd($source, $source . '.webp');
37 }
38
39 private function createGdExposer($src)
40 {
41 $gd = $this->createGd($src);
42 return new GdExposer($gd);
43 }
44
45 private static function resetPretending()
46 {
47 reset_pretending();
48 }
49
50 // pretend imagewebp is missing
51 public function testNotOperational1()
52 {
53 global $pretend;
54
55 $gd = $this->createGd('test.png');
56 self::resetPretending();
57
58 $pretend['functionsNotExisting'] = ['imagewebp'];
59 $this->expectException(SystemRequirementsNotMetException::class);
60 $gd->checkOperationality();
61 }
62
63
64 // pretend gd is not loaded
65 public function testNotOperational2()
66 {
67 global $pretend;
68
69 $gd = $this->createGd('test.png');
70 self::resetPretending();
71
72 $pretend['extensionsNotExisting'] = ['gd'];
73 $this->expectException(SystemRequirementsNotMetException::class);
74 $gd->checkOperationality();
75 $pretend['extensionsNotExisting'] = [];
76 }
77
78 // pretend imagecreatefrompng is missing
79 public function testCheckConvertability1()
80 {
81 global $pretend;
82
83 $gd = $this->createGd('test.png');
84 self::resetPretending();
85
86 $pretend['functionsNotExisting'] = ['imagecreatefrompng'];
87 $this->expectException(SystemRequirementsNotMetException::class);
88 $gd->checkConvertability();
89 $pretend['functionsNotExisting'] = [];
90 }
91
92 // pretend imagecreatefrompng is working
93 public function testCheckConvertability2()
94 {
95 global $pretend;
96
97 $gd = $this->createGd('test.png');
98 self::resetPretending();
99
100 $pretend['functionsExisting'] = ['imagecreatefrompng'];
101 $gd->checkConvertability();
102 $pretend['functionsExisting'] = [];
103 }
104
105 // pretend imagecreatefromjpeg is missing
106 public function testCheckConvertability3()
107 {
108 global $pretend;
109
110 $gd = $this->createGd('test.jpg');
111 self::resetPretending();
112
113 $pretend['functionsNotExisting'] = ['imagecreatefromjpeg'];
114 $this->expectException(SystemRequirementsNotMetException::class);
115 $gd->checkConvertability();
116 $pretend['functionsNotExisting'] = [];
117 }
118
119 public function testSource()
120 {
121
122 $source = self::getImagePath('test.png');
123 $gd = new Gd($source, $source . '.webp');
124
125 self::resetPretending();
126
127 $gdExposer = new GdExposer($gd);
128
129 $this->assertEquals($source, $gdExposer->getSource());
130 $this->assertTrue(file_exists($source), 'source does not exist');
131 }
132
133 public function testCreateImageResource()
134 {
135 $gd = $this->createGd('test.png');
136 self::resetPretending();
137
138 $gdExposer = new GdExposer($gd);
139
140 if (!$gdExposer->isOperating()) {
141 //$this->assertTrue(false);
142 return;
143 }
144
145 // It is operating and image should be ok.
146 // - so it should be able to create image resource (or, for PHP 8, an \GdImage object)
147 $image = $gdExposer->createImageResource();
148 $isResourceOrObject = ((gettype($image) == 'resource') || (gettype($image) == 'object'));
149 $this->assertTrue($isResourceOrObject, 'Expected createImageResource to return a resource or an object but got:' . gettype($image));
150
151 /*
152 // Try the workaround method.
153 $result = $gdExposer->makeTrueColorUsingWorkaround($image);
154
155 // As the workaround is pretty sturdy, let us assert that it simply works.
156 // It would be good to find out if it doesn't, anyway!
157 $this->assertTrue($result); */
158
159 //$gdExposer->tryToMakeTrueColorIfNot($image);
160 $this->assertTrue(imageistruecolor($image), 'image is not true color');
161
162 $result = $gdExposer->trySettingAlphaBlending($image);
163 $this->assertTrue($result, 'failed setting alpha blending');
164 }
165
166 public function testStuffOnNotTrueColor()
167 {
168 $gd = $this->createGd('not-true-color.png');
169 self::resetPretending();
170
171 $gdExposer = new GdExposer($gd);
172
173 if (!$gdExposer->isOperating()) {
174 return;
175 }
176
177 // It is operating and image should be ok.
178 // - so it should be able to create image resource
179 $image = $gdExposer->createImageResource();
180 $isResourceOrObject = ((gettype($image) == 'resource') || (gettype($image) == 'object'));
181 $this->assertTrue($isResourceOrObject, 'Expected createImageResource to return a resource or an object but got:' . gettype($image));
182
183 $this->assertFalse(imageistruecolor($image), 'image is already true color');
184 $gdExposer->tryToMakeTrueColorIfNot($image);
185 $this->assertTrue(imageistruecolor($image), 'image is not true color after trying to make it');
186 $result = $gdExposer->trySettingAlphaBlending($image);
187 $this->assertTrue($result, 'failed setting alpha blending');
188
189 // Test the workaround method.
190 $gd = $this->createGd('not-true-color.png');
191 $gdExposer = new GdExposer($gd);
192 $image = $gdExposer->createImageResource();
193 $this->assertFalse(imageistruecolor($image), 'image is already true color');
194
195 //$image = imagecreatetruecolor(imagesx($image), imagesy($image));
196 $result = $gdExposer->makeTrueColorUsingWorkaround($image);
197 //$result = $gd->makeTrueColorUsingWorkaround($image);
198 $this->assertTrue($result);
199 $this->assertTrue(imageistruecolor($image), 'image is not true color after trying to make it (with workaround method)');
200 $result = $gdExposer->trySettingAlphaBlending($image);
201 $this->assertTrue($result, 'failed setting alpha blending');
202 }
203
204 public function testConvertFailure()
205 {
206 echo 'OS: ' . PHP_OS;
207 $gdExposer = $this->createGdExposer('not-true-color.png');
208
209 self::resetPretending();
210
211 // The next requires imagewebp...
212 if (!function_exists('imagewebp')) {
213 return;
214 }
215
216 $image = $gdExposer->createImageResource();
217
218 // This image is not true color.
219 // Trying to convert it fails (empty string is generated)
220 // Assert that I am right!
221
222 // In most cases the following triggers a warning:
223 // Warning: imagewebp(): Palette image not supported by webp in /var/www/wc/wc0/webp-convert/tests/Convert/Converters/GdTest.php on line 215
224 //
225 // However, in Windows-2022 (PHP 8), it throws A FATAL!
226 // Error: PHP Fatal error: Paletter image not supported by webp in D:\a\webp-convert\webp-convert\tests\Convert\Converters\GdTest.php on line 222
227 //
228 // And its worse and Mac (PHP 7.1 and 7.3, not 7.4 and 8.0)
229 // It just halts execution - see ##322
230 //
231 // In Ubuntu, PHP 8.1 it throws fatal too. I think this behaviour starts from PHP 8.0.0.
232
233 $isWindows = preg_match('/^win/i', PHP_OS);
234 $isMacDarwin = preg_match('/^darwin/i', PHP_OS);
235 $isPHP8orAbove = (version_compare(PHP_VERSION, '8.0.0') >= 0);
236
237 if (!$isWindows && !$isMacDarwin && !$isPHP8orAbove) {
238 ob_start();
239
240 try {
241 @imagewebp($image, null, 80);
242 } catch (\Exception $e) {
243 } catch (\Throwable $e) {
244 }
245 $output = ob_get_clean();
246
247 // The failure results in no output:
248 $this->assertEquals($output, '');
249
250 // similary, we should get an exception when calling tryConverting ('Gd failed: imagewebp() returned empty string')
251 //$this->expectException(ConversionFailedException::class);
252 $gotExpectedException = false;
253 try {
254 $gdExposer->tryConverting($image);
255 } catch (ConversionFailedException $e) {
256 $gotExpectedException = true;
257 }
258 $this->assertTrue(
259 $gotExpectedException,
260 'did not get expected exception when converting palette image with Gd, ' .
261 'bypassing the code that converts to true color'
262 );
263
264 }
265
266 //$gdExposer->tryToMakeTrueColorIfNot($image);
267
268 //$pretend['functionsNotExisting'] = ['imagewebp'];
269
270 }
271 /*
272 public function testMakeTrueColorUsingWorkaround()
273 {
274 $gd = $this->createGd('test.png');
275 self::resetPretending();
276
277 $gdExposer = new GdExposer($gd);
278
279 if (!$gdExposer->isOperating()) {
280 return;
281 }
282
283 }*/
284 }
285
286 require_once('pretend.inc');
287