Usage-Quickstart.md.txt 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Quickstart
  2. ## Import the library
  3. Import the main class(es) and include the autoloader (if necessary):
  4. ```php
  5. use chillerlan\QRCode\{QRCode, QROptions};
  6. require_once __DIR__.'/../vendor/autoload.php';
  7. ```
  8. ## Create your first QR Code
  9. We want to encode this URI for a mobile authenticator into a QRcode image:
  10. ```php
  11. $data = 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net';
  12. $qrcode = (new QRCode)->render($data);
  13. printf('<img src="%s" alt="QR Code" />', $qrcode);
  14. ```
  15. ### Configuration
  16. Configuration using `QROptions`:
  17. ```php
  18. $options = new QROptions;
  19. $options->version = 7;
  20. $options->outputBase64 = false; // output raw image instead of base64 data URI
  21. header('Content-type: image/svg+xml'); // the image type is SVG by default
  22. echo (new QRCode($options))->render($data);
  23. ```
  24. See [Advanced usage](./Usage-Advanced-usage.md) for a more in-depth usage guide.
  25. Also, have a look [in the examples folder](https://github.com/chillerlan/php-qrcode/tree/main/examples) for some more usage examples.
  26. ## Reading QR Codes
  27. Using the built-in QR Code reader is pretty straight-forward:
  28. ```php
  29. try{
  30. $result = (new QRCode)->readFromFile('path/to/file.png'); // -> DecoderResult
  31. }
  32. catch(Throwable $exception){
  33. // handle exception...
  34. // throw ...
  35. }
  36. // you can now use the result instance...
  37. $content = $result->data;
  38. // ...or simply cast the result instance to string to get the content
  39. $content = (string)$result;
  40. ```
  41. It's generally a good idea to wrap the reading in a try/catch block to handle any errors that may occur in the process.
  42. ## Notes
  43. The QR encoder, especially the subroutines for mask pattern testing, can cause high CPU load on increased matrix size.
  44. You can avoid a part of this load by choosing a fast output module, like SVG.
  45. Oh hey and don't forget to sanitize any user input!