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