qrcode.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @filesource qrcode.php
  4. * @created 18.11.2017
  5. * @author Smiley <smiley@chillerlan.net>
  6. * @copyright 2017 Smiley
  7. * @license MIT
  8. */
  9. namespace chillerlan\QRCodePublic;
  10. use chillerlan\QRCode\QRCode;
  11. use chillerlan\QRCode\QROptions;
  12. require_once '../vendor/autoload.php';
  13. try{
  14. $moduleValues = [
  15. // finder
  16. 1536 => $_POST['m_finder_dark'],
  17. 6 => $_POST['m_finder_light'],
  18. // alignment
  19. 2560 => $_POST['m_alignment_dark'],
  20. 10 => $_POST['m_alignment_light'],
  21. // timing
  22. 3072 => $_POST['m_timing_dark'],
  23. 12 => $_POST['m_timing_light'],
  24. // format
  25. 3584 => $_POST['m_format_dark'],
  26. 14 => $_POST['m_format_light'],
  27. // version
  28. 4096 => $_POST['m_version_dark'],
  29. 16 => $_POST['m_version_light'],
  30. // data
  31. 1024 => $_POST['m_data_dark'],
  32. 4 => $_POST['m_data_light'],
  33. // darkmodule
  34. 512 => $_POST['m_darkmodule_dark'],
  35. // separator
  36. 8 => $_POST['m_separator_light'],
  37. // quietzone
  38. 18 => $_POST['m_quietzone_light'],
  39. ];
  40. $moduleValues = array_map(function($v){
  41. #var_dump(array_map('hexdec', unpack('a2r/a2g/a2b','ff00ff')));
  42. if(preg_match('/[a-f\d]{6}/i', $v) === 1){
  43. return '#'.$v;
  44. }
  45. return strpos($v, '_dark') !== false
  46. ? '#111111'
  47. : '#eeeeee';
  48. }, $moduleValues);
  49. $ecc = in_array($_POST['ecc'], ['L', 'M', 'Q', 'H'], true) ? $_POST['ecc'] : 'L';
  50. $qro = new QROptions;
  51. $qro->version = (int)$_POST['version'];
  52. $qro->eccLevel = constant('chillerlan\\QRCode\\QRCode::ECC_'.$ecc);
  53. $qro->moduleValues = $moduleValues;
  54. $qro->outputType = $_POST['output_type'];
  55. $qro->maskPattern = (int)$_POST['maskpattern'];
  56. $qro->addQuietzone = isset($_POST['quietzone']);
  57. $qro->quietzoneSize = (int)$_POST['quietzonesize'];
  58. $qro->scale = (int)$_POST['scale'];
  59. $qrcode = (new QRCode($qro))->render($_POST['inputstring']);
  60. send_response(['qrcode' => $qrcode]);
  61. }
  62. // Pokémon exception handler
  63. catch(\Exception $e){
  64. header('HTTP/1.1 500 Internal Server Error');
  65. send_response(['error' => $e->getMessage()]);
  66. }
  67. /**
  68. * @param array $response
  69. */
  70. function send_response(array $response){
  71. header('Content-type: application/json;charset=utf-8;');
  72. echo json_encode($response);
  73. exit;
  74. }