QRGdImage.md.txt 4.0 KB

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