Skip to content

Commit dd9959b

Browse files
committed
:octocat: convert errors to exceptions in GD output
1 parent 9b54d1d commit dd9959b

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/Output/QRImage.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
use chillerlan\QRCode\Data\QRMatrix;
1616
use chillerlan\QRCode\QRCode;
1717
use chillerlan\Settings\SettingsContainerInterface;
18-
use Exception;
18+
use ErrorException, Exception;
1919

2020
use function array_values, count, extension_loaded, imagecolorallocate, imagecolortransparent, imagecreatetruecolor,
2121
imagedestroy, imagefilledellipse, imagefilledrectangle, imagegif, imagejpeg, imagepng, imagescale, in_array,
22-
is_array, ob_end_clean, ob_get_contents, ob_start, range;
22+
is_array, ob_end_clean, ob_get_contents, ob_start, range, restore_error_handler, set_error_handler;
2323
use const IMG_BICUBIC;
2424

2525
/**
@@ -90,8 +90,15 @@ protected function setModuleValues():void{
9090
* @return string|resource|\GdImage
9191
*
9292
* @phan-suppress PhanUndeclaredTypeReturnType, PhanTypeMismatchReturn
93+
* @throws \ErrorException
9394
*/
9495
public function dump(string $file = null){
96+
97+
/** @phan-suppress-next-line PhanTypeMismatchArgumentInternal */
98+
set_error_handler(function(int $severity, string $msg, string $file, int $line):void{
99+
throw new ErrorException($msg, 0, $severity, $file, $line);
100+
});
101+
95102
$file ??= $this->options->cachefile;
96103

97104
// we're scaling the image up in order to draw crisp round circles, otherwise they appear square-y on small scales
@@ -126,6 +133,8 @@ public function dump(string $file = null){
126133
}
127134

128135
if($this->options->returnResource){
136+
restore_error_handler();
137+
129138
return $this->image;
130139
}
131140

@@ -139,6 +148,8 @@ public function dump(string $file = null){
139148
$imageData = $this->base64encode($imageData, 'image/'.$this->options->outputType);
140149
}
141150

151+
restore_error_handler();
152+
142153
return $imageData;
143154
}
144155

0 commit comments

Comments
 (0)