Doc #76235 [Opn->Ana]: imagejpeg does not always return false on error

From: Date: Wed, 18 Apr 2018 14:29:31 +0000
Subject: Doc #76235 [Opn->Ana]: imagejpeg does not always return false on error
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-15603@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76235&edit=1 ID: 76235 Updated by: cmb@php.net Reported by: inkredibl at gmail dot com Summary: imagejpeg does not always return false on error -Status: Open +Status: Analyzed Type: Documentation Problem -Package: Documentation problem +Package: GD related Operating System: Mac PHP Version: 7.1.16 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: 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. <https://github.com/libgd/libgd/issues/347> Previous Comments: ------------------------------------------------------------------------ [2018-04-18 12:50:18] inkredibl at gmail dot com Description: ------------ 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: ob_start(); imagejpeg($img, null); $content = ob_get_clean(); $result = (file_put_contents('file.jpg', $content) == strlen($content)); Test script: --------------- <?php $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) var_export($result) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76235&edit=1

« previous php.doc.bugs (#15603) next »