php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #76095 GD functions should throw on failure instead of emitting warnings
Submitted: 2018-03-14 12:37 UTC Modified: 2018-03-14 14:11 UTC
From: dadrych at gmail dot com Assigned:
Status: Suspended Package: GD related
PHP Version: Irrelevant OS: Ubuntu, Debian
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-03-14 12:49 UTC] daverandom@php.net
-Summary: Warning: imagecreatefromjpeg(): 'img.jpg' is not a valid JPEG file +Summary: GD functions should throw on failure instead of emitting warnings -Type: Bug +Type: Feature/Change Request
 [2018-03-14 12:49 UTC] 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 13:01 UTC] 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 14:11 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2018-03-14 14:11 UTC] 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 15:12 UTC] dadrych at gmail dot com
I used() set_error_handler() function and now it is fine, thank you
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Wed Nov 14 17:01:26 2018 UTC