You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.0 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. QR Code
  2. =======
  3. *By [endroid](http://endroid.nl/)*
  4. [![Latest Stable Version](http://img.shields.io/packagist/v/endroid/qrcode.svg)](https://packagist.org/packages/endroid/qrcode)
  5. [![Build Status](http://img.shields.io/travis/endroid/QrCode.svg)](http://travis-ci.org/endroid/QrCode)
  6. [![Total Downloads](http://img.shields.io/packagist/dt/endroid/qrcode.svg)](https://packagist.org/packages/endroid/qrcode)
  7. [![Monthly Downloads](http://img.shields.io/packagist/dm/endroid/qrcode.svg)](https://packagist.org/packages/endroid/qrcode)
  8. [![License](http://img.shields.io/packagist/l/endroid/qrcode.svg)](https://packagist.org/packages/endroid/qrcode)
  9. This library based on QRcode Perl CGI & PHP scripts by Y. Swetake helps you generate images containing a QR code.
  10. ## Installation
  11. Use [Composer](https://getcomposer.org/) to install the library.
  12. ``` bash
  13. $ composer require endroid/qrcode
  14. ```
  15. ## Usage
  16. ```php
  17. use Endroid\QrCode\QrCode;
  18. $qrCode = new QrCode();
  19. $qrCode
  20. ->setText('Life is too short to be generating QR codes')
  21. ->setSize(300)
  22. ->setPadding(10)
  23. ->setErrorCorrection('high')
  24. ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
  25. ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
  26. ->setLabel('Scan the code')
  27. ->setLabelFontSize(16)
  28. ->setImageType(QrCode::IMAGE_TYPE_PNG)
  29. ;
  30. // now we can directly output the qrcode
  31. header('Content-Type: '.$qrCode->getContentType());
  32. $qrCode->render();
  33. // save it to a file
  34. $qrCode->save('qrcode.png');
  35. // or create a response object
  36. $response = new Response($qrCode->get(), 200, ['Content-Type' => $qrCode->getContentType()]);
  37. ```
  38. ![QR Code](http://endroid.nl/qrcode/Life%20is%20too%20short%20to%20be%20generating%20QR%20codes.png?label=Scan%20the%20code)
  39. ## Symfony integration
  40. Register the Symfony bundle in the kernel.
  41. ```php
  42. // app/AppKernel.php
  43. public function registerBundles()
  44. {
  45. $bundles = [
  46. // ...
  47. new Endroid\QrCode\Bundle\EndroidQrCodeBundle(),
  48. ];
  49. }
  50. ```
  51. The default parameters can be overridden via the configuration.
  52. Alpha channel available range is [0, 127] in foreground and background colors.
  53. ```yaml
  54. endroid_qr_code:
  55. size: 100
  56. padding: 10
  57. extension: gif
  58. error_correction_level: high
  59. foreground_color: { r: 0, g: 0, b: 0, a: 0 }
  60. background_color: { r: 255, g: 255, b: 255, a: 0 }
  61. label: 'My label'
  62. label_font_size: 16
  63. ```
  64. Now you can retrieve the factory as follows.
  65. ```php
  66. $factory = $this->get('endroid.qrcode.factory');
  67. $factory->createQrCode();
  68. ```
  69. Add the following section to your routing to be able to handle QR code URLs.
  70. This step can be skipped when you only use data URIs to display your images.
  71. ``` yml
  72. EndroidQrCodeBundle:
  73. resource: "@EndroidQrCodeBundle/Controller/"
  74. type: annotation
  75. prefix: /qrcode
  76. ```
  77. After installation and configuration, QR codes can be generated by appending
  78. the QR code text to the url as mounted, followed by .png, .jpg or .gif.
  79. ## Twig extension
  80. The bundle also provides a Twig extension for quickly generating QR code urls.
  81. Optional parameters are extension, size, padding and errorCorrectionLevel. When
  82. a parameter is omitted, the value in the bundle configuration is used.
  83. ``` twig
  84. <img src="{{ qrcode_url(message) }}" />
  85. <img src="{{ qrcode_url(message, { extension: 'png' }) }}" />
  86. <img src="{{ qrcode_url(message, { size: 150 }) }}" />
  87. ```
  88. You can also use the data URI helper to embed the QR code within your HTML
  89. instead of requiring a separate HTTP request to load your image.
  90. ``` twig
  91. <img src="{{ qrcode_data_uri(message, { size: 200, padding: 10 }) }}" />
  92. ```
  93. ## Versioning
  94. Version numbers follow the MAJOR.MINOR.PATCH scheme. Backwards compatibility
  95. breaking changes will be kept to a minimum but be aware that these can occur.
  96. Lock your dependencies for production and test your code when upgrading.
  97. ## License
  98. This bundle is under the MIT license. For the full copyright and license
  99. information please view the LICENSE file that was distributed with this source code.