GdEscposImageTest.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. use Mike42\Escpos\GdEscposImage;
  3. use Mike42\Escpos\EscposImage;
  4. class GdEscposImageTest extends PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * Gd tests - Load tiny images and check how they are printed
  8. * These are skipped if you don't have imagick
  9. */
  10. public function testGdBadFilename()
  11. {
  12. $this -> expectException(Exception::class);
  13. $this -> loadAndCheckImg('not a real file.png', 1, 1, null, null);
  14. }
  15. /**
  16. * @medium
  17. */
  18. public function testGdEmpty()
  19. {
  20. $this -> loadAndCheckImg(null, 0, 0, "", array());
  21. }
  22. /**
  23. * @medium
  24. */
  25. public function testGdBlack()
  26. {
  27. foreach (array('png', 'jpg', 'gif') as $format) {
  28. $this -> loadAndCheckImg('canvas_black.' . $format, 1, 1, "\x80", array("\x80"));
  29. }
  30. }
  31. /**
  32. * @medium
  33. */
  34. public function testGdBlackTransparent()
  35. {
  36. foreach (array('png', 'gif') as $format) {
  37. $this -> loadAndCheckImg('black_transparent.' . $format, 2, 2, "\xc0\x00", array("\x80\x80"));
  38. }
  39. }
  40. /**
  41. * @medium
  42. */
  43. public function testGdBlackWhite()
  44. {
  45. foreach (array('png', 'jpg', 'gif') as $format) {
  46. $this -> loadAndCheckImg('black_white.' . $format, 2, 2, "\xc0\x00", array("\x80\x80"));
  47. }
  48. }
  49. /**
  50. * @medium
  51. */
  52. public function testGdWhite()
  53. {
  54. foreach (array('png', 'jpg', 'gif') as $format) {
  55. $this -> loadAndCheckImg('canvas_white.' . $format, 1, 1, "\x00", array("\x00"));
  56. }
  57. }
  58. /**
  59. * Load an EscposImage with (optionally) certain libraries disabled and run a check.
  60. */
  61. private function loadAndCheckImg($fn, $width, $height, $rasterFormat = null, $columnFormat = null)
  62. {
  63. if (!EscposImage::isGdLoaded()) {
  64. $this -> markTestSkipped("imagick plugin is required for this test");
  65. }
  66. $onDisk = ($fn === null ? null : (dirname(__FILE__) . "/resources/$fn"));
  67. // With optimisations
  68. $imgOptimised = new GdEscposImage($onDisk, true);
  69. $this -> checkImg($imgOptimised, $width, $height, $rasterFormat, $columnFormat);
  70. // ... and without
  71. $imgUnoptimised = new GdEscposImage($onDisk, false);
  72. $this -> checkImg($imgUnoptimised, $width, $height, $rasterFormat, $columnFormat);
  73. }
  74. /**
  75. * Check image against known width, height, output.
  76. */
  77. private function checkImg(EscposImage $img, $width, $height, $rasterFormatExpected = null, $columnFormatExpected = null)
  78. {
  79. $rasterFormatActual = $img -> toRasterFormat();
  80. $columnFormatActual = $img -> toColumnFormat();
  81. if ($rasterFormatExpected === null) {
  82. echo "\nImage was: " . $img -> getWidth() . "x" . $img -> getHeight() . ", raster data \"" . friendlyBinary($rasterFormatActual) . "\"";
  83. }
  84. if ($columnFormatExpected === null) {
  85. echo "\nImage was: " . $img -> getWidth() . "x" . $img -> getHeight() . ", column data \"" . friendlyBinary($columnFormatActual) . "\"";
  86. }
  87. $this -> assertTrue($img -> getHeight() == $height);
  88. $this -> assertTrue($img -> getWidth() == $width);
  89. $this -> assertTrue($rasterFormatExpected === $rasterFormatActual);
  90. $this -> assertTrue($columnFormatExpected === $columnFormatActual);
  91. }
  92. }