qrcode-interactive.php 3.1 KB

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