Skip to content

fix: Images\Handlers\GDHandler Implicit conversion from float to int loses precision #5965

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 1 commit into from
May 6, 2022
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
2 changes: 1 addition & 1 deletion system/Images/Handlers/GDHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ protected function textOverlay(string $text, array $options = [], bool $isShadow
* Get the rest of the string and split it into 2-length
* hex values:
*/
$opacity = ($options['opacity'] * 127);
$opacity = (int) ($options['opacity'] * 127);
Copy link
Member

Choose a reason for hiding this comment

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

Casting by (int) removes the decimals part. https://3v4l.org/8lXKD
Is that the intended behavior or should we use round()?

Copy link
Member

Choose a reason for hiding this comment

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

Looking at my comment in #5370 (comment), it seems this is to match the current behavior.

Copy link
Member Author

Choose a reason for hiding this comment

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

Frankly, I don't think there is much difference either way.
And yes, this is to match the current behavior.


// Allow opacity to be applied to the text
imagealphablending($src, true);
Expand Down
5 changes: 4 additions & 1 deletion tests/system/Images/GDHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,10 @@ public function testFitPositions()
public function testText()
{
$this->handler->withFile($this->path);
$this->handler->text('vertical', ['hAlign' => 'right', 'vAlign' => 'bottom']);
$this->handler->text(
'vertical',
['hAlign' => 'right', 'vAlign' => 'bottom', 'opacity' => 0.5]
);
$this->assertSame(155, $this->handler->getWidth());
$this->assertSame(200, $this->handler->getHeight());
}
Expand Down