qrcode.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. if(preg_match('/[a-f\d]{6}/i', $v) === 1){
  42. return in_array($_POST['output_type'], ['png', 'jpg', 'gif'])
  43. ? array_map('hexdec', str_split($v, 2))
  44. : '#'.$v ;
  45. }
  46. return null;
  47. }, $moduleValues);
  48. $ecc = in_array($_POST['ecc'], ['L', 'M', 'Q', 'H'], true) ? $_POST['ecc'] : 'L';
  49. $qro = new QROptions;
  50. $qro->version = (int)$_POST['version'];
  51. $qro->eccLevel = constant('chillerlan\\QRCode\\QRCode::ECC_'.$ecc);
  52. $qro->maskPattern = (int)$_POST['maskpattern'];
  53. $qro->addQuietzone = isset($_POST['quietzone']);
  54. $qro->quietzoneSize = (int)$_POST['quietzonesize'];
  55. $qro->moduleValues = $moduleValues;
  56. $qro->outputType = $_POST['output_type'];
  57. $qro->scale = (int)$_POST['scale'];
  58. $qro->imageTransparent = false;
  59. $qrcode = (new QRCode($qro))->render($_POST['inputstring']);
  60. if(in_array($_POST['output_type'], ['png', 'jpg', 'gif'])){
  61. $qrcode = '<img src="'.$qrcode.'" />';
  62. }
  63. elseif($_POST['output_type'] === 'text'){
  64. $qrcode = '<pre style="font-size: 75%; line-height: 1;">'.$qrcode.'</pre>';
  65. }
  66. elseif($_POST['output_type'] === 'json'){
  67. $qrcode = '<pre style="font-size: 75%; overflow-x: auto;">'.$qrcode.'</pre>';
  68. }
  69. send_response(['qrcode' => $qrcode]);
  70. }
  71. // Pokémon exception handler
  72. catch(\Exception $e){
  73. header('HTTP/1.1 500 Internal Server Error');
  74. send_response(['error' => $e->getMessage()]);
  75. }
  76. /**
  77. * @param array $response
  78. */
  79. function send_response(array $response){
  80. header('Content-type: application/json;charset=utf-8;');
  81. echo json_encode($response);
  82. exit;
  83. }