Skip to content

Commit

Permalink
Merge pull request #2 from heiglandreas/feature/addPagesSupport
Browse files Browse the repository at this point in the history
Adds support for page-range
  • Loading branch information
heiglandreas committed Jun 6, 2016
2 parents f1f62b9 + 3ecfea4 commit 3a9b0a4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Ghostscript.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ class Ghostscript
'application/ps',
);

protected $pageStart = null;

protected $pageEnd = null;

/**
* Set the path to the gs-executable and return it.
*
Expand Down Expand Up @@ -319,9 +323,30 @@ public function getRenderString()
if ($this -> isGraphicsAntiAliasingSet()) {
$string .= ' -dGraphicsAlphaBits=' . $this -> getGraphicsAntiAliasing();
}

$string .= $this->getPageRangeString();

$string .= ' "' . $this -> getInputFile() . '"';
return $string;
}

public function getPageRangeString()
{
if (null === $this->pageStart) {
return '';
}

$string = ' -dFirstPage=%d -dLastPage=%d';

$pageStart = $this->pageStart;
$pageEnd = $this->pageEnd;
if (null === $this->pageEnd) {
$pageEnd = $this->pageStart;
}

return sprintf($string, $pageStart, $pageEnd);
}

/**
* Check whether Anti ALiasing for graphics is set
*
Expand Down Expand Up @@ -522,6 +547,25 @@ public function useCie()
return (bool) $this -> _useCie;
}

/**
* Set a page-Range
*
* @param $startPage
* @param $endPage
*
* @return self
*/
public function setPages($startPage, $endPage = null)
{
$this->pageStart = (int) $startPage;

if (null !== $endPage) {
$this->pageEnd = (int) $endPage;
}

return $this;
}

/**
* Store whether to use CIE for color conversion or not
*
Expand Down
18 changes: 18 additions & 0 deletions tests/GhostscriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,22 @@ public function testRendering()
$this -> assertTrue($f -> render());
unlink(dirname($filename) . DIRECTORY_SEPARATOR . 'output.png');
}

public function testSettingPages()
{
$f = new Ghostscript();
$this->assertAttributeEquals(null, 'pageStart', $f);
$this->assertAttributeEquals(null, 'pageEnd', $f);
$this->assertEmpty($f->getPageRangeString());

$f->setPages(2);
$this->assertAttributeEquals(2, 'pageStart', $f);
$this->assertAttributeEquals(null, 'pageEnd', $f);
$this->assertEquals(' -dFirstPage=2 -dLastPage=2', $f->getPageRangeString());

$f->setPages(3,4);
$this->assertAttributeEquals(3, 'pageStart', $f);
$this->assertAttributeEquals(4, 'pageEnd', $f);
$this->assertEquals(' -dFirstPage=3 -dLastPage=4', $f->getPageRangeString());
}
}

0 comments on commit 3a9b0a4

Please sign in to comment.