func.inc 3.8 KB

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