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

Add viewBox support to rasterization #22

Merged
merged 6 commits into from
Jan 2, 2017
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
4 changes: 2 additions & 2 deletions src/Rasterization/Renderers/SVGEllipseRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class SVGEllipseRenderer extends SVGRenderer
protected function prepareRenderParams(SVGRasterizer $rasterizer, array $options)
{
return array(
'cx' => self::prepareLengthX($options['cx'], $rasterizer),
'cy' => self::prepareLengthY($options['cy'], $rasterizer),
'cx' => self::prepareLengthX($options['cx'], $rasterizer) + $rasterizer->getOffsetX(),
'cy' => self::prepareLengthY($options['cy'], $rasterizer) + $rasterizer->getOffsetY(),
'width' => self::prepareLengthX($options['rx'], $rasterizer) * 2,
'height' => self::prepareLengthY($options['ry'], $rasterizer) * 2,
);
Expand Down
11 changes: 7 additions & 4 deletions src/Rasterization/Renderers/SVGLineRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ class SVGLineRenderer extends SVGRenderer
{
protected function prepareRenderParams(SVGRasterizer $rasterizer, array $options)
{
$offsetX = $rasterizer->getOffsetX();
$offsetY = $rasterizer->getOffsetY();

return array(
'x1' => self::prepareLengthX($options['x1'], $rasterizer),
'y1' => self::prepareLengthY($options['y1'], $rasterizer),
'x2' => self::prepareLengthX($options['x2'], $rasterizer),
'y2' => self::prepareLengthY($options['y2'], $rasterizer),
'x1' => self::prepareLengthX($options['x1'], $rasterizer) + $offsetX,
'y1' => self::prepareLengthY($options['y1'], $rasterizer) + $offsetY,
'x2' => self::prepareLengthX($options['x2'], $rasterizer) + $offsetX,
'y2' => self::prepareLengthY($options['y2'], $rasterizer) + $offsetY,
);
}

Expand Down
7 changes: 5 additions & 2 deletions src/Rasterization/Renderers/SVGPolygonRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ protected function prepareRenderParams(SVGRasterizer $rasterizer, array $options
$scaleX = $rasterizer->getScaleX();
$scaleY = $rasterizer->getScaleY();

$offsetX = $rasterizer->getOffsetX();
$offsetY = $rasterizer->getOffsetY();

$points = array();
foreach($options['points'] as $point) {
$points[] = $point[0] * $scaleX;
$points[] = $point[1] * $scaleY;
$points[] = $point[0] * $scaleX + $offsetX;
$points[] = $point[1] * $scaleY + $offsetY;
}

return array(
Expand Down
4 changes: 2 additions & 2 deletions src/Rasterization/Renderers/SVGRectRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class SVGRectRenderer extends SVGRenderer
{
protected function prepareRenderParams(SVGRasterizer $rasterizer, array $options)
{
$x1 = self::prepareLengthX($options['x'], $rasterizer);
$y1 = self::prepareLengthY($options['y'], $rasterizer);
$x1 = self::prepareLengthX($options['x'], $rasterizer) + $rasterizer->getOffsetX();
$y1 = self::prepareLengthY($options['y'], $rasterizer) + $rasterizer->getOffsetY();
$w = self::prepareLengthX($options['width'], $rasterizer);
$h = self::prepareLengthY($options['height'], $rasterizer);

Expand Down
117 changes: 100 additions & 17 deletions src/Rasterization/SVGRasterizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace JangoBrick\SVG\Rasterization;

use JangoBrick\SVG\SVG;
use JangoBrick\SVG\Nodes\SVGNode;

/**
Expand All @@ -28,41 +29,42 @@ class SVGRasterizer
* @var int $docHeight The original SVG document height, in pixels.
*/
private $docWidth, $docHeight;
/**
* @var float[] The document's viewBox (x, y, w, h).
*/
private $viewBox;
/**
* @var int $width The output image width, in pixels.
* @var int $height The output image height, in pixels.
*/
private $width, $height;
/**
* @var float $scaleX The factor by which output is scaled on the x-axis.
* @var float $scaleY The factor by which output is scaled on the y-axis.
*/
private $scaleX, $scaleY;
/** @var resource $outImage The output image as a GD resource. */
private $outImage;

/**
* @param int $docWidth The original SVG document width, in pixels.
* @param int $docHeight The original SVG document height, in pixels.
* @param int $width The output image width, in pixels.
* @param int $height The output image height, in pixels.
* @param int $docWidth The original SVG document width, in pixels.
* @param int $docHeight The original SVG document height, in pixels.
* @param float[] $viewBox The document's viewBox.
* @param int $width The output image width, in pixels.
* @param int $height The output image height, in pixels.
*/
public function __construct($docWidth, $docHeight, $width, $height)
public function __construct($docWidth, $docHeight, $viewBox, $width, $height)
{
$this->docWidth = $docWidth;
$this->docHeight = $docHeight;

$this->viewBox = $viewBox;

$this->width = $width;
$this->height = $height;

$this->scaleX = $width / $docWidth;
$this->scaleY = $height / $docHeight;

$this->outImage = self::createImage($width, $height);

self::createDependencies();
}



/**
* Sets up a new truecolor GD image resource with the given dimensions.
*
Expand All @@ -85,6 +87,34 @@ private static function createImage($width, $height)
return $img;
}

/**
* Applies a clip to the given image. In other words, ignores everything
* outside the given rectangle. Returns a new image.
*
* @param resource $img The image resource to clip.
* @param int $x1 The rect's start x coordinate.
* @param int $y1 The rect's start y coordinate.
* @param int $x2 The rect's end x coordinate.
* @param int $y2 The rect's end y coordinate.
*
* @return resource A new image that is clipped.
*/
private static function clipImage($img, $x1, $y1, $x2, $y2)
{
$imgWidth = imagesx($img);
$imgHeight = imagesy($img);

$x1 = min(max($x1, 0), $imgWidth);
$y1 = min(max($y1, 0), $imgHeight);
$x2 = min(max($x2, 0), $imgWidth);
$y2 = min(max($y2, 0), $imgHeight);

$newImg = self::createImage($imgWidth, $imgHeight);
imagecopy($newImg, $img, $x1, $y1, $x1, $y1, ($x2 - $x1), ($y2 - $y1));

return $newImg;
}



/**
Expand Down Expand Up @@ -176,15 +206,15 @@ public function render($rendererId, array $params, SVGNode $context)
*/
public function getDocumentWidth()
{
return $this->docWidth;
return SVG::convertUnit($this->docWidth ?: '100%', $this->width);
}

/**
* @return int The original SVG document height, in pixels.
*/
public function getDocumentHeight()
{
return $this->docHeight;
return SVG::convertUnit($this->docHeight ?: '100%', $this->height);
}


Expand Down Expand Up @@ -212,19 +242,72 @@ public function getHeight()
*/
public function getScaleX()
{
return $this->scaleX;
if (!empty($this->viewBox)) {
return $this->getDocumentWidth() / $this->viewBox[2];
}
return $this->width / $this->getDocumentWidth();
}

/**
* @return float The factor by which the output is scaled on the y axis.
*/
public function getScaleY()
{
return $this->scaleY;
if (!empty($this->viewBox)) {
return $this->getDocumentHeight() / $this->viewBox[3];
}
return $this->height / $this->getDocumentHeight();
}



/**
* @return float The amount by which renderers must offset their drawings
* on the x-axis (not to be scaled).
*/
public function getOffsetX()
{
if (!empty($this->viewBox)) {
$scale = $this->getDocumentWidth() / $this->viewBox[2];
return -($this->viewBox[0] * $scale);
}
return 0;
}

/**
* @return float The amount by which renderers must offset their drawings
* on the y-axis (not to be scaled).
*/
public function getOffsetY()
{
if (!empty($this->viewBox)) {
$scale = $this->getDocumentHeight() / $this->viewBox[3];
return -($this->viewBox[1] * $scale);
}
return 0;
}



/**
* Applies final processing steps to the output image. It is then returned.
*
* @return resource The GD image resource this rasterizer is operating on.
*/
public function finish()
{
if (empty($this->viewBox)) {
return $this->outImage;
}

$width = $this->getDocumentWidth();
$height = $this->getDocumentHeight();

$this->outImage = self::clipImage($this->outImage, 0, 0, $width, $height);

return $this->outImage;
}

/**
* @return resource The GD image resource this rasterizer is operating on.
*/
Expand Down
10 changes: 8 additions & 2 deletions src/SVGImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,16 @@ public function toRasterImage($width, $height)
$docWidth = $this->document->getWidth();
$docHeight = $this->document->getHeight();

$rasterizer = new SVGRasterizer($docWidth, $docHeight, $width, $height);
$viewBox = null;
$viewBoxAttr = $this->document->getAttribute('viewBox');
if ($viewBoxAttr) {
$viewBox = array_map('floatval', explode(' ', $viewBoxAttr));
}

$rasterizer = new SVGRasterizer($docWidth, $docHeight, $viewBox, $width, $height);
$this->document->rasterize($rasterizer);

return $rasterizer->getImage();
return $rasterizer->finish();
}

/**
Expand Down