Quickstart.md.txt 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // default output is a base64 encoded data URI
  14. printf('<img src="%s" alt="QR Code" />', $qrcode);
  15. ```
  16. ### Configuration
  17. Configuration using `QROptions`:
  18. ```php
  19. $options = new QROptions;
  20. $options->version = 7;
  21. $options->outputBase64 = false; // output raw image instead of base64 data URI
  22. header('Content-type: image/svg+xml'); // the image type is SVG by default
  23. echo (new QRCode($options))->render($data);
  24. ```
  25. See [Advanced usage](../Usage/Advanced-usage.md) for a more in-depth usage guide
  26. and [configuration settings](../Usage/Configuration-settings.md) for a list of available options.
  27. Also, have a look [in the examples folder](https://github.com/chillerlan/php-qrcode/tree/main/examples) for some more usage examples.
  28. ## Reading QR Codes
  29. Using the built-in QR Code reader is pretty straight-forward:
  30. ```php
  31. try{
  32. $result = (new QRCode)->readFromFile('path/to/file.png'); // -> DecoderResult
  33. // you can now use the result instance...
  34. $content = $result->data;
  35. // ...or simply cast the result instance to string to get the content
  36. $content = (string)$result;
  37. }
  38. catch(Throwable $exception){
  39. // handle exception...
  40. }
  41. ```
  42. 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.
  43. ## Notes
  44. The QR encoder, especially the subroutines for mask pattern testing, can cause high CPU load on increased matrix size.
  45. You can avoid a part of this load by choosing a fast output module, like SVG.
  46. Oh hey and don't forget to sanitize any user input!