xref: /PHP-7.2/ext/gd/tests/func.inc (revision 902d39a3)
1<?php
2
3function get_gd_version()
4{
5	return GD_VERSION;
6}
7
8function get_php_info()
9{
10	ob_start();
11	phpinfo();
12	$info = ob_get_contents();
13	ob_end_clean();
14
15	return $info;
16}
17
18function get_freetype_version()
19{
20	$version = 0;
21
22	if (preg_match(',FreeType Version => (\d+\.\d+\.\d+),s', get_php_info(), $match)) {
23		$version = $match[1];
24	}
25
26	return $version;
27}
28
29function get_libjpeg_version()
30{
31	$version = 0;
32
33	if (preg_match(',libJPEG Version => ([a-z0-9]+),s', get_php_info(), $match)) {
34		$version = $match[1];
35	}
36
37	return $version;
38}
39
40function get_libpng_version()
41{
42	$version = 0;
43
44	if (preg_match(',libPNG Version => (\d+\.\d+\.\d+),s', get_php_info(), $match)) {
45		$version = $match[1];
46	}
47
48	return $version;
49}
50
51function get_libxpm_version()
52{
53	$version = 0;
54
55	if (preg_match(',libXpm Version => (\d+),s', get_php_info(), $match)) {
56		$version = $match[1];
57	}
58
59	return $version;
60}
61
62/**
63 * Tests that an in-memory image equals a PNG file.
64 *
65 * It checks for equal image sizes, and whether any pixels are different.
66 * The textual result is printed, so the EXPECT section should contain the line
67 * "The images are equal."
68 *
69 * If the PNG file does not exists, or the images are not equal, a diagnostic
70 * message is printed, and the actual file is stored right beside the temporary
71 * .php test file with the extension .out.png, to be able to manually inspect
72 * the result.
73 *
74 * @param string $filename
75 * @param resource $actual
76 * @return void
77 */
78function test_image_equals_file($filename, $actual)
79{
80	if (!file_exists($filename)) {
81		echo "The expected image does not exist.\n";
82		save_actual_image($actual);
83		return;
84	}
85	$actual = test_to_truecolor($actual);
86	$expected = imagecreatefrompng($filename);
87	$expected = test_to_truecolor($expected);
88	$exp_x = imagesx($expected);
89	$exp_y = imagesy($expected);
90	$act_x = imagesx($actual);
91	$act_y = imagesy($actual);
92	if ($exp_x != $act_x || $exp_y != $act_y) {
93		echo "The image size differs: expected {$exp_x}x{$exp_y}, got {$act_x}x{$act_y}.\n";
94		save_actual_image($actual);
95		return;
96	}
97	$pixels_changed = 0;
98	for ($y = 0; $y < $exp_y; $y++) {
99		for ($x = 0; $x < $exp_x; $x ++) {
100			$exp_c = imagecolorat($expected, $x, $y);
101			$act_c = imagecolorat($actual, $x, $y);
102			if ($exp_c != $act_c) {
103				$pixels_changed++;
104			}
105		}
106	}
107	if (!$pixels_changed) {
108		echo "The images are equal.\n";
109	} else {
110		echo "The images differ in {$pixels_changed} pixels.\n";
111		save_actual_image($actual);
112	}
113}
114
115/**
116 * Returns the truecolor version of an image.
117 *
118 * @param resource $image
119 * @return resource
120 */
121function test_to_truecolor($image)
122{
123	if (imageistruecolor($image)) {
124		return $image;
125	} else {
126		$width = imagesx($image);
127		$height = imagesy($image);
128		$result = imagecreatetruecolor($width, $height);
129		imagecopy($result, $image, 0,0, 0,0, $width, $height);
130		return $result;
131	}
132}
133
134/**
135 * Saves an actual image to disk.
136 *
137 * The image is saved right beside the temporary .php test file with the
138 * extension .out.png.
139 *
140 * @param resource $image
141 * @return void
142 */
143function save_actual_image($image)
144{
145	$pathinfo = pathinfo($_SERVER['SCRIPT_FILENAME']);
146	$filename = "{$pathinfo['dirname']}/{$pathinfo['filename']}.out.png";
147	imagepng($image, $filename);
148}
149