xref: /PHP-8.1/ext/gd/tests/gif.phpt (revision a375d547)
1--TEST--
2gif in/out
3--EXTENSIONS--
4gd
5--SKIPIF--
6<?php
7    if (!function_exists("imagegif") || !function_exists("imagecreatefromgif") || !function_exists('imagecreatefromjpeg')) {
8        die("skip gif support unavailable");
9    }
10?>
11--FILE--
12<?php
13
14function check_box($r,$g,$b, $error=0) {
15    $cwd = __DIR__;
16    $im2 = imagecreatefromgif($cwd . '/test_gif.gif');
17
18    $c = imagecolorsforindex($im2, imagecolorat($im2, 8,8));
19
20    if ($error>0) {
21        $r_min = $r - $error; $r_max = $r + $error;
22        $g_min = $g - $error; $g_max = $g + $error;
23        $b_min = $b - $error; $b_max = $b + $error;
24
25        if (
26            ($c['red'] >= $r_min || $c['red'] <= $r_max)
27            &&
28            ($c['green'] >= $g_min || $c['green'] <= $g_max)
29            &&
30            ($c['blue'] >= $b_min || $c['blue'] <= $b_max)
31            ) {
32            return true;
33        } else {
34            return false;
35        }
36    } else {
37        if ($c['red']==$r && $c['green']==$g && $c['blue']==$b) {
38            return true;
39        } else {
40            return false;
41        }
42    }
43}
44$cwd = __DIR__;
45
46$im = imagecreate(10,10);
47$c = imagecolorallocate($im, 255,255,255);
48imagefilledrectangle($im, 5,5, 10,10, $c);
49imagegif($im, $cwd . '/test_gif.gif');
50if (check_box(255,255,255)) {
51    echo "<4 cols: ok\n";
52}
53
54$im = imagecreate(10,10);
55for ($i=0; $i<7; $i++) {
56    $c = imagecolorallocate($im, $i,$i,$i);
57}
58imagefilledrectangle($im, 5,5, 10,10, $c);
59imagegif($im, $cwd . '/test_gif.gif');
60$i--;
61if (check_box($i,$i,$i)) {
62        echo "<8 cols: ok\n";
63}
64
65
66$im = imagecreate(10,10);
67for ($i=0; $i<15; $i++) {
68    $c = imagecolorallocate($im, $i,$i,$i);
69}
70imagefilledrectangle($im, 5,5, 10,10, $c);
71imagegif($im, $cwd . '/test_gif.gif');
72$i--;
73if (check_box($i,$i,$i)) {
74        echo "<16 cols: ok\n";
75}
76
77
78$im = imagecreate(10,10);
79for ($i=0; $i<31; $i++) {
80    $c = imagecolorallocate($im, $i,$i,$i);
81}
82imagefilledrectangle($im, 5,5, 10,10, $c);
83imagegif($im, $cwd . '/test_gif.gif');
84$i--;
85if (check_box($i,$i,$i)) {
86        echo "<32 cols: ok\n";
87}
88
89
90$im = imagecreate(10,10);
91for ($i=0; $i<63; $i++) {
92    $c = imagecolorallocate($im, $i,$i,$i);
93}
94imagefilledrectangle($im, 5,5, 10,10, $c);
95imagegif($im, $cwd . '/test_gif.gif');
96$i--;
97if (check_box($i,$i,$i)) {
98        echo "<64 cols: ok\n";
99}
100
101$im = imagecreate(10,10);
102for ($i=0; $i<127; $i++) {
103    $c = imagecolorallocate($im, $i,$i,$i);
104}
105imagefilledrectangle($im, 5,5, 10,10, $c);
106imagegif($im, $cwd . '/test_gif.gif');
107$i--;
108if (check_box($i,$i,$i)) {
109        echo "<128 cols: ok\n";
110}
111
112$im = imagecreate(10,10);
113for ($i=0; $i<255; $i++) {
114    $c = imagecolorallocate($im, $i,$i,$i);
115}
116imagefilledrectangle($im, 5,5, 10,10, $c);
117imagegif($im, $cwd . '/test_gif.gif');
118$i--;
119if (check_box($i,$i,$i)) {
120        echo "<256 cols: ok\n";
121}
122
123
124$im = imagecreatefromjpeg($cwd . '/conv_test.jpg');
125imagefilledrectangle($im, 5,5, 10,10, 0xffffff);
126imagegif($im, $cwd . '/test_gif.gif');
127imagegif($im, $cwd . '/test_gif_2.gif');
128
129if (check_box(255,255,255, 10)) {
130    echo ">256 cols: ok\n";
131}
132
133@unlink($cwd . "/test_gif.gif");
134@unlink($cwd . "/test_gif_2.gif");
135?>
136--EXPECT--
137<4 cols: ok
138<8 cols: ok
139<16 cols: ok
140<32 cols: ok
141<64 cols: ok
142<128 cols: ok
143<256 cols: ok
144>256 cols: ok
145