From 3ecfea48c160b2ce9fac6b9d56ab9b1a51843714 Mon Sep 17 00:00:00 2001 From: Andreas Heigl Date: Mon, 6 Jun 2016 08:50:40 +0200 Subject: [PATCH] Adds support for page-range --- src/Ghostscript.php | 44 +++++++++++++++++++++++++++++++++++++++ tests/GhostscriptTest.php | 18 ++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/Ghostscript.php b/src/Ghostscript.php index a2644da..bb33059 100644 --- a/src/Ghostscript.php +++ b/src/Ghostscript.php @@ -142,6 +142,10 @@ class Ghostscript 'application/ps', ); + protected $pageStart = null; + + protected $pageEnd = null; + /** * Set the path to the gs-executable and return it. * @@ -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 * @@ -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 * diff --git a/tests/GhostscriptTest.php b/tests/GhostscriptTest.php index 7fda85c..aa312f3 100644 --- a/tests/GhostscriptTest.php +++ b/tests/GhostscriptTest.php @@ -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()); + } }