php-src icon indicating copy to clipboard operation
php-src copied to clipboard

image output: throw ValueError on out-of-bounds qualities

Open steffdimitrov opened this issue 4 years ago • 4 comments

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

steffdimitrov avatar Feb 03 '22 14:02 steffdimitrov

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.

cmb69 avatar Feb 03 '22 15:02 cmb69

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...

steffdimitrov avatar Feb 04 '22 06:02 steffdimitrov

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.

cmb69 avatar Feb 04 '22 09:02 cmb69

Hah, already happened for PHP 8.4: https://github.com/php/php-src/commit/7b2ca071c5d8d14582d61b5b046d81198fc43770

cmb69 avatar Feb 04 '25 20:02 cmb69