ImagickEscposImageTest.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. use Mike42\Escpos\ImagickEscposImage;
  3. use Mike42\Escpos\EscposImage;
  4. class ImagickEscposImageTest extends PHPUnit\Framework\TestCase
  5. {
  6. /**
  7. * Imagick tests - Load tiny images and check how they are printed
  8. * These are skipped if you don't have imagick
  9. */
  10. public function testImagickBadFilename()
  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 testImagickEmpty()
  19. {
  20. $this -> loadAndCheckImg(null, 0, 0, "", array());
  21. }
  22. /**
  23. * @medium
  24. */
  25. public function testImagickBlack()
  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 testImagickBlackTransparent()
  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 testImagickBlackWhite()
  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 testImagickBlackWhiteTall()
  53. {
  54. // We're very interested in correct column format chopping here at 8 pixels
  55. $this -> loadAndCheckImg('black_white_tall.png', 2, 16,
  56. "\xc0\xc0\xc0\xc0\xc0\xc0\xc0\xc0\x00\x00\x00\x00\x00\x00\x00\x00", array("\xff\xff", "\x00\x00"));
  57. }
  58. /**
  59. * @medium
  60. */
  61. public function testImagickWhite()
  62. {
  63. foreach (array('png', 'jpg', 'gif') as $format) {
  64. $this -> loadAndCheckImg('canvas_white.' . $format, 1, 1, "\x00", array("\x00"));
  65. }
  66. }
  67. /**
  68. * PDF test - load tiny PDF and check for well-formedness
  69. * These are also skipped if you don't have imagick
  70. * @medium
  71. */
  72. public function testPdfAllPages()
  73. {
  74. $this -> loadAndCheckPdf('doc.pdf', 1, 1, array("\x00", "\x80"), array(array("\x00"), array("\x80")));
  75. }
  76. public function testPdfBadFilename()
  77. {
  78. $this -> expectException(Exception::class);
  79. $this -> loadAndCheckPdf('not a real file', 1, 1, array(), array());
  80. }
  81. /**
  82. * Load an EscposImage and run a check.
  83. */
  84. private function loadAndCheckImg($fn, $width, $height, $rasterFormat = null, $columnFormat = null)
  85. {
  86. if (!EscposImage::isImagickLoaded()) {
  87. $this -> markTestSkipped("imagick plugin is required for this test");
  88. }
  89. $onDisk = ($fn === null ? null : (dirname(__FILE__) . "/resources/$fn"));
  90. // With optimisations
  91. $imgOptimised = new ImagickEscposImage($onDisk, true);
  92. $this -> checkImg($imgOptimised, $width, $height, $rasterFormat, $columnFormat);
  93. // ... and without
  94. $imgUnoptimised = new ImagickEscposImage($onDisk, false);
  95. $this -> checkImg($imgUnoptimised, $width, $height, $rasterFormat, $columnFormat);
  96. }
  97. /**
  98. * Same as above, loading document and checking pages against some expected values.
  99. */
  100. private function loadAndCheckPdf($fn, $width, $height, array $rasterFormat = null, array $columnFormat = null)
  101. {
  102. if (!EscposImage::isImagickLoaded()) {
  103. $this -> markTestSkipped("imagick plugin required for this test");
  104. }
  105. $pdfPages = ImagickEscposImage::loadPdf(dirname(__FILE__) . "/resources/$fn", $width);
  106. $this -> assertTrue(count($pdfPages) == count($rasterFormat), "Got back wrong number of pages");
  107. foreach ($pdfPages as $id => $img) {
  108. $this -> checkImg($img, $width, $height, $rasterFormat[$id], $columnFormat[$id]);
  109. }
  110. }
  111. /**
  112. * Check image against known width, height, output.
  113. */
  114. private function checkImg(EscposImage $img, $width, $height, $rasterFormatExpected = null, $columnFormatExpected = null)
  115. {
  116. $rasterFormatActual = $img -> toRasterFormat();
  117. $columnFormatActual = $img -> toColumnFormat();
  118. if ($rasterFormatExpected === null) {
  119. echo "\nImage was: " . $img -> getWidth() . "x" . $img -> getHeight() . ", raster data \"" . friendlyBinary($rasterFormatActual) . "\"";
  120. }
  121. if ($columnFormatExpected === null) {
  122. echo "\nImage was: " . $img -> getWidth() . "x" . $img -> getHeight() . ", column data \"" . friendlyBinary($columnFormatActual) . "\"";
  123. }
  124. $this -> assertEquals($height , $img -> getHeight());
  125. $this -> assertEquals($width, $img -> getWidth());
  126. $this -> assertEquals($rasterFormatExpected, $rasterFormatActual, "Raster format did not match expected");
  127. $this -> assertEquals($columnFormatExpected, $columnFormatActual, "Column format did not match expected");
  128. }
  129. }