Skip to content

Commit

Permalink
Merge 28474c7 into f9a822d
Browse files Browse the repository at this point in the history
  • Loading branch information
heiglandreas committed Oct 17, 2017
2 parents f9a822d + 28474c7 commit 198e5d0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
sudo: false
dist: trusty

language: php

Expand All @@ -21,6 +22,11 @@ env:
- GH_REF: github.com:heiglandreas/Org_Heigl_Ghostscript.git
- ENCRYPTION_LABEL: "f38516f65eab"

addons:
apt:
packages:
- ghostscript

matrix:
fast_finish: true
include:
Expand Down Expand Up @@ -55,6 +61,9 @@ matrix:
- php: 7.1
env:
- DEPS=latest
- php: 7.2
env:
- DEPS=latest
- php: nightly
env:
- DEPS=lowest
Expand Down
22 changes: 18 additions & 4 deletions src/Ghostscript.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public static function setGsPath($path = null)
throw new \InvalidArgumentException('The given file is not executable');
}

@exec($path . ' -v', $result);
@exec('"' . $path . '" -v', $result);
$content = implode("\n", $result);
if (false === stripos($content, 'ghostscript')) {
throw new \InvalidArgumentException('No valid Ghostscript found');
Expand Down Expand Up @@ -363,9 +363,10 @@ public function getInputFile()
*/
public function setOutputFile($name = 'output')
{
if (0 !== strpos($name, DIRECTORY_SEPARATOR)) {
if ($this->isRelative($name)) {
$name = $this->getBasePath() . DIRECTORY_SEPARATOR . $name;
}

$this->outfile = $name;

return $this;
Expand All @@ -382,7 +383,7 @@ public function setOutputFile($name = 'output')
*/
public function getOutputFile()
{
if (0 !== strpos($this->outfile, DIRECTORY_SEPARATOR)) {
if ($this->isRelative($this->outfile)) {
return $this->getBasePath() . DIRECTORY_SEPARATOR . $this->outfile;
}

Expand Down Expand Up @@ -440,7 +441,7 @@ public function getRenderString()
if (null === $this->getInputFile()) {
return '';
}
$string = self::getGsPath();
$string = '"' . self::getGsPath() . '"';
$string .= ' -dSAFER -dQUIET -dNOPLATFONTS -dNOPAUSE -dBATCH';
$string .= ' -sOutputFile="' . $this->getOutputFile() . '.' . $this->getDevice()->getFileEnding() . '"';
$string .= $this->getDevice()->getParameterString();
Expand Down Expand Up @@ -878,6 +879,19 @@ public function setPages($startPage, $endPage = null)

return $this;
}

private function isRelative($path)
{
if (0 === strpos($path, DIRECTORY_SEPARATOR)) {
return false;
}

if (1 === strpos($path, ':\\') && preg_match('/^[A-Za-z]/', $path)) {
return false;
}

return true;
}
}

try {
Expand Down
23 changes: 22 additions & 1 deletion tests/GhostscriptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function testRenderingStrings()
$f = new Ghostscript();
$dir = __DIR__ . DIRECTORY_SEPARATOR . 'support' . DIRECTORY_SEPARATOR;
$filename = $dir . 'test.pdf';
$path = Ghostscript::getGsPath();
$path = '"' . Ghostscript::getGsPath() . '"';
$this->assertEquals('', $f->getRenderString());
$f->setInputFile($filename);
$expect = $path . ' -dSAFER -dQUIET -dNOPLATFONTS -dNOPAUSE -dBATCH -sOutputFile="' . $dir . 'output.png" -sDEVICE=pngalpha -r72 "' .$filename . '"';
Expand Down Expand Up @@ -266,4 +266,25 @@ public function testThatSettingPathToSomethingGsLikeWorks()
Ghostscript::class
);
}

/** @dataProvider provideRelativePaths */
public function testThatRelativePathsAreCorrectlyDetected($path, $result)
{
$reflection = new \ReflectionClass(Ghostscript::class);
$method = $reflection->getMethod('isRelative');
$method->setAccessible(true);

$this->assertSame($result, $method->invokeArgs(new Ghostscript(), [$path]));
}

public function provideRelativePaths()
{
return [
['/test/foo', false],
['a:\foo', false],
['bar', true],
['\foob', true],
['a:/test', true],
];
}
}

0 comments on commit 198e5d0

Please sign in to comment.