createfromstring.phpt 1.5 KB

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