Skip to content

Conversation

glodov
Copy link

@glodov glodov commented Jan 25, 2023

Fix for
The error is thrown in the HumHub project:

PHP Deprecated Warning 'yii\base\ErrorException' with message 'Implicit conversion from float-string "42.95454545454545" to int loses precision'

in /www/wwwroot/yta.yaro.info/protected/vendor/imagine/imagine/src/Imagick/Image.php:164

Stack trace:
#0 [internal function]: yii\base\ErrorHandler->handleError() #1 /www/wwwroot/yta.yaro.info/protected/vendor/imagine/imagine/src/Imagick/Image.php(164): Imagick->cropImage() #2 /www/wwwroot/yta.yaro.info/protected/humhub/libs/ProfileImage.p...",

The error is thrown in the HumHub project:

PHP Deprecated Warning 'yii\\base\\ErrorException' with message 'Implicit conversion from float-string "42.95454545454545" to int loses precision' 

in /www/wwwroot/yta.yaro.info/protected/vendor/imagine/imagine/src/Imagick/Image.php:164

Stack trace:
#0 [internal function]: yii\\base\\ErrorHandler->handleError()
php-imagine#1 /www/wwwroot/yta.yaro.info/protected/vendor/imagine/imagine/src/Imagick/Image.php(164): Imagick->cropImage()
php-imagine#2 /www/wwwroot/yta.yaro.info/protected/humhub/libs/ProfileImage.p...",
@ausi
Copy link
Collaborator

ausi commented Jan 25, 2023

How can this error be reproduced?
$size and $start should always contain integer values.

@glodov
Copy link
Author

glodov commented Mar 10, 2023

How can this error be reproduced? $size and $start should always contain integer values.

I just had it with PHP version 8.
It seems some functions send numbers as string values.

@ausi
Copy link
Collaborator

ausi commented Mar 13, 2023

I just had it with PHP version 8.
It seems some functions send numbers as string values.

I was not able to reproduce that.
How does your code in humhub/libs/ProfileImage.p... look like? Maybe the issue has its cause there.

@glodov
Copy link
Author

glodov commented Mar 20, 2023

I just had it with PHP version 8.
It seems some functions send numbers as string values.

I was not able to reproduce that. How does your code in humhub/libs/ProfileImage.p... look like? Maybe the issue has its cause there.

I found this issue in HumHub project: https://www.humhub.com/.
When you try to upload an image to your space and change the size and position it throws an error.

You can also try to pass a string as a number '8.123' or just float the number '8.123'.
PHP8 is a strict language by default.

@ausi
Copy link
Collaborator

ausi commented Mar 21, 2023

You can also try to pass a string as a number '8.123' or just float the number '8.123'.

I think I see the problem now:

$box = new Box('123.45', '123.45');
$point = new Point('123.45', '123.45');

var_dump($box->getWidth()); // int(123)
var_dump($point->getX()); // string(6) "123.45"

So I think this needs to be fixed in the Point class similar to how it is handled in the Box class:

if (!\is_int($width)) {
$width = (int) round($width);
}
if (!\is_int($height)) {
$height = (int) round($height);
}

Point issue with non Integer values.
Link: php-imagine#843
@glodov
Copy link
Author

glodov commented Mar 21, 2023

thanks @ausi!
@mlocati can you apply the pull request?

*/
public function __construct($x, $y)
{
if (!\is_int($x)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we pass wrong arguments, they will be silently converted to 0.

I'd instead do something like

        if (!is_numeric($x)) {
            throw new InvalidArgumentException('x must be numeric');
        }
        $x = (int) $x;
        if (!is_numeric($y)) {
            throw new InvalidArgumentException('y must be numeric');
        }
        $y = (int) $y;

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think for better consistency, this should be handled the same way as Box class does:

if (!\is_int($width)) {
$width = (int) round($width);
}
if (!\is_int($height)) {
$height = (int) round($height);
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

With the Box class it's a bit easier, since it can't accept a width or a height with a value of 0 (so, an exception will be thrown if we pass a non-numeric value to it).
But I do agree that we should use round.

So, what about something like this?

if (!is_numeric($x)) {
    throw new InvalidArgumentException('x must be numeric');
}
$x = (int) round((float) $x));

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, I didn’t think of the 0 case.
So +1 for you latest code suggestion 👍

@mlocati
Copy link
Collaborator

mlocati commented Jun 7, 2023

Superseeded by #847

@mlocati mlocati closed this Jun 7, 2023
@glodov glodov deleted the patch-1 branch July 13, 2023 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants