|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #76235 imagejpeg does not always return false on error
Submitted: 2018-04-18 12:50 UTC Modified: 2018-04-18 16:26 UTC
From: inkredibl at gmail dot com Assigned: cmb (profile)
Status: Closed Package: GD related
PHP Version: 7.1.16 OS: Mac
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: inkredibl at gmail dot com
New email:
PHP Version: OS:


 [2018-04-18 12:50 UTC] inkredibl at gmail dot com
Whenever out of space situation happens on a drive imagejpeg() does not necessarily return false when it fails to write the image to disk. This creates a problem when you need to handle errors since no error can be detected in this case. I'm fine with it not working all the time but the documentation needs to mention this somewhere. Fixing this would also be nice but still it needs to be mentioned that this is going on so that people can write valid code.

If you could get a disk with tiny amounts of available space and save images on it this will happen whenever there's not enough space on the drive to save the whole image.

The workaround is to use:
    imagejpeg($img, null);
    $content = ob_get_clean();
    $result = (file_put_contents('file.jpg', $content) == strlen($content));

Test script:
$img = imagecreate(4000, 4000);
$result = imagejpeg($img, 'file.jpg', 100);
// $result will be true even if the image is truncated or in some cases even totally empty (my guess is it depends on the file system)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-04-18 14:29 UTC]
-Status: Open +Status: Analyzed -Package: Documentation problem +Package: GD related -Assigned To: +Assigned To: cmb
 [2018-04-18 14:29 UTC]
Indeed, this is the case for (almost?) all image output functions.
The main reason is that libgd doesn't provide a useful return
value[1], so the PHP bindings don't know whether the function
succeeded or not.

[1] e.g. <>
 [2018-04-18 16:26 UTC]
Automatic comment from SVN on behalf of cmb
Log: Fix bug #76235: imagejpeg does not always return false on error
 [2018-04-18 16:26 UTC]
-Status: Analyzed +Status: Closed
 [2018-04-18 16:26 UTC]
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2020-02-07 06:05 UTC]
Automatic comment on behalf of cmb
Log: Fix bug #76235: imagejpeg does not always return false on error
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 22 02:01:28 2024 UTC