qrcode-interactive.php 3.1 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. try{
  23. $moduleValues = [
  24. // finder
  25. QRMatrix::M_FINDER_DARK => $_POST['m_finder_dark'],
  26. QRMatrix::M_FINDER_DOT => $_POST['m_finder_dot_dark'],
  27. QRMatrix::M_FINDER => $_POST['m_finder_light'],
  28. // alignment
  29. QRMatrix::M_ALIGNMENT_DARK => $_POST['m_alignment_dark'],
  30. QRMatrix::M_ALIGNMENT => $_POST['m_alignment_light'],
  31. // timing
  32. QRMatrix::M_TIMING_DARK => $_POST['m_timing_dark'],
  33. QRMatrix::M_TIMING => $_POST['m_timing_light'],
  34. // format
  35. QRMatrix::M_FORMAT_DARK => $_POST['m_format_dark'],
  36. QRMatrix::M_FORMAT => $_POST['m_format_light'],
  37. // version
  38. QRMatrix::M_VERSION_DARK => $_POST['m_version_dark'],
  39. QRMatrix::M_VERSION => $_POST['m_version_light'],
  40. // data
  41. QRMatrix::M_DATA_DARK => $_POST['m_data_dark'],
  42. QRMatrix::M_DATA => $_POST['m_data_light'],
  43. // darkmodule
  44. QRMatrix::M_DARKMODULE => $_POST['m_darkmodule_dark'],
  45. // separator
  46. QRMatrix::M_SEPARATOR => $_POST['m_separator_light'],
  47. // quietzone
  48. QRMatrix::M_QUIETZONE => $_POST['m_quietzone_light'],
  49. // logo
  50. QRMatrix::M_LOGO => $_POST['m_logo_light'],
  51. ];
  52. $moduleValues = array_map(function($v){
  53. if(preg_match('/[a-f\d]{6}/i', $v) === 1){
  54. return in_array($_POST['output_type'], ['png', 'jpg', 'gif'], true)
  55. ? array_map('hexdec', str_split($v, 2))
  56. : '#'.$v ;
  57. }
  58. return null;
  59. }, $moduleValues);
  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. 'moduleValues' => $moduleValues,
  68. 'outputType' => $_POST['output_type'],
  69. 'scale' => (int)$_POST['scale'],
  70. 'outputBase64' => true,
  71. 'imageTransparent' => false,
  72. ]);
  73. $qrcode = (new QRCode($options))->render($_POST['inputstring']);
  74. if(in_array($_POST['output_type'], ['png', 'jpg', 'gif', 'svg'], true)){
  75. $qrcode = '<img alt="qrcode" src="'.$qrcode.'" />';
  76. }
  77. elseif($_POST['output_type'] === 'text'){
  78. $qrcode = '<pre style="font-size: 75%; line-height: 1;">'.$qrcode.'</pre>';
  79. }
  80. elseif($_POST['output_type'] === 'json'){
  81. $qrcode = '<pre style="font-size: 75%; overflow-x: auto;">'.$qrcode.'</pre>';
  82. }
  83. sendResponse(['qrcode' => $qrcode]);
  84. }
  85. // Pokémon exception handler
  86. catch(Throwable $e){
  87. header('HTTP/1.1 500 Internal Server Error');
  88. sendResponse(['error' => $e->getMessage()]);
  89. }