Visitar URL original
Merge pull request #19820 from LuckyFellow/feature/gd2-detection · phpmyadmin/phpmyadmin@548c5f3 · GitHub
Skip to content

Commit 548c5f3

Browse files
Merge pull request #19820 from LuckyFellow/feature/gd2-detection
Refactor GD2 availability check to use GD_MAJOR_VERSION
2 parents 38fb49c + cd4ddfe commit 548c5f3

2 files changed

Lines changed: 14 additions & 49 deletions

File tree

‎src/Config.php‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
use function fopen;
3030
use function fread;
3131
use function function_exists;
32-
use function gd_info;
32+
use function get_defined_constants;
3333
use function implode;
3434
use function ini_get;
3535
use function is_array;
@@ -48,7 +48,6 @@
4848
use function rtrim;
4949
use function setcookie;
5050
use function sprintf;
51-
use function str_contains;
5251
use function str_ends_with;
5352
use function stripos;
5453
use function strtolower;
@@ -153,6 +152,12 @@ public function loadFromFile(string|null $source = null): void
153152
$this->baseSettings = $this->settings;
154153
}
155154

155+
/**
156+
* Determines if GD2+ is available.
157+
*
158+
* Respects the config override ('yes' / 'no') if set,
159+
* otherwise checks the `GD_MAJOR_VERSION` constant (>= 2).
160+
*/
156161
public function isGd2Available(): bool
157162
{
158163
if ($this->config->GD2Available === 'yes') {
@@ -163,11 +168,7 @@ public function isGd2Available(): bool
163168
return false;
164169
}
165170

166-
if (! function_exists('imagecreatetruecolor')) {
167-
return false;
168-
}
169-
170-
return function_exists('gd_info') && str_contains(gd_info()['GD Version'], '2.');
171+
return (get_defined_constants()['GD_MAJOR_VERSION'] ?? 0) >= 2;
171172
}
172173

173174
public function isWindows(): bool

‎tests/unit/ConfigTest.php‎

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,9 @@
1717
use ReflectionProperty;
1818

1919
use function file_put_contents;
20-
use function function_exists;
21-
use function gd_info;
20+
use function get_defined_constants;
2221
use function md5;
23-
use function ob_get_clean;
24-
use function ob_start;
25-
use function phpinfo;
26-
use function preg_match;
2722
use function realpath;
28-
use function str_contains;
29-
use function strip_tags;
3023
use function stristr;
3124
use function sys_get_temp_dir;
3225
use function tempnam;
@@ -35,7 +28,6 @@
3528
use const CHANGELOG_FILE;
3629
use const CONFIG_FILE;
3730
use const DIRECTORY_SEPARATOR;
38-
use const INFO_MODULES;
3931
use const PHP_OS;
4032

4133
#[CoversClass(Config::class)]
@@ -123,7 +115,7 @@ public function testLoadConfigs(): void
123115
}
124116

125117
/**
126-
* test for CheckGd2
118+
* test for isGd2Available
127119
*/
128120
public function testCheckGd2(): void
129121
{
@@ -134,38 +126,10 @@ public function testCheckGd2(): void
134126
self::assertFalse($this->object->isGd2Available());
135127

136128
$this->object->set('GD2Available', 'auto');
137-
138-
if (! function_exists('imagecreatetruecolor')) {
139-
self::assertFalse(
140-
$this->object->isGd2Available(),
141-
'imagecreatetruecolor does not exist, isGd2 should be false',
142-
);
143-
}
144-
145-
if (function_exists('gd_info')) {
146-
$gdNfo = gd_info();
147-
if (str_contains($gdNfo['GD Version'], '2.')) {
148-
self::assertTrue($this->object->isGd2Available(), 'GD Version >= 2, isGd2 should be true');
149-
} else {
150-
self::assertFalse($this->object->isGd2Available(), 'GD Version < 2, isGd2 should be false');
151-
}
152-
}
153-
154-
/* Get GD version string from phpinfo output */
155-
ob_start();
156-
phpinfo(INFO_MODULES); /* Only modules */
157-
$a = strip_tags((string) ob_get_clean());
158-
159-
if (preg_match('@GD Version[[:space:]]*\(.*\)@', $a, $v) !== 1) {
160-
return;
161-
}
162-
163-
// TODO: The variable $v clearly is incorrect. Was this meant to be $a?
164-
if (str_contains($v, '2.')) {
165-
self::assertTrue($this->object->isGd2Available(), 'isGd2 should be true');
166-
} else {
167-
self::assertFalse($this->object->isGd2Available(), 'isGd2 should be false');
168-
}
129+
self::assertSame(
130+
(get_defined_constants()['GD_MAJOR_VERSION'] ?? 0) >= 2,
131+
$this->object->isGd2Available(),
132+
);
169133
}
170134

171135
/**

0 commit comments

Comments
 (0)