xref: /PHP-8.3/ext/gd/tests/createfromstring.phpt (revision a375d547)
1--TEST--
2imagecreatefromstring
3--EXTENSIONS--
4gd
5--SKIPIF--
6<?php
7        if (!function_exists('imagepng')) die("skip no imagpng()\n");
8?>
9--FILE--
10<?php
11$dir = __DIR__;
12
13$im = imagecreatetruecolor(5,5);
14imagefill($im, 0,0, 0xffffff);
15imagesetpixel($im, 3,3, 0x0);
16imagepng($im, $dir . '/tc.png');
17
18$im_string = file_get_contents(__DIR__ . '/tc.png');
19$im = imagecreatefromstring($im_string);
20echo 'createfromstring truecolor png: ';
21if (imagecolorat($im, 3,3) != 0x0) {
22    echo 'failed';
23} else {
24    echo 'ok';
25}
26echo "\n";
27unlink($dir . '/tc.png');
28
29
30
31$im = imagecreate(5,5);
32$c1 = imagecolorallocate($im, 255,255,255);
33$c2 = imagecolorallocate($im, 255,0,0);
34imagefill($im, 0,0, $c1);
35imagesetpixel($im, 3,3, $c2);
36imagepng($im, $dir . '/p.png');
37
38$im_string = file_get_contents(__DIR__ . '/p.png');
39$im = imagecreatefromstring($im_string);
40
41echo'createfromstring palette png: ';
42
43$c = imagecolorsforindex($im, imagecolorat($im, 3,3));
44$failed = false;
45if ($c['red'] != 255 || $c['green'] != 0 || $c['blue'] != 0) {
46    echo 'failed';
47} else {
48    echo 'ok';
49}
50echo "\n";
51unlink($dir . '/p.png');
52
53
54//empty string
55try {
56    imagecreatefromstring('');
57} catch (ValueError $exception) {
58    echo $exception->getMessage() . "\n";
59}
60//random string > 12
61$im = imagecreatefromstring(' asdf jklp foo');
62?>
63--EXPECTF--
64createfromstring truecolor png: ok
65createfromstring palette png: ok
66
67Warning: imagecreatefromstring(): Data is not in a recognized format in %s on line %d
68
69Warning: imagecreatefromstring(): Data is not in a recognized format in %s on line %d
70