Skip to content

Conversation

javajawa
Copy link

@javajawa javajawa commented Jul 2, 2020

When the image* output functions, such as imagepng(), are passed null as the filename/to parameter, the raw image data is returned from the function.

This patch adds this signature for:

  • imagebmp
  • imagegd
  • imagegd2
  • imagegif
  • imagejpeg
  • imagepng
  • imagewbmp
  • imagewebp
  • imagexbm
When the image* output functions, such as `imagepng()` are passed `null` as the filename/to parameter, the raw image data is returned from the function. This patch adds this signature for: - `imagebmp` - `imagegd` - `imagegd2` - `imagegif` - `imagejpeg` - `imagepng` - `imagewbmp` - `imagewebp` - `imagexbm`
@javajawa
Copy link
Author

javajawa commented Jul 2, 2020

I do not know what to make of the tests failing specifically in php7.4, nor do I see the connection between the binaryOps test and the changes I've made. Suggestions most welcome.

Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

Yep, the failure is random and unrelated.

'imagegd' => ['bool', 'im'=>'resource', 'filename='=>'string|resource|null'],
'imagegd2' => ['bool', 'im'=>'resource', 'filename='=>'string|resource|null', 'chunk_size='=>'int', 'type='=>'int'],
'imagegd' => ['bool', 'im'=>'resource', 'filename='=>'string|resource'],
'imagegd\'1' => ['string', 'im'=>'resource', 'filename='=>'null'],
Copy link
Member

Choose a reason for hiding this comment

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

Can you test that ParametersAcceptorSelector will figure this out? Add a new data provider to NodeScopeResolverTest::testFileAsserts and see that the right imagegd() calls lead to bool or string with assertType(), thanks.

Ben Harcourt added 2 commits July 2, 2020 15:48
Even when writing the output to the return value, the GD functions are allowed to fail and return `false`. The signatures and tests have been updated to reflect this. In addition, added a specific test for passing a `resource` to `imagegd`.

assertType('bool', imagegd($image, 'php://memory'));
assertType('bool', imagegd($image, $memoryHandle));
assertType('string|false', imagegd($image, null));
Copy link
Member

Choose a reason for hiding this comment

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

Please verify imagegd($image); too, thanks.

When the function is called as `imagepng($image)`, the output is returned, and thus the expected return type is `string|false`. The previous commits had the second parameter optional for both variants, resulting in PHPStan inferring a return of `string|bool`. Additionally, this parameter has been marked as optional for `imagexbm`, in line with the PHP documentation.
@ondrejmirtes ondrejmirtes merged commit d025f01 into phpstan:master Jul 2, 2020
@ondrejmirtes
Copy link
Member

Thank you!

@javajawa
Copy link
Author

javajawa commented Jul 2, 2020

Thank you!

Thank you for the help, hints, and frankly the entire project!

@javajawa javajawa deleted the gd-functions-with-null-filenames branch July 2, 2020 16:29
ondrejmirtes pushed a commit that referenced this pull request Jul 16, 2020
The GD group of image output functions has the capability to not output to a file, but the raw data is not returned by the function. Instead, the function echos the data into the equivelant of `php://output`, and returns a boolean for success or failure. To quote the wording I previously misread on https://www.php.net/manual/en/function.imagepng.php: > The path or an open stream resource [...] to save the file to. > If not set or NULL, the raw image stream will be outputted directly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants