qr-code.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /* Demonstration of available options on the qrCode() command */
  3. require __DIR__ . '/../vendor/autoload.php';
  4. use Mike42\Escpos\Printer;
  5. use Mike42\Escpos\PrintConnectors\FilePrintConnector;
  6. $connector = new FilePrintConnector("php://stdout");
  7. $printer = new Printer($connector);
  8. // Most simple example
  9. title($printer, "QR code demo\n");
  10. $testStr = "Testing 123";
  11. $printer -> qrCode($testStr);
  12. $printer -> text("Most simple example\n");
  13. $printer -> feed();
  14. // Demo that alignment is the same as text
  15. $printer -> setJustification(Printer::JUSTIFY_CENTER);
  16. $printer -> qrCode($testStr);
  17. $printer -> text("Same example, centred\n");
  18. $printer -> setJustification();
  19. $printer -> feed();
  20. // Demo of numeric data being packed more densly
  21. title($printer, "Data encoding\n");
  22. $test = array(
  23. "Numeric" => "0123456789012345678901234567890123456789",
  24. "Alphanumeric" => "abcdefghijklmnopqrstuvwxyzabcdefghijklmn",
  25. "Binary" => str_repeat("\0", 40));
  26. foreach ($test as $type => $data) {
  27. $printer -> qrCode($data);
  28. $printer -> text("$type\n");
  29. $printer -> feed();
  30. }
  31. // Demo of error correction
  32. title($printer, "Error correction\n");
  33. $ec = array(
  34. Printer::QR_ECLEVEL_L => "L",
  35. Printer::QR_ECLEVEL_M => "M",
  36. Printer::QR_ECLEVEL_Q => "Q",
  37. Printer::QR_ECLEVEL_H => "H");
  38. foreach ($ec as $level => $name) {
  39. $printer -> qrCode($testStr, $level);
  40. $printer -> text("Error correction $name\n");
  41. $printer -> feed();
  42. }
  43. // Change size
  44. title($printer, "Pixel size\n");
  45. $sizes = array(
  46. 1 => "(minimum)",
  47. 2 => "",
  48. 3 => "(default)",
  49. 4 => "",
  50. 5 => "",
  51. 10 => "",
  52. 16 => "(maximum)");
  53. foreach ($sizes as $size => $label) {
  54. $printer -> qrCode($testStr, Printer::QR_ECLEVEL_L, $size);
  55. $printer -> text("Pixel size $size $label\n");
  56. $printer -> feed();
  57. }
  58. // Change model
  59. title($printer, "QR model\n");
  60. $models = array(
  61. Printer::QR_MODEL_1 => "QR Model 1",
  62. Printer::QR_MODEL_2 => "QR Model 2 (default)",
  63. Printer::QR_MICRO => "Micro QR code\n(not supported on all printers)");
  64. foreach ($models as $model => $name) {
  65. $printer -> qrCode($testStr, Printer::QR_ECLEVEL_L, 3, $model);
  66. $printer -> text("$name\n");
  67. $printer -> feed();
  68. }
  69. // Cut & close
  70. $printer -> cut();
  71. $printer -> close();
  72. function title(Printer $printer, $str)
  73. {
  74. $printer -> selectPrintMode(Printer::MODE_DOUBLE_HEIGHT | Printer::MODE_DOUBLE_WIDTH);
  75. $printer -> text($str);
  76. $printer -> selectPrintMode();
  77. }