Skip to content

Commit ab358f2

Browse files
committed
:octocat: fixes/implements #223
1 parent 5e8d10d commit ab358f2

30 files changed

+569
-114
lines changed

.phan/config.php

+2
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,7 @@
5656
'suppress_issue_types' => [
5757
'PhanAccessMethodInternal',
5858
'PhanAccessOverridesFinalConstant',
59+
'PhanDeprecatedClass',
60+
'PhanDeprecatedClassConstant',
5961
],
6062
];

examples/imageWithLogo.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
use chillerlan\QRCode\{QRCode, QROptions};
1414
use chillerlan\QRCode\Common\EccLevel;
1515
use chillerlan\QRCode\Data\QRMatrix;
16-
use chillerlan\QRCode\Output\{QRGdImage, QRCodeOutputException};
16+
use chillerlan\QRCode\Output\{QRGdImagePNG, QRCodeOutputException};
1717

1818
require_once __DIR__.'/../vendor/autoload.php';
1919

2020
/*
2121
* Class definition
2222
*/
2323

24-
class QRImageWithLogo extends QRGdImage{
24+
class QRImageWithLogo extends QRGdImagePNG{
2525

2626
/**
2727
* @param string|null $file
@@ -64,7 +64,7 @@ public function dump(string $file = null, string $logo = null):string{
6464
$this->saveToFile($imageData, $file);
6565

6666
if($this->options->outputBase64){
67-
$imageData = $this->toBase64DataURI($imageData, 'image/'.$this->options->outputType);
67+
$imageData = $this->toBase64DataURI($imageData);
6868
}
6969

7070
return $imageData;

examples/imageWithRoundedShapes.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
use chillerlan\QRCode\Common\EccLevel;
1717
use chillerlan\QRCode\Data\QRMatrix;
18-
use chillerlan\QRCode\Output\QRGdImage;
18+
use chillerlan\QRCode\Output\QRGdImagePNG;
1919
use chillerlan\QRCode\Output\QROutputInterface;
2020
use chillerlan\QRCode\QRCode;
2121
use chillerlan\QRCode\QROptions;
@@ -27,7 +27,7 @@
2727
// Class definition
2828
// --------------------
2929

30-
class QRGdRounded extends QRGdImage{
30+
class QRGdRounded extends QRGdImagePNG{
3131

3232
/** @inheritDoc */
3333
public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){

examples/imageWithText.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
*/
1414

1515
use chillerlan\QRCode\{QRCode, QROptions};
16-
use chillerlan\QRCode\Output\{QROutputInterface, QRGdImage};
16+
use chillerlan\QRCode\Output\{QROutputInterface, QRGdImagePNG};
1717

1818
require_once __DIR__.'/../vendor/autoload.php';
1919

2020
/*
2121
* Class definition
2222
*/
2323

24-
class QRImageWithText extends QRGdImage{
24+
class QRImageWithText extends QRGdImagePNG{
2525

2626
/**
2727
* @inheritDoc
@@ -43,7 +43,7 @@ public function dump(string $file = null, string $text = null):string{
4343
$this->saveToFile($imageData, $file);
4444

4545
if($this->options->outputBase64){
46-
$imageData = $this->toBase64DataURI($imageData, 'image/'.$this->options->outputType);
46+
$imageData = $this->toBase64DataURI($imageData);
4747
}
4848

4949
return $imageData;
@@ -97,7 +97,6 @@ protected function addText(string $text):void{
9797
$options = new QROptions;
9898

9999
$options->version = 7;
100-
$options->outputType = QROutputInterface::GDIMAGE_PNG;
101100
$options->scale = 3;
102101
$options->outputBase64 = false;
103102

examples/text.php

+17-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use chillerlan\QRCode\{QRCode, QROptions};
1212
use chillerlan\QRCode\Data\QRMatrix;
13-
use chillerlan\QRCode\Output\{QROutputInterface, QRString};
13+
use chillerlan\QRCode\Output\{QROutputInterface, QRStringText};
1414

1515
require_once __DIR__.'/../vendor/autoload.php';
1616

@@ -22,22 +22,22 @@
2222
$options->eol = "\n";
2323
$options->textLineStart = str_repeat(' ', 6);
2424
$options->moduleValues = [
25-
QRMatrix::M_FINDER_DARK => QRString::ansi8('██', 124),
26-
QRMatrix::M_FINDER => QRString::ansi8('░░', 124),
27-
QRMatrix::M_FINDER_DOT => QRString::ansi8('██', 124),
28-
QRMatrix::M_ALIGNMENT_DARK => QRString::ansi8('██', 2),
29-
QRMatrix::M_ALIGNMENT => QRString::ansi8('░░', 2),
30-
QRMatrix::M_TIMING_DARK => QRString::ansi8('██', 184),
31-
QRMatrix::M_TIMING => QRString::ansi8('░░', 184),
32-
QRMatrix::M_FORMAT_DARK => QRString::ansi8('██', 200),
33-
QRMatrix::M_FORMAT => QRString::ansi8('░░', 200),
34-
QRMatrix::M_VERSION_DARK => QRString::ansi8('██', 21),
35-
QRMatrix::M_VERSION => QRString::ansi8('░░', 21),
36-
QRMatrix::M_DARKMODULE => QRString::ansi8('██', 53),
37-
QRMatrix::M_DATA_DARK => QRString::ansi8('██', 166),
38-
QRMatrix::M_DATA => QRString::ansi8('░░', 166),
39-
QRMatrix::M_QUIETZONE => QRString::ansi8('░░', 253),
40-
QRMatrix::M_SEPARATOR => QRString::ansi8('░░', 253),
25+
QRMatrix::M_FINDER_DARK => QRStringText::ansi8('██', 124),
26+
QRMatrix::M_FINDER => QRStringText::ansi8('░░', 124),
27+
QRMatrix::M_FINDER_DOT => QRStringText::ansi8('██', 124),
28+
QRMatrix::M_ALIGNMENT_DARK => QRStringText::ansi8('██', 2),
29+
QRMatrix::M_ALIGNMENT => QRStringText::ansi8('░░', 2),
30+
QRMatrix::M_TIMING_DARK => QRStringText::ansi8('██', 184),
31+
QRMatrix::M_TIMING => QRStringText::ansi8('░░', 184),
32+
QRMatrix::M_FORMAT_DARK => QRStringText::ansi8('██', 200),
33+
QRMatrix::M_FORMAT => QRStringText::ansi8('░░', 200),
34+
QRMatrix::M_VERSION_DARK => QRStringText::ansi8('██', 21),
35+
QRMatrix::M_VERSION => QRStringText::ansi8('░░', 21),
36+
QRMatrix::M_DARKMODULE => QRStringText::ansi8('██', 53),
37+
QRMatrix::M_DATA_DARK => QRStringText::ansi8('██', 166),
38+
QRMatrix::M_DATA => QRStringText::ansi8('░░', 166),
39+
QRMatrix::M_QUIETZONE => QRStringText::ansi8('░░', 253),
40+
QRMatrix::M_SEPARATOR => QRStringText::ansi8('░░', 253),
4141
];
4242

4343

src/Output/QREps.php

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
class QREps extends QROutputAbstract{
2424

25+
public const MIME_TYPE = 'application/postscript';
26+
2527
/**
2628
* @inheritDoc
2729
*/

src/Output/QRFpdf.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
*/
2626
class QRFpdf extends QROutputAbstract{
2727

28+
public const MIME_TYPE = 'application/pdf';
29+
2830
protected FPDF $fpdf;
2931
protected ?array $prevColor = null;
3032

@@ -146,7 +148,7 @@ public function dump(string $file = null){
146148
$this->saveToFile($pdfData, $file);
147149

148150
if($this->options->outputBase64){
149-
$pdfData = $this->toBase64DataURI($pdfData, 'application/pdf');
151+
$pdfData = $this->toBase64DataURI($pdfData);
150152
}
151153

152154
return $pdfData;

src/Output/QRGdImage.php

+46-19
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
* Converts the matrix into GD images, raw or base64 output (requires ext-gd)
2626
*
2727
* @see https://php.net/manual/book.image.php
28+
*
29+
* @deprecated 5.0.0 this class will be made abstract in future versions,
30+
* calling it directly is deprecated - use one of the child classes instead
31+
* @see https://github.com/chillerlan/php-qrcode/issues/223
2832
*/
2933
class QRGdImage extends QROutputAbstract{
3034

@@ -33,6 +37,8 @@ class QRGdImage extends QROutputAbstract{
3337
*
3438
* @see imagecreatetruecolor()
3539
* @var resource|\GdImage
40+
*
41+
* @todo: add \GdImage type in v6
3642
*/
3743
protected $image;
3844

@@ -209,6 +215,7 @@ public function dump(string $file = null){
209215
$this->saveToFile($imageData, $file);
210216

211217
if($this->options->outputBase64){
218+
// @todo: remove mime parameter in v6
212219
$imageData = $this->toBase64DataURI($imageData, 'image/'.$this->options->outputType);
213220
}
214221

@@ -261,6 +268,7 @@ protected function setBgColor():void{
261268
*/
262269
protected function setTransparencyColor():void{
263270

271+
// @todo: the jpg skip can be removed in v6
264272
if($this->options->outputType === QROutputInterface::GDIMAGE_JPG || !$this->options->imageTransparent){
265273
return;
266274
}
@@ -319,36 +327,55 @@ protected function module(int $x, int $y, int $M_TYPE):void{
319327
);
320328
}
321329

330+
/**
331+
* Renders the image with the gdimage function for the desired output
332+
*
333+
* @see \imagebmp()
334+
* @see \imagegif()
335+
* @see \imagejpeg()
336+
* @see \imagepng()
337+
* @see \imagewebp()
338+
*
339+
* @todo: v6.0: make abstract and call from child classes
340+
* @see https://github.com/chillerlan/php-qrcode/issues/223
341+
* @codeCoverageIgnore
342+
*/
343+
protected function renderImage():void{
344+
345+
switch($this->options->outputType){
346+
case QROutputInterface::GDIMAGE_BMP:
347+
imagebmp($this->image, null, ($this->options->quality > 0));
348+
break;
349+
case QROutputInterface::GDIMAGE_GIF:
350+
imagegif($this->image);
351+
break;
352+
case QROutputInterface::GDIMAGE_JPG:
353+
imagejpeg($this->image, null, max(-1, min(100, $this->options->quality)));
354+
break;
355+
case QROutputInterface::GDIMAGE_WEBP:
356+
imagewebp($this->image, null, max(-1, min(100, $this->options->quality)));
357+
break;
358+
// silently default to png output
359+
case QROutputInterface::GDIMAGE_PNG:
360+
default:
361+
imagepng($this->image, null, max(-1, min(9, $this->options->quality)));
362+
}
363+
364+
}
365+
322366
/**
323367
* Creates the final image by calling the desired GD output function
324368
*
325369
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
326370
*/
327371
protected function dumpImage():string{
328372
$exception = null;
373+
$imageData = null;
329374

330375
ob_start();
331376

332377
try{
333-
334-
switch($this->options->outputType){
335-
case QROutputInterface::GDIMAGE_BMP:
336-
imagebmp($this->image, null, ($this->options->quality > 0));
337-
break;
338-
case QROutputInterface::GDIMAGE_GIF:
339-
imagegif($this->image);
340-
break;
341-
case QROutputInterface::GDIMAGE_JPG:
342-
imagejpeg($this->image, null, max(-1, min(100, $this->options->quality)));
343-
break;
344-
case QROutputInterface::GDIMAGE_WEBP:
345-
imagewebp($this->image, null, max(-1, min(100, $this->options->quality)));
346-
break;
347-
// silently default to png output
348-
case QROutputInterface::GDIMAGE_PNG:
349-
default:
350-
imagepng($this->image, null, max(-1, min(9, $this->options->quality)));
351-
}
378+
$this->renderImage();
352379

353380
$imageData = ob_get_contents();
354381
imagedestroy($this->image);

src/Output/QRGdImageBMP.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Class QRGdImageBMP
4+
*
5+
* @created 25.10.2023
6+
* @author smiley <[email protected]>
7+
* @copyright 2023 smiley
8+
* @license MIT
9+
*
10+
* @noinspection PhpComposerExtensionStubsInspection
11+
*/
12+
13+
namespace chillerlan\QRCode\Output;
14+
15+
use function imagebmp;
16+
17+
/**
18+
* GdImage bmp output
19+
*
20+
* @see \imagebmp()
21+
*/
22+
class QRGdImageBMP extends QRGdImage{
23+
24+
public const MIME_TYPE = 'image/bmp';
25+
26+
/**
27+
* @inheritDoc
28+
*/
29+
protected function renderImage():void{
30+
imagebmp($this->image, null, ($this->options->quality > 0));
31+
}
32+
33+
}

src/Output/QRGdImageGIF.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Class QRGdImageGIF
4+
*
5+
* @created 25.10.2023
6+
* @author smiley <[email protected]>
7+
* @copyright 2023 smiley
8+
* @license MIT
9+
*
10+
* @noinspection PhpComposerExtensionStubsInspection
11+
*/
12+
13+
namespace chillerlan\QRCode\Output;
14+
15+
use function imagegif;
16+
17+
/**
18+
* GdImage gif output
19+
*
20+
* @see \imagegif()
21+
*/
22+
class QRGdImageGIF extends QRGdImage{
23+
24+
public const MIME_TYPE = 'image/gif';
25+
26+
/**
27+
* @inheritDoc
28+
*/
29+
protected function renderImage():void{
30+
imagegif($this->image);
31+
}
32+
33+
}

src/Output/QRGdImageJPEG.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Class QRGdImageJPEG
4+
*
5+
* @created 25.10.2023
6+
* @author smiley <[email protected]>
7+
* @copyright 2023 smiley
8+
* @license MIT
9+
*
10+
* @noinspection PhpComposerExtensionStubsInspection
11+
*/
12+
13+
namespace chillerlan\QRCode\Output;
14+
15+
use function imagejpeg;
16+
use function max;
17+
use function min;
18+
19+
/**
20+
* GdImage jpeg output
21+
*
22+
* @see \imagejpeg()
23+
*/
24+
class QRGdImageJPEG extends QRGdImage{
25+
26+
public const MIME_TYPE = 'image/jpg';
27+
28+
/**
29+
* @inheritDoc
30+
*/
31+
protected function setTransparencyColor():void{
32+
// noop - transparency is not supported
33+
}
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
protected function renderImage():void{
39+
imagejpeg($this->image, null, max(-1, min(100, $this->options->quality)));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)