qrcode.php 2.5 KB

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