ImagickEscposImageTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 is expected to fail on many distributions with an error, due to GhostScript defaults.
  75. // 'Exception: not authorized `/path/to/doc.pdf' @error/constitute.c/ReadImage/412'
  76. // The defaults were changed to prevent a vulnerability (arbitrary code execution), and can be bypassed if you
  77. // trust the source of PDF files.
  78. // https://stackoverflow.com/a/52661288/1808534
  79. $this -> markTestSkipped('unsupported feature');
  80. $this -> loadAndCheckPdf('doc.pdf', 1, 1, array("\x00", "\x80"), array(array("\x00"), array("\x80")));
  81. }
  82. public function testPdfBadFilename()
  83. {
  84. $this -> expectException(Exception::class);
  85. $this -> loadAndCheckPdf('not a real file', 1, 1, array(), array());
  86. }
  87. /**
  88. * Load an EscposImage and run a check.
  89. */
  90. private function loadAndCheckImg($fn, $width, $height, $rasterFormat = null, $columnFormat = null)
  91. {
  92. if (!EscposImage::isImagickLoaded()) {
  93. $this -> markTestSkipped("imagick plugin is required for this test");
  94. }
  95. $onDisk = ($fn === null ? null : (dirname(__FILE__) . "/resources/$fn"));
  96. // With optimisations
  97. $imgOptimised = new ImagickEscposImage($onDisk, true);
  98. $this -> checkImg($imgOptimised, $width, $height, $rasterFormat, $columnFormat);
  99. // ... and without
  100. $imgUnoptimised = new ImagickEscposImage($onDisk, false);
  101. $this -> checkImg($imgUnoptimised, $width, $height, $rasterFormat, $columnFormat);
  102. }
  103. /**
  104. * Same as above, loading document and checking pages against some expected values.
  105. */
  106. private function loadAndCheckPdf($fn, $width, $height, array $rasterFormat = null, array $columnFormat = null)
  107. {
  108. if (!EscposImage::isImagickLoaded()) {
  109. $this -> markTestSkipped("imagick plugin required for this test");
  110. }
  111. $pdfPages = ImagickEscposImage::loadPdf(dirname(__FILE__) . "/resources/$fn", $width);
  112. $this -> assertTrue(count($pdfPages) == count($rasterFormat), "Got back wrong number of pages");
  113. foreach ($pdfPages as $id => $img) {
  114. $this -> checkImg($img, $width, $height, $rasterFormat[$id], $columnFormat[$id]);
  115. }
  116. }
  117. /**
  118. * Check image against known width, height, output.
  119. */
  120. private function checkImg(EscposImage $img, $width, $height, $rasterFormatExpected = null, $columnFormatExpected = null)
  121. {
  122. $rasterFormatActual = $img -> toRasterFormat();
  123. $columnFormatActual = $img -> toColumnFormat();
  124. if ($rasterFormatExpected === null) {
  125. echo "\nImage was: " . $img -> getWidth() . "x" . $img -> getHeight() . ", raster data \"" . friendlyBinary($rasterFormatActual) . "\"";
  126. }
  127. if ($columnFormatExpected === null) {
  128. echo "\nImage was: " . $img -> getWidth() . "x" . $img -> getHeight() . ", column data \"" . friendlyBinary($columnFormatActual) . "\"";
  129. }
  130. $this -> assertEquals($height , $img -> getHeight());
  131. $this -> assertEquals($width, $img -> getWidth());
  132. $this -> assertEquals($rasterFormatExpected, $rasterFormatActual, "Raster format did not match expected");
  133. $this -> assertEquals($columnFormatExpected, $columnFormatActual, "Column format did not match expected");
  134. }
  135. }