| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- /**
- * Class QRFpdf
- *
- * @created 03.06.2020
- * @author Maximilian Kresse
- * @license MIT
- *
- * @see https://github.com/chillerlan/php-qrcode/pull/49
- */
- namespace chillerlan\QRCode\Output;
- use chillerlan\QRCode\Data\QRMatrix;
- use chillerlan\Settings\SettingsContainerInterface;
- use FPDF;
- use function class_exists, count, is_array, is_numeric, max, min;
- /**
- * QRFpdf output module (requires fpdf)
- *
- * @see https://github.com/Setasign/FPDF
- * @see http://www.fpdf.org/
- */
- class QRFpdf extends QROutputAbstract{
- /**
- * QRFpdf constructor.
- *
- * @throws \chillerlan\QRCode\Output\QRCodeOutputException
- */
- public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
- if(!class_exists(FPDF::class)){
- // @codeCoverageIgnoreStart
- throw new QRCodeOutputException(
- 'The QRFpdf output requires FPDF (https://github.com/Setasign/FPDF)'.
- ' as dependency but the class "\\FPDF" couldn\'t be found.'
- );
- // @codeCoverageIgnoreEnd
- }
- parent::__construct($options, $matrix);
- }
- /**
- * @inheritDoc
- */
- protected function moduleValueIsValid($value):bool{
- if(!is_array($value) || count($value) < 3){
- return false;
- }
- // check the first 3 values of the array
- for($i = 0; $i < 3; $i++){
- if(!is_numeric($value[$i])){
- return false;
- }
- }
- return true;
- }
- /**
- * @inheritDoc
- */
- protected function getModuleValue($value):array{
- $v = [];
- for($i = 0; $i < 3; $i++){
- // clamp value
- $v[] = (int)max(0, min(255, $value[$i]));
- }
- return $v;
- }
- /**
- * @inheritDoc
- */
- protected function getDefaultModuleValue(bool $isDark):array{
- return $isDark ? [0, 0, 0] : [255, 255, 255];
- }
- /**
- * @inheritDoc
- *
- * @return string|\FPDF
- */
- public function dump(string $file = null){
- $fpdf = new FPDF('P', $this->options->fpdfMeasureUnit, [$this->length, $this->length]);
- $fpdf->AddPage();
- if($this->moduleValueIsValid($this->options->bgColor)){
- $bgColor = $this->getModuleValue($this->options->bgColor);
- /** @phan-suppress-next-line PhanParamTooFewUnpack */
- $fpdf->SetFillColor(...$bgColor);
- $fpdf->Rect(0, 0, $this->length, $this->length, 'F');
- }
- $prevColor = null;
- foreach($this->matrix->matrix() as $y => $row){
- foreach($row as $x => $M_TYPE){
- if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
- continue;
- }
- /** @var int $M_TYPE */
- $color = $this->moduleValues[$M_TYPE];
- if($prevColor !== $color){
- /** @phan-suppress-next-line PhanParamTooFewUnpack */
- $fpdf->SetFillColor(...$color);
- $prevColor = $color;
- }
- $fpdf->Rect($x * $this->scale, $y * $this->scale, $this->scale, $this->scale, 'F');
- }
- }
- if($this->options->returnResource){
- return $fpdf;
- }
- $pdfData = $fpdf->Output('S');
- $this->saveToFile($pdfData, $file);
- if($this->options->imageBase64){
- $pdfData = $this->toBase64DataURI($pdfData, 'application/pdf');
- }
- return $pdfData;
- }
- }
|