Skip to content

ext/gd: de-factorize image output functions #14523

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
Jun 13, 2024

Conversation

thg2k
Copy link
Contributor

@thg2k thg2k commented Jun 9, 2024

The image output functions imagegif/imagepng/imagewebp/imageavif/imagejpeg were all calling the static function _php_image_output_ctx which was basically a big switch statement between each image type which also have different parameters.

The only identical part was the call to create_stream_context_from_zval that is now merged with create_output_context.

This greatly increases readability and maintainability of this file.

@thg2k thg2k force-pushed the pr/gd_out_refactor_2 branch from a465d90 to 0db0d51 Compare June 9, 2024 13:37
ext/gd/gd.c Outdated
Comment on lines 1981 to 2044
if (quality < -1 || quality > 100) {
zend_argument_value_error(3, "must be between -1 and 100");
ctx->gd_free(ctx);
RETURN_THROWS();
}
if (speed < -1 || speed > 10) {
zend_argument_value_error(4, "must be between -1 and 10");
ctx->gd_free(ctx);
RETURN_THROWS();
} else if (speed == -1) {
speed = 6;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto those checks should be before the call to php_gd_libgdimageptr_from_zval_p

Comment on lines +2023 to +2080
if (quality < -1 || quality > 100) {
zend_argument_value_error(3, "must be at between -1 and 100");
ctx->gd_free(ctx);
RETURN_THROWS();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

ctx->gd_free(ctx);
RETURN_THROWS();
if (Z_TYPE_P(to_zval) == IS_RESOURCE) {
php_stream_from_zval_no_verify(stream, to_zval);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this no verify? This function should return a TypeError for incorrect resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only moved around this code, so I have no idea why it's no verify. This PR is only about reorganizing the file, it should not contain any functional changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll keep a note for a follow-up then.

@thg2k thg2k force-pushed the pr/gd_out_refactor_2 branch from 0db0d51 to 31c4d5a Compare June 11, 2024 20:52
@thg2k
Copy link
Contributor Author

thg2k commented Jun 11, 2024

I rebased it due to #14534 and added the argument number as parameter

The image output functions imagegif/imagepng/imagewebp/imageavif/imagejpeg
were all calling the static function _php_image_output_ctx which was basically
a big switch statement between each image type which also have different
parameters.

The only identical part was the call to create_stream_context_from_zval that
is now merged with create_output_context.
@thg2k thg2k force-pushed the pr/gd_out_refactor_2 branch from 31c4d5a to df54879 Compare June 11, 2024 21:04
Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM just one tiny nit, but could be added in a follow-up

@thg2k
Copy link
Contributor Author

thg2k commented Jun 13, 2024

LGTM just one tiny nit, but could be added in a follow-up

Sure thing, I added the assertion. From the procedural point of views, in this case that the PR was double approved already, is it better if I amend the first commit or just add a second one and you guys squash it? I think rebasing is out of the question because it's hard to check the changes..

Just to know how to do it in the future 😄

@devnexen
Copy link
Member

does not matter you can squash it or the committer will ;)

@devnexen devnexen merged commit 592d899 into php:master Jun 13, 2024
11 checks passed
@thg2k thg2k deleted the pr/gd_out_refactor_2 branch June 13, 2024 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants