Skip to content

Commit 925dcc3

Browse files
committed
ext/gd: imagewebp/imageavif/imagepng/imagejpeg stricter checks quality/speed.
1 parent 0d913d0 commit 925dcc3

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

ext/gd/gd.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4108,27 +4108,50 @@ static void _php_image_output_ctx(INTERNAL_FUNCTION_PARAMETERS, int image_type,
41084108
switch (image_type) {
41094109
#ifdef HAVE_GD_JPG
41104110
case PHP_GDIMG_TYPE_JPG:
4111+
if (quality < -1 || quality > 100) {
4112+
zend_argument_value_error(3, "must be at between -1 and 100");
4113+
ctx->gd_free(ctx);
4114+
RETURN_THROWS();
4115+
}
41114116
gdImageJpegCtx(im, ctx, (int) quality);
41124117
break;
41134118
#endif
41144119
#ifdef HAVE_GD_WEBP
41154120
case PHP_GDIMG_TYPE_WEBP:
4116-
if (quality == -1) {
4121+
if (quality < -1) {
4122+
zend_argument_value_error(3, "must be at least -1");
4123+
ctx->gd_free(ctx);
4124+
RETURN_THROWS();
4125+
} else if (quality == -1) {
41174126
quality = 80;
41184127
}
41194128
gdImageWebpCtx(im, ctx, (int) quality);
41204129
break;
41214130
#endif
41224131
#ifdef HAVE_GD_AVIF
41234132
case PHP_GDIMG_TYPE_AVIF:
4124-
if (speed == -1) {
4133+
if (quality < -1 || quality > 100) {
4134+
zend_argument_value_error(3, "must be between -1 and 100");
4135+
ctx->gd_free(ctx);
4136+
RETURN_THROWS();
4137+
}
4138+
if (speed < -1 || speed > 10) {
4139+
zend_argument_value_error(4, "must be between -1 and 10");
4140+
ctx->gd_free(ctx);
4141+
RETURN_THROWS();
4142+
} else if (speed == -1) {
41254143
speed = 6;
41264144
}
41274145
gdImageAvifCtx(im, ctx, (int) quality, (int) speed);
41284146
break;
41294147
#endif
41304148
#ifdef HAVE_GD_PNG
41314149
case PHP_GDIMG_TYPE_PNG:
4150+
if (quality < -1 || quality > 9) {
4151+
zend_argument_value_error(3, "must be between -1 and 9");
4152+
ctx->gd_free(ctx);
4153+
RETURN_THROWS();
4154+
}
41324155
#ifdef HAVE_GD_BUNDLED
41334156
gdImagePngCtxEx(im, ctx, (int) quality, (int) basefilter);
41344157
#else

ext/gd/tests/avif_decode_encode.phpt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,19 @@ gd
3636
echo_status(imageavif($img, $outfile, -1));
3737

3838
echo 'Encoding AVIF with illegal quality: ';
39-
echo_status(imageavif($img, $outfile, 1234));
39+
try {
40+
imageavif($img, $outfile, 1234);
41+
} catch (\ValueError $e) {
42+
echo $e->getMessage() . PHP_EOL;
43+
}
4044

4145
echo 'Encoding AVIF with illegal speed: ';
42-
echo_status(imageavif($img, $outfile, 70, 1234));
46+
47+
try {
48+
imageavif($img, $outfile, 70, 1234);
49+
} catch (\ValueError $e) {
50+
echo $e->getMessage() . PHP_EOL;
51+
}
4352

4453
echo 'Encoding AVIF losslessly... ';
4554
echo_status(imageavif($img, $outfile, 100, 0));
@@ -66,8 +75,8 @@ Default AVIF encoding: ok
6675
Encoding AVIF at quality 70: ok
6776
Encoding AVIF at quality 70 with speed 5: ok
6877
Encoding AVIF with default quality: ok
69-
Encoding AVIF with illegal quality: ok
70-
Encoding AVIF with illegal speed: ok
78+
Encoding AVIF with illegal quality: imageavif(): Argument #3 ($quality) must be between -1 and 100
79+
Encoding AVIF with illegal speed: imageavif(): Argument #4 ($speed) must be between -1 and 10
7180
Encoding AVIF losslessly... ok
7281
Decoding the AVIF we just wrote...
7382
How many pixels are different in the two images? 0

ext/gd/tests/imageresolution_jpeg.phpt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ imageresolution($exp, 71, 299);
2222
imagejpeg($exp, $filename);
2323
$act = imagecreatefromjpeg($filename);
2424
var_dump(imageresolution($act));
25+
imageresolution($exp, 71, 299);
26+
27+
try {
28+
imagejpeg($exp, $filename, 101);
29+
} catch (\ValueError $e) {
30+
echo $e->getMessage();
31+
}
2532
?>
2633
--EXPECT--
2734
array(2) {
@@ -36,6 +43,7 @@ array(2) {
3643
[1]=>
3744
int(299)
3845
}
46+
imagejpeg(): Argument #3 ($quality) must be at between -1 and 100
3947
--CLEAN--
4048
<?php
4149
@unlink(__DIR__ . DIRECTORY_SEPARATOR . 'imageresolution_jpeg.jpeg');

ext/gd/tests/pngcomp.phpt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,20 @@ gd
1212
<?php
1313
$cwd = __DIR__;
1414

15-
echo "PNG compression test: ";
1615

1716
$im = imagecreatetruecolor(20,20);
1817
imagefilledrectangle($im, 5,5, 10,10, 0xffffff);
18+
try {
19+
imagepng($im, $cwd . '/test_pngcomp.png', -2);
20+
} catch (\ValueError $e) {
21+
echo $e->getMessage() . PHP_EOL;
22+
}
23+
try {
24+
imagepng($im, $cwd . '/test_pngcomp.png', 10);
25+
} catch (\ValueError $e) {
26+
echo $e->getMessage() . PHP_EOL;
27+
}
28+
echo "PNG compression test: ";
1929
imagepng($im, $cwd . '/test_pngcomp.png', 9);
2030

2131
$im2 = imagecreatefrompng($cwd . '/test_pngcomp.png');
@@ -27,4 +37,6 @@ gd
2737
@unlink($cwd . "/test_pngcomp.png");
2838
?>
2939
--EXPECT--
40+
imagepng(): Argument #3 ($quality) must be between -1 and 9
41+
imagepng(): Argument #3 ($quality) must be between -1 and 9
3042
PNG compression test: ok

ext/gd/tests/webp_basic.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ $im_lossless = imagecreatefromwebp($filename);
3737
echo 'Does lossless conversion work? ';
3838
var_dump(calc_image_dissimilarity($im1, $im_lossless) == 0);
3939

40+
try {
41+
imagewebp($im1, $filename, -10);
42+
} catch (\ValueError $e) {
43+
echo $e->getMessage();
44+
}
45+
4046
?>
4147
--CLEAN--
4248
<?php
@@ -45,3 +51,4 @@ var_dump(calc_image_dissimilarity($im1, $im_lossless) == 0);
4551
--EXPECT--
4652
Is lossy conversion close enough? bool(true)
4753
Does lossless conversion work? bool(true)
54+
imagewebp(): Argument #3 ($quality) must be at least -1

0 commit comments

Comments
 (0)