qrcode-interactive.php 3.2 KB

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