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