Skip to content

Commit 7e7552b

Browse files
committed
:octocat: QRGdImage: remove custom error handler
1 parent e51e367 commit 7e7552b

7 files changed

+74
-47
lines changed

src/Output/QRGdImage.php

+31-40
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
use chillerlan\QRCode\QROptions;
1717
use chillerlan\QRCode\Data\QRMatrix;
1818
use chillerlan\Settings\SettingsContainerInterface;
19-
use ErrorException, GdImage, Throwable;
19+
use GdImage;
2020
use function extension_loaded, imagecolorallocate, imagecolortransparent,
2121
imagecreatetruecolor, imagedestroy, imagefilledellipse, imagefilledrectangle,
2222
imagescale, imagetypes, intdiv, intval, max, min, ob_end_clean, ob_get_contents, ob_start,
23-
restore_error_handler, set_error_handler, sprintf;
23+
sprintf;
2424
use const IMG_AVIF, IMG_BMP, IMG_GIF, IMG_JPG, IMG_PNG, IMG_WEBP;
2525

2626
/**
@@ -140,17 +140,14 @@ protected function getDefaultModuleValue(bool $isDark):int{
140140
* @throws \ErrorException|\chillerlan\QRCode\Output\QRCodeOutputException
141141
*/
142142
public function dump(string|null $file = null):string|GdImage{
143-
144-
set_error_handler(function(int $errno, string $errstr):bool{
145-
throw new ErrorException($errstr, $errno);
146-
});
147-
148143
$this->image = $this->createImage();
149144
// set module values after image creation because we need the GdImage instance
150145
$this->setModuleValues();
151146
$this->setBgColor();
152147

153-
imagefilledrectangle($this->image, 0, 0, $this->length, $this->length, $this->background);
148+
if(imagefilledrectangle($this->image, 0, 0, $this->length, $this->length, $this->background) === false){
149+
throw new QRCodeOutputException('imagefilledrectangle() error');
150+
}
154151

155152
$this->drawImage();
156153

@@ -173,8 +170,6 @@ public function dump(string|null $file = null):string|GdImage{
173170
$this->setTransparencyColor();
174171

175172
if($this->options->returnResource){
176-
restore_error_handler();
177-
178173
return $this->image;
179174
}
180175

@@ -186,8 +181,6 @@ public function dump(string|null $file = null):string|GdImage{
186181
$imageData = $this->toBase64DataURI($imageData);
187182
}
188183

189-
restore_error_handler();
190-
191184
return $imageData;
192185
}
193186

@@ -197,6 +190,8 @@ public function dump(string|null $file = null):string|GdImage{
197190
* we're scaling the image up in order to draw crisp round circles, otherwise they appear square-y on small scales
198191
*
199192
* @see https://github.com/chillerlan/php-qrcode/issues/23
193+
*
194+
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
200195
*/
201196
protected function createImage():GdImage{
202197

@@ -207,7 +202,13 @@ protected function createImage():GdImage{
207202
$this->upscaled = true;
208203
}
209204

210-
return imagecreatetruecolor($this->length, $this->length);
205+
$im = imagecreatetruecolor($this->length, $this->length);
206+
207+
if($im === false){
208+
throw new QRCodeOutputException('imagecreatetruecolor() error');
209+
}
210+
211+
return $im;
211212
}
212213

213214
/**
@@ -229,12 +230,12 @@ protected function setBgColor():void{
229230
}
230231

231232
/**
232-
* Sets the transparency color
233+
* Sets the transparency color, returns the identifier of the new transparent color
233234
*/
234-
protected function setTransparencyColor():void{
235+
protected function setTransparencyColor():int{
235236

236237
if(!$this->options->imageTransparent){
237-
return;
238+
return -1;
238239
}
239240

240241
$transparencyColor = $this->background;
@@ -243,7 +244,14 @@ protected function setTransparencyColor():void{
243244
$transparencyColor = $this->prepareModuleValue($this->options->transparencyColor);
244245
}
245246

246-
imagecolortransparent($this->image, $transparencyColor);
247+
return imagecolortransparent($this->image, $transparencyColor);
248+
}
249+
250+
/**
251+
* Returns the image quality value for the current GdImage output child class (defaults to -1 ... 100)
252+
*/
253+
protected function getQuality():int{
254+
return max(-1, min(100, $this->options->quality));
247255
}
248256

249257
/**
@@ -304,37 +312,20 @@ abstract protected function renderImage():void;
304312
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
305313
*/
306314
protected function dumpImage():string{
307-
$exception = null;
308-
$imageData = null;
309-
310315
ob_start();
311316

312-
try{
313-
$this->renderImage();
317+
$this->renderImage();
314318

315-
$imageData = ob_get_contents();
319+
$imageData = ob_get_contents();
316320

317-
if($imageData === false){
318-
throw new QRCodeOutputException('ob_get_contents() error');
319-
}
320-
321-
imagedestroy($this->image);
322-
}
323-
// not going to cover edge cases
324-
// @codeCoverageIgnoreStart
325-
catch(Throwable $e){
326-
$exception = $e;
321+
if($imageData === false){
322+
throw new QRCodeOutputException('ob_get_contents() error');
327323
}
328-
// @codeCoverageIgnoreEnd
329324

330-
ob_end_clean();
325+
imagedestroy($this->image);
331326

332-
// throw here in case an exception happened within the output buffer
333-
if($exception instanceof Throwable){
334-
throw new QRCodeOutputException($exception->getMessage());
335-
}
327+
ob_end_clean();
336328

337-
/** @var string $imageData */
338329
return $imageData;
339330
}
340331

src/Output/QRGdImageAVIF.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ class QRGdImageAVIF extends QRGdImage{
2424

2525
final public const MIME_TYPE = 'image/avif';
2626

27+
/**
28+
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
29+
*/
2730
protected function renderImage():void{
28-
imageavif($this->image, null, max(-1, min(100, $this->options->quality)));
31+
if(imageavif(image: $this->image, quality: $this->getQuality()) === false){
32+
throw new QRCodeOutputException('imageavif() error');
33+
}
2934
}
3035

3136
}

src/Output/QRGdImageBMP.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ class QRGdImageBMP extends QRGdImage{
2424

2525
final public const MIME_TYPE = 'image/bmp';
2626

27+
/**
28+
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
29+
*/
2730
protected function renderImage():void{
28-
imagebmp($this->image, null, ($this->options->quality > 0));
31+
// the $compressed parameter is boolean here
32+
if(imagebmp(image: $this->image, compressed: ($this->options->quality > 0)) === false){
33+
throw new QRCodeOutputException('imagebmp() error');
34+
}
2935
}
3036

3137
}

src/Output/QRGdImageGIF.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ class QRGdImageGIF extends QRGdImage{
2424

2525
final public const MIME_TYPE = 'image/gif';
2626

27+
/**
28+
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
29+
*/
2730
protected function renderImage():void{
28-
imagegif($this->image);
31+
if(imagegif(image: $this->image) === false){
32+
throw new QRCodeOutputException('imagegif() error');
33+
}
2934
}
3035

3136
}

src/Output/QRGdImageJPEG.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@ class QRGdImageJPEG extends QRGdImage{
2424

2525
final public const MIME_TYPE = 'image/jpg';
2626

27-
protected function setTransparencyColor():void{
27+
protected function setTransparencyColor():int{
2828
// noop - transparency is not supported
29+
return -1;
2930
}
3031

32+
/**
33+
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
34+
*/
3135
protected function renderImage():void{
32-
imagejpeg($this->image, null, max(-1, min(100, $this->options->quality)));
36+
if(imagejpeg(image: $this->image, quality: $this->getQuality()) === false){
37+
throw new QRCodeOutputException('imagejpeg() error');
38+
}
3339
}
3440

3541
}

src/Output/QRGdImagePNG.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,17 @@ class QRGdImagePNG extends QRGdImage{
2424

2525
final public const MIME_TYPE = 'image/png';
2626

27+
protected function getQuality():int{
28+
return max(-1, min(9, $this->options->quality));
29+
}
30+
31+
/**
32+
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
33+
*/
2734
protected function renderImage():void{
28-
imagepng($this->image, null, max(-1, min(9, $this->options->quality)));
35+
if(imagepng(image: $this->image, quality: $this->getQuality()) === false){
36+
throw new QRCodeOutputException('imagepng() error');
37+
}
2938
}
3039

3140
}

src/Output/QRGdImageWEBP.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ class QRGdImageWEBP extends QRGdImage{
2424

2525
final public const MIME_TYPE = 'image/webp';
2626

27+
/**
28+
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
29+
*/
2730
protected function renderImage():void{
28-
imagewebp($this->image, null, max(-1, min(100, $this->options->quality)));
31+
if(imagewebp(image: $this->image, quality: $this->getQuality()) === false){
32+
throw new QRCodeOutputException('imagewebp() error');
33+
}
2934
}
3035

3136
}

0 commit comments

Comments
 (0)