qrcode-interactive.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * @created 18.11.2017
  4. * @author Smiley <smiley@chillerlan.net>
  5. * @copyright 2017 Smiley
  6. * @license MIT
  7. *
  8. * @noinspection PhpComposerExtensionStubsInspection
  9. */
  10. use chillerlan\QRCode\{QRCode, QROptions};
  11. use chillerlan\QRCode\Data\QRMatrix;
  12. require_once '../vendor/autoload.php';
  13. try{
  14. $moduleValues = [
  15. // finder
  16. QRMatrix::M_FINDER_DARK => $_POST['m_finder_dark'],
  17. QRMatrix::M_FINDER_DOT => $_POST['m_finder_dark'],
  18. QRMatrix::M_FINDER => $_POST['m_finder_light'],
  19. // alignment
  20. QRMatrix::M_ALIGNMENT_DARK => $_POST['m_alignment_dark'],
  21. QRMatrix::M_ALIGNMENT => $_POST['m_alignment_light'],
  22. // timing
  23. QRMatrix::M_TIMING_DARK => $_POST['m_timing_dark'],
  24. QRMatrix::M_TIMING => $_POST['m_timing_light'],
  25. // format
  26. QRMatrix::M_FORMAT_DARK => $_POST['m_format_dark'],
  27. QRMatrix::M_FORMAT => $_POST['m_format_light'],
  28. // version
  29. QRMatrix::M_VERSION_DARK => $_POST['m_version_dark'],
  30. QRMatrix::M_VERSION => $_POST['m_version_light'],
  31. // data
  32. QRMatrix::M_DATA_DARK => $_POST['m_data_dark'],
  33. QRMatrix::M_DATA => $_POST['m_data_light'],
  34. // darkmodule
  35. QRMatrix::M_DARKMODULE => $_POST['m_darkmodule_dark'],
  36. // separator
  37. QRMatrix::M_SEPARATOR => $_POST['m_separator_light'],
  38. // quietzone
  39. QRMatrix::M_QUIETZONE => $_POST['m_quietzone_light'],
  40. ];
  41. $moduleValues = array_map(function($v){
  42. if(preg_match('/[a-f\d]{6}/i', $v) === 1){
  43. return in_array($_POST['output_type'], ['png', 'jpg', 'gif'])
  44. ? array_map('hexdec', str_split($v, 2))
  45. : '#'.$v ;
  46. }
  47. return null;
  48. }, $moduleValues);
  49. $ecc = in_array($_POST['ecc'], ['L', 'M', 'Q', 'H'], true) ? $_POST['ecc'] : 'L';
  50. $qro = new QROptions;
  51. $qro->version = (int)$_POST['version'];
  52. $qro->eccLevel = constant('chillerlan\\QRCode\\QRCode::ECC_'.$ecc);
  53. $qro->maskPattern = (int)$_POST['maskpattern'];
  54. $qro->addQuietzone = isset($_POST['quietzone']);
  55. $qro->quietzoneSize = (int)$_POST['quietzonesize'];
  56. $qro->moduleValues = $moduleValues;
  57. $qro->outputType = $_POST['output_type'];
  58. $qro->scale = (int)$_POST['scale'];
  59. $qro->imageTransparent = false;
  60. $qrcode = (new QRCode($qro))->render($_POST['inputstring']);
  61. if(in_array($_POST['output_type'], ['png', 'jpg', 'gif'])){
  62. $qrcode = '<img src="'.$qrcode.'" />';
  63. }
  64. elseif($_POST['output_type'] === 'text'){
  65. $qrcode = '<pre style="font-size: 75%; line-height: 1;">'.$qrcode.'</pre>';
  66. }
  67. elseif($_POST['output_type'] === 'json'){
  68. $qrcode = '<pre style="font-size: 75%; overflow-x: auto;">'.$qrcode.'</pre>';
  69. }
  70. sendResponse(['qrcode' => $qrcode]);
  71. }
  72. // Pokémon exception handler
  73. catch(Exception $e){
  74. header('HTTP/1.1 500 Internal Server Error');
  75. sendResponse(['error' => $e->getMessage()]);
  76. }
  77. /**
  78. * @param array $response
  79. */
  80. function sendResponse(array $response){
  81. header('Content-type: application/json;charset=utf-8;');
  82. echo json_encode($response);
  83. exit;
  84. }