qrcode-interactive.php 3.1 KB

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