Req #76095 [Sus->Wfx]: GD functions should throw on failure instead of emitting warnings

From: Date: Mon, 02 Aug 2021 10:39:22 +0000
Subject: Req #76095 [Sus->Wfx]: GD functions should throw on failure instead of emitting warnings
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235523@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76095&edit=1 ID: 76095 Updated by: cmb@php.net Reported by: dadrych at gmail dot com Summary: GD functions should throw on failure instead of emitting warnings -Status: Suspended +Status: Wont fix Type: Feature/Change Request Package: GD related Operating System: Ubuntu, Debian PHP Version: Irrelevant -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: Well, nobody came up with an RFC so far, and it's actually unlikely that the image reader functions will be changed to throw; instead it's more likely that creation methods will be added to GdImage, and these *could* throw on failure. Furthermore, set_error_handler() already allows to convert warnings to exceptions, so I'm closing this as WONTFIX. Previous Comments: ------------------------------------------------------------------------ [2018-03-14 15:12:38] dadrych at gmail dot com I used() set_error_handler() function and now it is fine, thank you ------------------------------------------------------------------------ [2018-03-14 14:11:57] cmb@php.net > I try to open jpeg file, but I get warning, which I can not catch. Well, you could use set_error_handler() to catch the warning and throw a custom exception, if desired. Anyway, if imagecreatefromjpeg() fails, it returns NULL, which you should check; for instance, you could do: if (is_null(imagecreatefromjpeg($filename))) { throw new InvalidArgumentException('Image is invalid'); } > Imho better way is throw exception instead trigger error. Almost none of the functions of PHP's bundled extensions throws an exception; changing this would most likely require the RFC process[1]. Anybody is welcome to start it! For the time being, I'm suspending this feature request. > I set gd.jpeg_ignore_warning, but I still get a warning. gd.jpeg_ignore_warning causes warning issued by libjpeg to be suppressed. However, this image raises an unrecoverable error, and this will be reported. [1] <https://wiki.php.net/rfc/howto> ------------------------------------------------------------------------ [2018-03-14 13:01:00] dadrych at gmail dot com Ok, thank you for your answer. I tried to save picture with imagick $imagick = new Imagick(); $imagick->writeImage($file); after that I don't have any warnings with imagecreatefromjpeg and picture is saved correctly, so probably this is a problem with libgd. ------------------------------------------------------------------------ [2018-03-14 12:49:43] daverandom@php.net The image cannot be opened because libgd does not understand it. If you believe the image data is valid and the operation should succeed with this particular file, you will need to raise this upstream in libgd https://libgd.github.io/ I have changed this report to a feature request asking that the GD functions throw on failure instead of emitting errors. ------------------------------------------------------------------------ [2018-03-14 12:37:53] dadrych at gmail dot com Description: ------------ I try to open jpeg file, but I get warning, which I can not catch. Imho better way is throw exception instead trigger error. I set gd.jpeg_ignore_warning, but I still get a warning. https://drive.google.com/file/d/1gkZVOjRQ-9EOctcLCjhJOojtHz5nyYQ6/view Test script: --------------- imagecreatefromjpeg($filename); // warning Expected result: ---------------- try { imagecreatefromjpeg($filename); } catch (Exception $e) { throw new InvalidArgumentException('Image is invalid'); } Actual result: -------------- dawid@asus:~$ php5.6 -v PHP 5.6.34-1+ubuntu16.04.1+deb.sury.org+1 (cli) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies with Xdebug v2.5.5, Copyright (c) 2002-2017, by Derick Rethans dawid@asus:~$ php5.6 -a Interactive mode enabled php > imagecreatefromjpeg('/home/dawid/21_3a38432dc66e33d5afcdba4d225cc73b.jpg'); PHP Warning: imagecreatefromjpeg(): gd-jpeg, libjpeg: recoverable error: Invalid SOS parameters for sequential JPEG in php shell code on line 1 PHP Stack trace: PHP 1. {main}() php shell code:0 PHP 2. imagecreatefromjpeg() php shell code:1 PHP Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: Unsupported marker type 0xbf in php shell code on line 1 PHP Stack trace: PHP 1. {main}() php shell code:0 PHP 2. imagecreatefromjpeg() php shell code:1 PHP Warning: imagecreatefromjpeg(): '/home/dawid/21_3a38432dc66e33d5afcdba4d225cc73b.jpg' is not a valid JPEG file in php shell code on line 1 PHP Stack trace: PHP 1. {main}() php shell code:0 PHP 2. imagecreatefromjpeg() php shell code:1 dawid@asus:~$ php7.0 -v PHP 7.0.28-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Mar 6 2018 10:44:15) ( NTS ) Copyright (c) 1997-2017 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.0.28-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans dawid@asus:~$ php7.0 -a Interactive mode enabled php > imagecreatefromjpeg('/home/dawid/21_3a38432dc66e33d5afcdba4d225cc73b.jpg'); PHP Warning: imagecreatefromjpeg(): gd-jpeg, libjpeg: recoverable error: Invalid SOS parameters for sequential JPEG in php shell code on line 1 PHP Stack trace: PHP 1. {main}() php shell code:0 PHP 2. imagecreatefromjpeg() php shell code:1 PHP Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: Unsupported marker type 0xbf in php shell code on line 1 PHP Stack trace: PHP 1. {main}() php shell code:0 PHP 2. imagecreatefromjpeg() php shell code:1 PHP Warning: imagecreatefromjpeg(): '/home/dawid/21_3a38432dc66e33d5afcdba4d225cc73b.jpg' is not a valid JPEG file in php shell code on line 1 PHP Stack trace: PHP 1. {main}() php shell code:0 PHP 2. imagecreatefromjpeg() php shell code:1 dawid@asus:~$ php7.1 -v PHP 7.1.15-1+ubuntu16.04.1+deb.sury.org+2 (cli) (built: Mar 6 2018 11:10:13) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.1.15-1+ubuntu16.04.1+deb.sury.org+2, Copyright (c) 1999-2018, by Zend Technologies with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans dawid@asus:~$ php7.1 -a Interactive mode enabled php > imagecreatefromjpeg('/home/dawid/21_3a38432dc66e33d5afcdba4d225cc73b.jpg'); PHP Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: Unsupported marker type 0xbf in php shell code on line 1 PHP Stack trace: PHP 1. {main}() php shell code:0 PHP 2. imagecreatefromjpeg() php shell code:1 PHP Warning: imagecreatefromjpeg(): '/home/dawid/21_3a38432dc66e33d5afcdba4d225cc73b.jpg' is not a valid JPEG file in php shell code on line 1 PHP Stack trace: PHP 1. {main}() php shell code:0 PHP 2. imagecreatefromjpeg() php shell code:1 dawid@asus:~$ php7.2 -v PHP 7.2.3-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Mar 6 2018 11:18:25) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.3-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans dawid@asus:~$ php7.2 -a Interactive mode enabled php > imagecreatefromjpeg('/home/dawid/21_3a38432dc66e33d5afcdba4d225cc73b.jpg'); PHP Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: Unsupported marker type 0xbf in php shell code on line 1 PHP Stack trace: PHP 1. {main}() php shell code:0 PHP 2. imagecreatefromjpeg() php shell code:1 PHP Warning: imagecreatefromjpeg(): '/home/dawid/21_3a38432dc66e33d5afcdba4d225cc73b.jpg' is not a valid JPEG file in php shell code on line 1 PHP Stack trace: PHP 1. {main}() php shell code:0 PHP 2. imagecreatefromjpeg() php shell code:1 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76095&edit=1

« previous php.bugs (#235523) next »