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