16
16
use chillerlan \QRCode \QROptions ;
17
17
use chillerlan \QRCode \Data \QRMatrix ;
18
18
use chillerlan \Settings \SettingsContainerInterface ;
19
- use ErrorException , GdImage , Throwable ;
19
+ use GdImage ;
20
20
use function extension_loaded , imagecolorallocate , imagecolortransparent ,
21
21
imagecreatetruecolor , imagedestroy , imagefilledellipse , imagefilledrectangle ,
22
22
imagescale , imagetypes , intdiv , intval , max , min , ob_end_clean , ob_get_contents , ob_start ,
23
- restore_error_handler , set_error_handler , sprintf ;
23
+ sprintf ;
24
24
use const IMG_AVIF , IMG_BMP , IMG_GIF , IMG_JPG , IMG_PNG , IMG_WEBP ;
25
25
26
26
/**
@@ -140,17 +140,14 @@ protected function getDefaultModuleValue(bool $isDark):int{
140
140
* @throws \ErrorException|\chillerlan\QRCode\Output\QRCodeOutputException
141
141
*/
142
142
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
-
148
143
$ this ->image = $ this ->createImage ();
149
144
// set module values after image creation because we need the GdImage instance
150
145
$ this ->setModuleValues ();
151
146
$ this ->setBgColor ();
152
147
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
+ }
154
151
155
152
$ this ->drawImage ();
156
153
@@ -173,8 +170,6 @@ public function dump(string|null $file = null):string|GdImage{
173
170
$ this ->setTransparencyColor ();
174
171
175
172
if ($ this ->options ->returnResource ){
176
- restore_error_handler ();
177
-
178
173
return $ this ->image ;
179
174
}
180
175
@@ -186,8 +181,6 @@ public function dump(string|null $file = null):string|GdImage{
186
181
$ imageData = $ this ->toBase64DataURI ($ imageData );
187
182
}
188
183
189
- restore_error_handler ();
190
-
191
184
return $ imageData ;
192
185
}
193
186
@@ -197,6 +190,8 @@ public function dump(string|null $file = null):string|GdImage{
197
190
* we're scaling the image up in order to draw crisp round circles, otherwise they appear square-y on small scales
198
191
*
199
192
* @see https://github.com/chillerlan/php-qrcode/issues/23
193
+ *
194
+ * @throws \chillerlan\QRCode\Output\QRCodeOutputException
200
195
*/
201
196
protected function createImage ():GdImage {
202
197
@@ -207,7 +202,13 @@ protected function createImage():GdImage{
207
202
$ this ->upscaled = true ;
208
203
}
209
204
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 ;
211
212
}
212
213
213
214
/**
@@ -229,12 +230,12 @@ protected function setBgColor():void{
229
230
}
230
231
231
232
/**
232
- * Sets the transparency color
233
+ * Sets the transparency color, returns the identifier of the new transparent color
233
234
*/
234
- protected function setTransparencyColor ():void {
235
+ protected function setTransparencyColor ():int {
235
236
236
237
if (!$ this ->options ->imageTransparent ){
237
- return ;
238
+ return - 1 ;
238
239
}
239
240
240
241
$ transparencyColor = $ this ->background ;
@@ -243,7 +244,14 @@ protected function setTransparencyColor():void{
243
244
$ transparencyColor = $ this ->prepareModuleValue ($ this ->options ->transparencyColor );
244
245
}
245
246
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 ));
247
255
}
248
256
249
257
/**
@@ -304,37 +312,20 @@ abstract protected function renderImage():void;
304
312
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
305
313
*/
306
314
protected function dumpImage ():string {
307
- $ exception = null ;
308
- $ imageData = null ;
309
-
310
315
ob_start ();
311
316
312
- try {
313
- $ this ->renderImage ();
317
+ $ this ->renderImage ();
314
318
315
- $ imageData = ob_get_contents ();
319
+ $ imageData = ob_get_contents ();
316
320
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 ' );
327
323
}
328
- // @codeCoverageIgnoreEnd
329
324
330
- ob_end_clean ( );
325
+ imagedestroy ( $ this -> image );
331
326
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 ();
336
328
337
- /** @var string $imageData */
338
329
return $ imageData ;
339
330
}
340
331
0 commit comments