|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2018-03-14 12:37 UTC] 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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 21:00:02 2025 UTC | 
> 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>