# qr-code
**Repository Path**: ahai574/qr-code
## Basic Information
- **Project Name**: qr-code
- **Description**: 修改二维码背景透明(背景颜色--) 二维码码点修改--比如椭圆形等 (修改码点 同时修改码点的颜色)
- **Primary Language**: PHP
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2022-04-19
- **Last Updated**: 2022-04-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## **这里只进行描述如何修改--本地已测试过了是可以的,代码就懒得整理了,等有时间再做吧**
## 二维码 样式是否符合预期 自己修改对应的函数即可--- 总之自己找到对应的函数即可-
endroid/qr-code是二维码的外部内容渲染--比如标题 logo 外边距等 -- 也就是logo其实也可以自定义修改
bacon/bacon-qr-code是二维码码点的渲染---具体出什么问题找到对应位置函数进行调试修改即可---可以调试各种各样的二维码,草料二维码有很多的样式参考,具体想要什么样式可以参考着做,比如码点渐变色,码点的形状等(码点的渲染是从左到右,然后一行一行往下渲染,横竖由29个码点区--所以可以具体的区调整(你能拿到二维码的宽高,之后进行计算即可))
## 目的1:可以生成透明背景的二维码(替换qrcode里面渲染背景的函数,修改为可以定义透明度的函数);
vendor\endroid\qr-code\src\Writer\PngWriter.php
addMargin()和addLabel()
将上面两个函数中的 背景色的颜色渲染 imagecolorallocate 修改为 imagecolorallocatealpha
imageToString()
将上面函数修改为:
`
ob_start();
//设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息。
imagesavealpha($image, true);
imagepng($image);
$string = ob_get_clean();
return $string;
`
## 目的2:修改码点的形状和颜色的定义
码点的渲染函数是:vendor\bacon\bacon-qr-code\src\BaconQrCode\Renderer\Image\Png.php 的 drawBlock()
`
//矩形 --- 默认是矩形
imagefilledrectangle(
$this->image,
$x,
$y,
$x + $this->blockSize - 1,
$y + $this->blockSize - 1,
$this->colors[$colorId]
);
可以修改为:
$totalWidth = $this->finalWidth; //总宽度
$totalHeight = $this->finalHeight; //总高度
//椭圆 --- 二维码 横向和纵向 由29个方块组成 间距2
imagefilledellipse (
$this->image,
$x+($totalWidth/29)/2-2, //圆点的中心 == 横坐标加上椭圆的半径 -- 横向
$y+($totalHeight/29)/2-2, //圆点的中心 == 横坐标加上椭圆的半径 纵向
($totalWidth/29)-1,//横坐标加上椭圆的直径-- 横向 --这里要减去1
($totalHeight/29)-1,//横坐标加上椭圆的直径-- 纵向
$this->colors[$colorId]
);
addColor() 定义 == $this->alphaColors[$colorId] ==//其实这个就是定义透明度的背景块
drawBackground() == 在这个函数里 除了增加透明度 你还可以增加背景图片
修改 imagefill($this->image, 0, 0, $this->alphaColors[$colorId]);
getByteStream() 函数修改为:---如果要二维码内容区背景透明 就修改
ob_start();
//设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息。
imagesavealpha($this->image, true);
imagepng($this->image);
return ob_get_clean();
`
# QR Code
*By [endroid](https://endroid.nl/)*
[](https://packagist.org/packages/endroid/qr-code)
[](https://github.com/endroid/qr-code/actions)
[](https://packagist.org/packages/endroid/qr-code)
[](https://packagist.org/packages/endroid/qr-code)
[](https://packagist.org/packages/endroid/qr-code)
This library helps you generate QR codes in a jiffy. Makes use of [bacon/bacon-qr-code](https://github.com/Bacon/BaconQrCode)
to generate the matrix and [khanamiryan/qrcode-detector-decoder](https://github.com/khanamiryan/php-qrcode-detector-decoder)
for validating generated QR codes. Further extended with Twig extensions, generation routes, a factory and a
Symfony bundle for easy installation and configuration. Different writers are provided to generate the QR code
as PNG, SVG, EPS or in binary format.
## Installation
Use [Composer](https://getcomposer.org/) to install the library. Also make sure you have enabled and configured the
[GD extension](https://www.php.net/manual/en/book.image.php) if you want to generate images.
``` bash
$ composer require endroid/qr-code
```
## Usage: using the builder
```php
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter;
use Endroid\QrCode\Label\Font\NotoSans;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
$result = Builder::create()
->writer(new PngWriter())
->writerOptions([])
->data('Custom QR code contents')
->encoding(new Encoding('UTF-8'))
->errorCorrectionLevel(new ErrorCorrectionLevelHigh())
->size(300)
->margin(10)
->roundBlockSizeMode(new RoundBlockSizeModeMargin())
->logoPath(__DIR__.'/assets/symfony.png')
->labelText('This is the label')
->labelFont(new NotoSans(20))
->labelAlignment(new LabelAlignmentCenter())
->build();
```
## Usage: without using the builder
```php
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelLow;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;
$writer = new PngWriter();
// Create QR code
$qrCode = QrCode::create('Data')
->setEncoding(new Encoding('UTF-8'))
->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
->setSize(300)
->setMargin(10)
->setRoundBlockSizeMode(new RoundBlockSizeModeMargin())
->setForegroundColor(new Color(0, 0, 0))
->setBackgroundColor(new Color(255, 255, 255));
// Create generic logo
$logo = Logo::create(__DIR__.'/assets/symfony.png')
->setResizeToWidth(50);
// Create generic label
$label = Label::create('Label')
->setTextColor(new Color(255, 0, 0));
$result = $writer->write($qrCode, $logo, $label);
```
## Usage: working with results
```php
// Directly output the QR code
header('Content-Type: '.$result->getMimeType());
echo $result->getString();
// Save it to a file
$result->saveToFile(__DIR__.'/qrcode.png');
// Generate a data URI to include image data inline (i.e. inside an
tag)
$dataUri = $result->getDataUri();
```

### Writer options
```php
use Endroid\QrCode\Writer\SvgWriter;
$builder->setWriterOptions([SvgWriter::WRITER_OPTION_EXCLUDE_XML_DECLARATION => true]);
```
### Encoding
If you use a barcode scanner you can have some troubles while reading the
generated QR codes. Depending on the encoding you chose you will have an extra
amount of data corresponding to the ECI block. Some barcode scanner are not
programmed to interpret this block of information. To ensure a maximum
compatibility you can use the `ISO-8859-1` encoding that is the default
encoding used by barcode scanners (if your character set supports it,
i.e. no Chinese characters are present).
### Round block size mode
By default block sizes are rounded to guarantee sharp images and improve
readability. However some other rounding variants are available.
* `margin (default)`: the size of the QR code is shrunk if necessary but the size
of the final image remains unchanged due to additional margin being added.
* `enlarge`: the size of the QR code and the final image are enlarged when
rounding differences occur.
* `shrink`: the size of the QR code and the final image are
shrunk when rounding differences occur.
* `none`: No rounding. This mode can be used when blocks don't need to be rounded
to pixels (for instance SVG).
## Readability
The readability of a QR code is primarily determined by the size, the input
length, the error correction level and any possible logo over the image so you
can tweak these parameters if you are looking for optimal results. You can also
check $qrCode->getRoundBlockSize() value to see if block dimensions are rounded
so that the image is more sharp and readable. Please note that rounding block
size can result in additional padding to compensate for the rounding difference.
And finally the encoding (default UTF-8 to support large character sets) can be
set to `ISO-8859-1` if possible to improve readability.
## Built-in validation reader
You can enable the built-in validation reader (disabled by default) by calling
setValidateResult(true). This validation reader does not guarantee that the QR
code will be readable by all readers but it helps you provide a minimum level
of quality. Take note that the validator can consume quite amount of additional
resources and it should be installed separately only if you use it.
## Symfony integration
The [endroid/qr-code-bundle](https://github.com/endroid/qr-code-bundle)
integrates the QR code library in Symfony for an even better experience.
* Configure your defaults (like image size, default writer etc.)
* Support for multiple configurations and injection via aliases
* Generate QR codes for defined configurations via URL like /qr-code//Hello
* Generate QR codes or URLs directly from Twig using dedicated functions
Read the [bundle documentation](https://github.com/endroid/qr-code-bundle)
for more information.
## Versioning
Version numbers follow the MAJOR.MINOR.PATCH scheme. Backwards compatibility
breaking changes will be kept to a minimum but be aware that these can occur.
Lock your dependencies for production and test your code when upgrading.
## License
This bundle is under the MIT license. For the full copyright and license
information please view the LICENSE file that was distributed with this source code.