Skip to content

Declare proper parameter default values for imagegd2 #10569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions ext/gd/gd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1720,38 +1720,35 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
{
zval *imgind;
char *file = NULL;
zend_long quality = 0, type = 0;
zend_long quality = 128, type = 1;
gdImagePtr im;
FILE *fp;
size_t file_len = 0;
int argc = ZEND_NUM_ARGS();
int q = -1, t = 1;

/* The quality parameter for gd2 stands for chunk size */

switch (image_type) {
case PHP_GDIMG_TYPE_GD:
if (zend_parse_parameters(argc, "O|p!", &imgind, gd_image_ce, &file, &file_len) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|p!", &imgind, gd_image_ce, &file, &file_len) == FAILURE) {
RETURN_THROWS();
}
break;
case PHP_GDIMG_TYPE_GD2:
if (zend_parse_parameters(argc, "O|p!ll", &imgind, gd_image_ce, &file, &file_len, &quality, &type) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|p!ll", &imgind, gd_image_ce, &file, &file_len, &quality, &type) == FAILURE) {
RETURN_THROWS();
}
break;
EMPTY_SWITCH_DEFAULT_CASE()
}

im = php_gd_libgdimageptr_from_zval_p(imgind);

if (argc >= 3) {
q = quality;
if (argc == 4) {
t = type;
}
/* quality must fit in an int */
if (quality < INT_MIN || quality > INT_MAX) {
php_error_docref(NULL, E_WARNING, "Argument #3 ($chunk_size) must be between %d and %d", INT_MIN, INT_MAX);
RETURN_FALSE;
}

im = php_gd_libgdimageptr_from_zval_p(imgind);

if (file_len) {
PHP_GD_CHECK_OPEN_BASEDIR(file, "Invalid filename");

Expand All @@ -1766,10 +1763,10 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
gdImageGd(im, fp);
break;
case PHP_GDIMG_TYPE_GD2:
if (q == -1) {
q = 128;
if (quality == -1) {
quality = 128;
}
gdImageGd2(im, fp, q, t);
gdImageGd2(im, fp, quality, type);
break;
EMPTY_SWITCH_DEFAULT_CASE()
}
Expand All @@ -1792,10 +1789,10 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, char
gdImageGd(im, tmp);
break;
case PHP_GDIMG_TYPE_GD2:
if (q == -1) {
q = 128;
if (quality == -1) {
quality = 128;
}
gdImageGd2(im, tmp, q, t);
gdImageGd2(im, tmp, quality, type);
break;
EMPTY_SWITCH_DEFAULT_CASE()
}
Expand Down
2 changes: 1 addition & 1 deletion ext/gd/gd.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ function imagewbmp(GdImage $image, $file = null, ?int $foreground_color = null):

function imagegd(GdImage $image, ?string $file = null): bool {}

function imagegd2(GdImage $image, ?string $file = null, int $chunk_size = UNKNOWN, int $mode = UNKNOWN): bool {}
function imagegd2(GdImage $image, ?string $file = null, int $chunk_size = 128, int $mode = IMG_GD2_RAW): bool {}

#ifdef HAVE_GD_BMP
/** @param resource|string|null $file */
Expand Down
6 changes: 3 additions & 3 deletions ext/gd/gd_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.