Skip to content

Commit 3f5dccc

Browse files
authored
fix: Default x and y to 0 in ImageRenderer (#206)
Fixes #189
1 parent f6d501c commit 3f5dccc

5 files changed

Lines changed: 52 additions & 14 deletions

File tree

‎src/Rasterization/Renderers/ImageRenderer.php‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ public function render(SVGRasterizer $rasterizer, array $options, SVGNode $conte
2525
{
2626
$transform = $rasterizer->getCurrentTransform();
2727

28-
$x = $options['x'];
29-
$y = $options['y'];
28+
$x = $options['x'] ?? 0;
29+
$y = $options['y'] ?? 0;
3030
$transform->map($x, $y);
3131

32-
$width = $options['width'];
33-
$height = $options['height'];
32+
// TODO support "auto" values for width and height
33+
34+
$width = $options['width'] ?? 0;
35+
$height = $options['height'] ?? 0;
36+
if ($width <= 0 || $height <= 0) {
37+
return;
38+
}
3439
$transform->resize($width, $height);
3540

3641
$image = $rasterizer->getImage();

‎tests/Rasterization/Renderers/ImageRendererTest.php‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace SVG;
44

5+
use AssertGD\GDSimilarityConstraint;
56
use SVG\Rasterization\Renderers\ImageRenderer;
67
use SVG\Nodes\SVGNode;
8+
use SVG\Rasterization\SVGRasterizer;
79

810
class SVGNodeClass extends SVGNode
911
{
@@ -27,17 +29,39 @@ class ImageRendererTest extends \PHPUnit\Framework\TestCase
2729
{
2830
public function testRender()
2931
{
30-
$rast = new \SVG\Rasterization\SVGRasterizer(10, 20, null, 100, 200);
31-
$options = [
32-
'href' => __DIR__ . '/../../sample.svg',
33-
'x' => 10.5,
34-
'y' => 10.5,
35-
'width' => 100.5,
36-
'height' => 100.5
37-
];
38-
$svgImageRender = new ImageRenderer();
32+
$obj = new ImageRenderer();
33+
3934
$context = new SVGNodeClass();
4035

41-
$this->assertNull($svgImageRender->render($rast, $options, $context));
36+
$rasterizer = new SVGRasterizer(10, 10, null, 100, 100);
37+
$obj->render($rasterizer, [
38+
'href' => __DIR__ . '/../../squares.svg',
39+
'x' => 1,
40+
'y' => 2,
41+
'width' => 6,
42+
'height' => 6
43+
], $context);
44+
$img = $rasterizer->finish();
45+
46+
$this->assertThat($img, new GDSimilarityConstraint('./tests/images/renderer-image.png'));
47+
}
48+
49+
public function testDefaultsXAndYToZero()
50+
{
51+
$obj = new ImageRenderer();
52+
53+
$context = new SVGNodeClass();
54+
55+
$rasterizer = new SVGRasterizer('10px', '10px', null, 100, 100);
56+
$obj->render($rasterizer, [
57+
'href' => __DIR__ . '/../../squares.svg',
58+
'x' => null,
59+
'y' => null,
60+
'width' => 6,
61+
'height' => 6
62+
], $context);
63+
$img = $rasterizer->finish();
64+
65+
$this->assertThat($img, new GDSimilarityConstraint('./tests/images/renderer-image-x0y0.png'));
4266
}
4367
}
289 Bytes
Loading

‎tests/images/renderer-image.png‎

295 Bytes
Loading

‎tests/squares.svg‎

Lines changed: 9 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)