image output: throw ValueError on out-of-bounds qualities
Description
The following code:
<?php
ob_start();
imagepng($resource, null, 98);
$contents = ob_get_clean();
ob_end_flush();
var_dump($contents);
Resulted in this output:
string(0);
But I expected this output instead:
ERROR
or:
$content = <<<Binary data here >>>
Tested on php 7.3 but I think it is also applicable for other version using gd.
PHP Version
PHP 7.3.33
Operating System
Ubuntu 20.04
The $quality must be in range 0 to 9. If you pass 98, you get a warning: "imagepng(): gd-png error: compression level must be 0 through 9", and the function returs false, which is converted to an empty string.
Yes, I saw that my warnings/notices were turned off. But still, IMO passing data outside wanted range must be an error. Also, imagejpeg wants number between 0 and 100, imagepng wants number between 0 and 9, imagegif doesn't have quality parameter...
It might make sense to throw a ValueError for out-of-bounds qualitites, but for BC reasons, that would have to wait until PHP 9, and might require an RFC.
Hah, already happened for PHP 8.4: https://github.com/php/php-src/commit/7b2ca071c5d8d14582d61b5b046d81198fc43770