Skip to content
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

Fix small pixel values for aspect ratio size #24

Merged
merged 2 commits into from
Jun 28, 2020
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

* Fix small pixel values for aspect ratio size. [#24]

## [1.0.0] (2020-06-13)

Expand Down Expand Up @@ -79,6 +80,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
[0.1.1]: https://github.com/contao/imagine-svg/compare/0.1.0...0.1.1
[0.1.0]: https://github.com/contao/imagine-svg/commits/0.1.0

[#24]: https://github.com/contao/imagine-svg/issues/24
[#21]: https://github.com/contao/imagine-svg/issues/21
[#19]: https://github.com/contao/imagine-svg/issues/19
[#15]: https://github.com/contao/imagine-svg/issues/15
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,3 @@ rules:

parameters:
ignoreErrors:
-
message: '#undefined method DOMNode::((has|get|set)Attribute|getElementsByTagName)\(\)#'
path: tests/EffectsTest.php
29 changes: 3 additions & 26 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,34 +341,11 @@ public function usePalette(PaletteInterface $palette): self
*/
private function normalizeRatio(float $a, float $b): array
{
if ((float) (int) $a !== $a || (float) (int) $b !== $b) {
$maxDecimals = max($this->getFloatMultiplier($a), $this->getFloatMultiplier($b));
$a *= 10 ** $maxDecimals;
$b *= 10 ** $maxDecimals;

while ($a > PHP_INT_MAX || $b > PHP_INT_MAX) {
$a /= 10;
$b /= 10;
}
if ($a < $b) {
return [(int) round($a * 65535 / $b), 65535];
}

$divisor = $this->getGreatestCommonDivisor((int) round($a), (int) round($b));

return [(int) ((int) $a / $divisor), (int) ((int) $b / $divisor)];
}

private function getFloatMultiplier(float $number): int
{
return ini_get('precision') - 1 - (int) explode('e', sprintf('%.0e', $number))[1];
}

private function getGreatestCommonDivisor(int $a, int $b): int
{
if (0 === $b) {
return $a;
}

return $this->getGreatestCommonDivisor($b, $a % $b);
return [65535, (int) round($b * 65535 / $a)];
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,12 @@ public function testGetSize(): void
$svg->removeAttribute('height');

$this->assertSame(SvgBox::TYPE_ASPECT_RATIO, $image->getSize()->getType());
$this->assertSame(2.0, (float) ($image->getSize()->getWidth() / $image->getSize()->getHeight()));
$this->assertSame(2.0, round($image->getSize()->getWidth() / $image->getSize()->getHeight(), 4));

$svg->setAttribute('viewBox', '0 0 1 0.5');

$this->assertSame(SvgBox::TYPE_ASPECT_RATIO, $image->getSize()->getType());
$this->assertSame(2.0, (float) ($image->getSize()->getWidth() / $image->getSize()->getHeight()));
$this->assertSame(2.0, round($image->getSize()->getWidth() / $image->getSize()->getHeight(), 4));

$svg->removeAttribute('viewBox');

Expand Down Expand Up @@ -402,7 +402,7 @@ public function testGetSizeAspectRatio(string $viewBox, float $ratio): void
$svg->setAttribute('viewBox', $viewBox);

$this->assertSame(SvgBox::TYPE_ASPECT_RATIO, $image->getSize()->getType());
$this->assertSame($ratio, (float) ($image->getSize()->getWidth() / $image->getSize()->getHeight()));
$this->assertSame(round($ratio, 3), round($image->getSize()->getWidth() / $image->getSize()->getHeight(), 3));
}

public function getGetSizeAspectRatio(): array
Expand All @@ -418,7 +418,7 @@ public function getGetSizeAspectRatio(): array
['0 0 1777777777777777777777777777777777777777 1000000000000000000000000000000000000000', 16 / 9],
['0 0 1.77777777777777777e50 1e50', 16 / 9],
['0 0 1.77777777777777777e-50 1e-50', 16 / 9],
['0 0 10e5 10e4', 10.0 / 1],
['0 0 13e5 10e4', 13.0 / 1],
];
}

Expand Down