| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Endroid\QrCode\Writer; |
| 6 |
|
| 7 |
use Endroid\QrCode\Bacon\MatrixFactory; |
| 8 |
use Endroid\QrCode\Exception\ValidationException; |
| 9 |
use Endroid\QrCode\ImageData\LabelImageData; |
| 10 |
use Endroid\QrCode\ImageData\LogoImageData; |
| 11 |
use Endroid\QrCode\Label\LabelAlignment; |
| 12 |
use Endroid\QrCode\Label\LabelInterface; |
| 13 |
use Endroid\QrCode\Logo\LogoInterface; |
| 14 |
use Endroid\QrCode\Matrix\MatrixInterface; |
| 15 |
use Endroid\QrCode\QrCodeInterface; |
| 16 |
use Endroid\QrCode\RoundBlockSizeMode; |
| 17 |
use Endroid\QrCode\Writer\Result\GdResult; |
| 18 |
use Endroid\QrCode\Writer\Result\ResultInterface; |
| 19 |
use Zxing\QrReader; |
| 20 |
|
| 21 |
abstract class AbstractGdWriter implements WriterInterface, ValidatingWriterInterface |
| 22 |
{ |
| 23 |
protected function getMatrix(QrCodeInterface $qrCode): MatrixInterface |
| 24 |
{ |
| 25 |
$matrixFactory = new MatrixFactory(); |
| 26 |
|
| 27 |
return $matrixFactory->create($qrCode); |
| 28 |
} |
| 29 |
|
| 30 |
public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, array $options = []): ResultInterface |
| 31 |
{ |
| 32 |
if (!extension_loaded('gd')) { |
| 33 |
throw new \Exception('Unable to generate image: please check if the GD extension is enabled and configured correctly'); |
| 34 |
} |
| 35 |
|
| 36 |
$matrix = $this->getMatrix($qrCode); |
| 37 |
|
| 38 |
$baseBlockSize = RoundBlockSizeMode::None === $qrCode->getRoundBlockSizeMode() ? 10 : intval($matrix->getBlockSize()); |
| 39 |
$baseImage = imagecreatetruecolor($matrix->getBlockCount() * $baseBlockSize, $matrix->getBlockCount() * $baseBlockSize); |
| 40 |
|
| 41 |
if (!$baseImage) { |
| 42 |
throw new \Exception('Unable to generate image: please check if the GD extension is enabled and configured correctly'); |
| 43 |
} |
| 44 |
|
| 45 |
/** @var int $foregroundColor */ |
| 46 |
$foregroundColor = imagecolorallocatealpha( |
| 47 |
$baseImage, |
| 48 |
$qrCode->getForegroundColor()->getRed(), |
| 49 |
$qrCode->getForegroundColor()->getGreen(), |
| 50 |
$qrCode->getForegroundColor()->getBlue(), |
| 51 |
$qrCode->getForegroundColor()->getAlpha() |
| 52 |
); |
| 53 |
|
| 54 |
/** @var int $transparentColor */ |
| 55 |
$transparentColor = imagecolorallocatealpha($baseImage, 255, 255, 255, 127); |
| 56 |
|
| 57 |
imagefill($baseImage, 0, 0, $transparentColor); |
| 58 |
|
| 59 |
for ($rowIndex = 0; $rowIndex < $matrix->getBlockCount(); ++$rowIndex) { |
| 60 |
for ($columnIndex = 0; $columnIndex < $matrix->getBlockCount(); ++$columnIndex) { |
| 61 |
if (1 === $matrix->getBlockValue($rowIndex, $columnIndex)) { |
| 62 |
imagefilledrectangle( |
| 63 |
$baseImage, |
| 64 |
$columnIndex * $baseBlockSize, |
| 65 |
$rowIndex * $baseBlockSize, |
| 66 |
($columnIndex + 1) * $baseBlockSize - 1, |
| 67 |
($rowIndex + 1) * $baseBlockSize - 1, |
| 68 |
$foregroundColor |
| 69 |
); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
$targetWidth = $matrix->getOuterSize(); |
| 75 |
$targetHeight = $matrix->getOuterSize(); |
| 76 |
|
| 77 |
if ($label instanceof LabelInterface) { |
| 78 |
$labelImageData = LabelImageData::createForLabel($label); |
| 79 |
$targetHeight += $labelImageData->getHeight() + $label->getMargin()->getTop() + $label->getMargin()->getBottom(); |
| 80 |
} |
| 81 |
|
| 82 |
$targetImage = imagecreatetruecolor($targetWidth, $targetHeight); |
| 83 |
|
| 84 |
if (!$targetImage) { |
| 85 |
throw new \Exception('Unable to generate image: please check if the GD extension is enabled and configured correctly'); |
| 86 |
} |
| 87 |
|
| 88 |
/** @var int $backgroundColor */ |
| 89 |
$backgroundColor = imagecolorallocatealpha( |
| 90 |
$targetImage, |
| 91 |
$qrCode->getBackgroundColor()->getRed(), |
| 92 |
$qrCode->getBackgroundColor()->getGreen(), |
| 93 |
$qrCode->getBackgroundColor()->getBlue(), |
| 94 |
$qrCode->getBackgroundColor()->getAlpha() |
| 95 |
); |
| 96 |
|
| 97 |
imagefill($targetImage, 0, 0, $backgroundColor); |
| 98 |
|
| 99 |
imagecopyresampled( |
| 100 |
$targetImage, |
| 101 |
$baseImage, |
| 102 |
$matrix->getMarginLeft(), |
| 103 |
$matrix->getMarginLeft(), |
| 104 |
0, |
| 105 |
0, |
| 106 |
$matrix->getInnerSize(), |
| 107 |
$matrix->getInnerSize(), |
| 108 |
imagesx($baseImage), |
| 109 |
imagesy($baseImage) |
| 110 |
); |
| 111 |
|
| 112 |
if ($qrCode->getBackgroundColor()->getAlpha() > 0) { |
| 113 |
imagesavealpha($targetImage, true); |
| 114 |
} |
| 115 |
|
| 116 |
$result = new GdResult($matrix, $targetImage); |
| 117 |
|
| 118 |
if ($logo instanceof LogoInterface) { |
| 119 |
$result = $this->addLogo($logo, $result); |
| 120 |
} |
| 121 |
|
| 122 |
if ($label instanceof LabelInterface) { |
| 123 |
$result = $this->addLabel($label, $result); |
| 124 |
} |
| 125 |
|
| 126 |
return $result; |
| 127 |
} |
| 128 |
|
| 129 |
private function addLogo(LogoInterface $logo, GdResult $result): GdResult |
| 130 |
{ |
| 131 |
$logoImageData = LogoImageData::createForLogo($logo); |
| 132 |
|
| 133 |
if ('image/svg+xml' === $logoImageData->getMimeType()) { |
| 134 |
throw new \Exception('PNG Writer does not support SVG logo'); |
| 135 |
} |
| 136 |
|
| 137 |
$targetImage = $result->getImage(); |
| 138 |
$matrix = $result->getMatrix(); |
| 139 |
|
| 140 |
if ($logoImageData->getPunchoutBackground()) { |
| 141 |
/** @var int $transparent */ |
| 142 |
$transparent = imagecolorallocatealpha($targetImage, 255, 255, 255, 127); |
| 143 |
imagealphablending($targetImage, false); |
| 144 |
$xOffsetStart = intval($matrix->getOuterSize() / 2 - $logoImageData->getWidth() / 2); |
| 145 |
$yOffsetStart = intval($matrix->getOuterSize() / 2 - $logoImageData->getHeight() / 2); |
| 146 |
for ($xOffset = $xOffsetStart; $xOffset < $xOffsetStart + $logoImageData->getWidth(); ++$xOffset) { |
| 147 |
for ($yOffset = $yOffsetStart; $yOffset < $yOffsetStart + $logoImageData->getHeight(); ++$yOffset) { |
| 148 |
imagesetpixel($targetImage, $xOffset, $yOffset, $transparent); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
imagecopyresampled( |
| 154 |
$targetImage, |
| 155 |
$logoImageData->getImage(), |
| 156 |
intval($matrix->getOuterSize() / 2 - $logoImageData->getWidth() / 2), |
| 157 |
intval($matrix->getOuterSize() / 2 - $logoImageData->getHeight() / 2), |
| 158 |
0, |
| 159 |
0, |
| 160 |
$logoImageData->getWidth(), |
| 161 |
$logoImageData->getHeight(), |
| 162 |
imagesx($logoImageData->getImage()), |
| 163 |
imagesy($logoImageData->getImage()) |
| 164 |
); |
| 165 |
|
| 166 |
return new GdResult($matrix, $targetImage); |
| 167 |
} |
| 168 |
|
| 169 |
private function addLabel(LabelInterface $label, GdResult $result): GdResult |
| 170 |
{ |
| 171 |
$targetImage = $result->getImage(); |
| 172 |
|
| 173 |
$labelImageData = LabelImageData::createForLabel($label); |
| 174 |
|
| 175 |
/** @var int $textColor */ |
| 176 |
$textColor = imagecolorallocatealpha( |
| 177 |
$targetImage, |
| 178 |
$label->getTextColor()->getRed(), |
| 179 |
$label->getTextColor()->getGreen(), |
| 180 |
$label->getTextColor()->getBlue(), |
| 181 |
$label->getTextColor()->getAlpha() |
| 182 |
); |
| 183 |
|
| 184 |
$x = intval(imagesx($targetImage) / 2 - $labelImageData->getWidth() / 2); |
| 185 |
$y = imagesy($targetImage) - $label->getMargin()->getBottom(); |
| 186 |
|
| 187 |
if (LabelAlignment::Left === $label->getAlignment()) { |
| 188 |
$x = $label->getMargin()->getLeft(); |
| 189 |
} elseif (LabelAlignment::Right === $label->getAlignment()) { |
| 190 |
$x = imagesx($targetImage) - $labelImageData->getWidth() - $label->getMargin()->getRight(); |
| 191 |
} |
| 192 |
|
| 193 |
imagettftext($targetImage, $label->getFont()->getSize(), 0, $x, $y, $textColor, $label->getFont()->getPath(), $label->getText()); |
| 194 |
|
| 195 |
return new GdResult($result->getMatrix(), $targetImage); |
| 196 |
} |
| 197 |
|
| 198 |
public function validateResult(ResultInterface $result, string $expectedData): void |
| 199 |
{ |
| 200 |
$string = $result->getString(); |
| 201 |
|
| 202 |
if (!class_exists(QrReader::class)) { |
| 203 |
throw ValidationException::createForMissingPackage('khanamiryan/qrcode-detector-decoder'); |
| 204 |
} |
| 205 |
|
| 206 |
$reader = new QrReader($string, QrReader::SOURCE_TYPE_BLOB); |
| 207 |
if ($reader->text() !== $expectedData) { |
| 208 |
throw ValidationException::createForInvalidData($expectedData, strval($reader->text())); |
| 209 |
} |
| 210 |
} |
| 211 |
} |