Built-In-Output-QRGdImage.md.txt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # QRGdImage
  2. [Class `QRGdImage`](https://github.com/chillerlan/php-qrcode/blob/main/src/Output/QRGdImage.php): [GdImage](https://www.php.net/manual/book.image) raster graphic output (GIF, JPG, PNG)
  3. ## Example
  4. See: [GdImage example](https://github.com/chillerlan/php-qrcode/blob/main/examples/image.php)
  5. Set the options:
  6. ```php
  7. $options = new QROptions;
  8. // $outputType can be one of: GDIMAGE_BMP, GDIMAGE_GIF, GDIMAGE_JPG, GDIMAGE_PNG, GDIMAGE_WEBP
  9. $options->outputType = QROutputInterface::GDIMAGE_WEBP;
  10. $options->quality = 90;
  11. // the size of one qr module in pixels
  12. $options->scale = 20;
  13. $options->bgColor = [200, 150, 200];
  14. $options->imageTransparent = true;
  15. // the color that will be set transparent
  16. // @see https://www.php.net/manual/en/function.imagecolortransparent
  17. $options->transparencyColor = [200, 150, 200];
  18. $options->drawCircularModules = true;
  19. $options->drawLightModules = true;
  20. $options->circleRadius = 0.4;
  21. $options->keepAsSquare = [
  22. QRMatrix::M_FINDER_DARK,
  23. QRMatrix::M_FINDER_DOT,
  24. QRMatrix::M_ALIGNMENT_DARK,
  25. ];
  26. $options->moduleValues = [
  27. QRMatrix::M_FINDER_DARK => [0, 63, 255], // dark (true)
  28. QRMatrix::M_FINDER_DOT => [0, 63, 255], // finder dot, dark (true)
  29. QRMatrix::M_FINDER => [233, 233, 233], // light (false)
  30. QRMatrix::M_ALIGNMENT_DARK => [255, 0, 255],
  31. QRMatrix::M_ALIGNMENT => [233, 233, 233],
  32. QRMatrix::M_DATA_DARK => [0, 0, 0],
  33. QRMatrix::M_DATA => [233, 233, 233],
  34. ];
  35. ```
  36. Render the output:
  37. ```php
  38. $data = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
  39. $out = (new QRCode($options))->render($data); // -> data:image/webp;base64,...
  40. printf('<img alt="%s" src="%s" />', $alt, $out);
  41. ```
  42. Return the `GdImage` instance/resource (will ignore other output options):
  43. ```php
  44. $options->returnResource = true;
  45. /** @var \GdImage|resource $gdImage */
  46. $gdImage = (new QRCode($options))->render($data);
  47. // do stuff with the GdImage instance...
  48. $size = imagesx($gdImage);
  49. // ...
  50. // ...dump output
  51. header('Content-type: image/jpeg');
  52. imagejpeg($gdImage);
  53. imagedestroy($gdImage);
  54. ```
  55. ## Additional methods
  56. | method | return | description |
  57. |---------------------------------------------------|----------|-------------------------------------------------------------------|
  58. | (protected) `drawImage()` | `void` | Draws the QR image |
  59. | (protected) `dumpImage()` | `string` | Creates the final image by calling the desired GD output function |
  60. | (protected) `module(int $x, int $y, int $M_TYPE)` | `void` | Renders a single module |
  61. | (protected) `setBgColor()` | `void` | Sets the background color |
  62. | (protected) `setTransparencyColor()` | `void` | Sets the transparency color |
  63. ## Options that affect this module
  64. | property | type |
  65. |------------------------|----------------|
  66. | `$bgColor` | `mixed` |
  67. | `$circleRadius` | `float` |
  68. | `$drawCircularModules` | `bool` |
  69. | `$drawLightModules` | `bool` |
  70. | `$imageTransparent` | `bool` |
  71. | `$quality` | `int` |
  72. | `$keepAsSquare` | `array` |
  73. | `$outputBase64` | `bool` |
  74. | `$returnResource` | `bool` |
  75. | `$scale` | `int` |
  76. | `$transparencyColor` | `mixed` |
  77. ### Options that have no effect
  78. | property | reason |
  79. |-----------------------|--------|
  80. | `$connectPaths` | N/A |
  81. | `$excludeFromConnect` | N/A |