Skip to content

Commit 3f5e578

Browse files
mbardelmeijerMASNathan
authored andcommitted
Add the ability to pass options directly to the PhantomJS binary (#70)
1 parent 1ba90e0 commit 3f5e578

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,18 @@ $screen->includeJs("console.log('This is supa cool!');");
106106

107107
Just use this method before calling ```save(...)```
108108

109+
##Passing options to PhantomJS
110+
111+
You can set the options that will be passed to the PhantomJS binary.
112+
113+
``` php
114+
$screenCapture->setOptions([
115+
'ignore-ssl-errors' => 'yes',
116+
// '--ignore-ssl-errors' => 'yes', // dashes may be omitted
117+
]);
118+
```
119+
120+
109121
##Other configurations
110122
Additionally to the basic usage, you can set so extra configurations.
111123

src/Capture.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ class Capture
132132
*/
133133
protected $includedJsSnippets = array();
134134

135+
/**
136+
* List of options which will be passed to phantomjs
137+
*
138+
* @var array
139+
*/
140+
protected $options = array();
141+
135142
/**
136143
* Capture constructor.
137144
*/
@@ -217,12 +224,19 @@ public function save($imageLocation, $deleteFileIfExists = true)
217224
file_put_contents($jobPath, $resultString);
218225
}
219226

220-
$command = sprintf("%sphantomjs %s", $this->binPath, $jobPath);
227+
$command = sprintf("%sphantomjs %s %s", $this->binPath, $this->getOptionsString(), $jobPath);
221228
$result = exec(escapeshellcmd($command));
222229

223230
return file_exists($this->imageLocation);
224231
}
225232

233+
/**
234+
* @param string $templateName
235+
* @param array $args
236+
*
237+
* @return string
238+
* @throws TemplateNotFoundException
239+
*/
226240
private function getTemplateResult($templateName, array $args)
227241
{
228242
$templatePath = $this->templatePath . DIRECTORY_SEPARATOR . $templateName . '.php';
@@ -236,6 +250,26 @@ private function getTemplateResult($templateName, array $args)
236250
return ob_get_clean();
237251
}
238252

253+
/**
254+
* @return string
255+
*/
256+
private function getOptionsString()
257+
{
258+
if (empty($this->options)) {
259+
return '';
260+
}
261+
262+
$mappedOptions = array_map(function ($value, $key) {
263+
if (substr($key, 0, 2) === '--') {
264+
$key = substr($key, 2);
265+
}
266+
267+
return sprintf("--%s=%s", $key, $value);
268+
}, $this->options, array_keys($this->options));
269+
270+
return implode(' ', $mappedOptions);
271+
}
272+
239273
/**
240274
* Sets the path to PhantomJS binary, example: "/usr/bin"
241275
*
@@ -415,4 +449,18 @@ public function includeJs($script)
415449

416450
return $this;
417451
}
452+
453+
/**
454+
* Sets the options which will be passed to phantomjs
455+
*
456+
* @param array $options
457+
*
458+
* @return $this
459+
*/
460+
public function setOptions($options)
461+
{
462+
$this->options = $options;
463+
464+
return $this;
465+
}
418466
}

0 commit comments

Comments
 (0)