1616use chillerlan \QRCode \QROptions ;
1717use chillerlan \QRCode \Data \QRMatrix ;
1818use chillerlan \Settings \SettingsContainerInterface ;
19- use ErrorException , GdImage , Throwable ;
19+ use GdImage ;
2020use 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 ;
2424use 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
0 commit comments