QRcode.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Utils;
  3. use App\Models\User;
  4. use App\Services\Config;
  5. class QRcode
  6. {
  7. public static function decode($url)
  8. {
  9. switch (Config::get('telegram_qrcode')) {
  10. case 'phpzbar':
  11. return QRcode::phpzbar_decode($url);
  12. case 'online':
  13. return QRcode::online_decode($url);
  14. case 'zxing_local':
  15. return QRcode::zxing_local_decode($url);
  16. default:
  17. return QRcode::zxing_decode($url);
  18. }
  19. }
  20. public static function online_decode($url)
  21. {
  22. $data = array();
  23. $data['fileurl'] = $url;
  24. $param = http_build_query($data, '', '&');
  25. $sock = new HTTPSocket;
  26. $sock->connect("api.qrserver.com", 80);
  27. $sock->set_method('GET');
  28. $sock->query('/v1/read-qr-code/', $param);
  29. $raw_text = $sock->fetch_body();
  30. $result_array = json_decode($raw_text, true);
  31. if (isset($result_array[0])) {
  32. return $result_array[0]['symbol'][0]['data'];
  33. }
  34. return null;
  35. }
  36. public static function phpzbar_decode($url)
  37. {
  38. $filepath = BASE_PATH."/storage/".time().rand(1, 100).".png";
  39. $img = file_get_contents($url);
  40. file_put_contents($filepath, $img);
  41. /* Create new image object */
  42. $image = new \ZBarCodeImage($filepath);
  43. /* Create a barcode scanner */
  44. $scanner = new \ZBarCodeScanner();
  45. /* Scan the image */
  46. $barcode = $scanner->scan($image);
  47. unlink($filepath);
  48. return (isset($barcode[0]['data']) ? $barcode[0]['data'] : null);
  49. }
  50. public static function zxing_local_decode($url)
  51. {
  52. $filepath = BASE_PATH."/storage/".time().rand(1, 100).".png";
  53. $img = file_get_contents($url);
  54. file_put_contents($filepath, $img);
  55. $qrcode = new \QrReader($filepath);
  56. $text = $qrcode->text(); //return decoded text from QR Code
  57. unlink($filepath);
  58. if ($text == null || $text == "") {
  59. return null;
  60. }
  61. return $text;
  62. }
  63. public static function zxing_decode($url)
  64. {
  65. $raw_text = file_get_contents("https://zxing.org/w/decode?u=".urlencode($url));
  66. return Tools::get_middle_text($raw_text, "<tr><td>Raw text</td><td><pre>", "</pre></td></tr><tr><td>Raw bytes</td><td><pre>");
  67. }
  68. }