Skip to content

Commit

Permalink
Fix #28 bug with LC_NUMERIC locale
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Dec 18, 2016
1 parent 957e1bb commit 291363a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/PictureGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ private function generateSrcsetItem(ImageInterface $image, PictureConfigurationI
$src = [$resizedImage];

if ('x' === $descriptorType) {
$src[1] = round($resizedImage->getDimensions()->getSize()->getWidth() / $width1x, 3).'x';
$srcX = $resizedImage->getDimensions()->getSize()->getWidth() / $width1x;
$src[1] = rtrim(sprintf('%.3F', $srcX), '.0').'x';

This comment has been minimized.

Copy link
@leofeyer

leofeyer Dec 19, 2016

Member

Why not $src[1] = rtrim(sprintf('%.3F', $resizedImage->getDimensions()->getSize()->getWidth() / $width1x), '.0').'x';?

This comment has been minimized.

Copy link
@ausi

ausi Dec 19, 2016

Author Member

Because it’s too long and hard to read.

} elseif ('w' === $descriptorType) {
$src[1] = $resizedImage->getDimensions()->getSize()->getWidth().'w';
}
Expand Down
36 changes: 29 additions & 7 deletions tests/PictureGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ function (

$pictureConfig = (new PictureConfiguration())
->setSize((new PictureConfigurationItem())
->setDensities('1x, 2x')
->setDensities('1x, 1.35354x, 2x')
->setResizeConfig((new ResizeConfiguration())
->setWidth(100)
->setHeight(100)
->setWidth(99)
->setHeight(99)
)
)
->setSizeItems(
Expand Down Expand Up @@ -139,10 +139,10 @@ function (

$this->assertEquals(
[
'src' => 'image-100.jpg',
'width' => '100',
'height' => '100',
'srcset' => 'image-100.jpg 1x, image-200.jpg 2x',
'src' => 'image-99.jpg',
'width' => '99',
'height' => '99',
'srcset' => 'image-99.jpg 1x, image-134.jpg 1.354x, image-198.jpg 2x',
],
$picture->getImg('/root/dir')
);
Expand Down Expand Up @@ -388,6 +388,28 @@ function (ImageInterface $image, ResizeConfigurationInterface $config, ResizeOpt
$this->assertInstanceOf('Contao\Image\PictureInterface', $picture);
}

/**
* Tests the generate() method with a de_DE locale for LC_NUMERIC.
*/
public function testGenerateWithLocale()
{
$locale = setlocale(LC_NUMERIC, 0);

if (false === $locale) {
$this->markTestSkipped('Your platform does not support locales.');
}

try {
$requiredLocales = array('de_DE.UTF-8', 'de_DE.UTF8', 'de_DE.utf-8', 'de_DE.utf8', 'German_Germany.1252');
if (false === setlocale(LC_NUMERIC, $requiredLocales)) {
$this->markTestSkipped('Could not set any of required locales: '.implode(', ', $requiredLocales));
}
$this->testGenerate();
} finally {
setlocale(LC_NUMERIC, $locale);
}
}

/**
* Creates a PictureGenerator instance helper.
*
Expand Down

0 comments on commit 291363a

Please sign in to comment.