public function save($imageLocation, $deleteFileIfExists = true)
{
$this->imageLocation = $this->output->getLocation() . $imageLocation;
if (!pathinfo($this->imageLocation, PATHINFO_EXTENSION)) {
$this->imageLocation .= '.' . $this->getImageType()->getFormat();
}
$data = array('url' => (string) $this->url, 'width' => $this->width, 'height' => $this->height, 'imageLocation' => LocalPath::sanitize($this->imageLocation));
if ($this->clipWidth && $this->clipHeight) {
$data['clipOptions']['width'] = $this->clipWidth;
$data['clipOptions']['height'] = $this->clipHeight;
$data['clipOptions']['top'] = 0;
$data['clipOptions']['left'] = 0;
}
if ($this->backgroundColor) {
$data['backgroundColor'] = $this->backgroundColor;
} elseif ($this->getImageType()->getFormat() == Types\Jpg::FORMAT) {
// If there is no background color set, and it's a jpeg
// we need to set a bg color, otherwise the background will be black
$data['backgroundColor'] = '#FFFFFF';
}
if ($this->userAgentString) {
$data['userAgent'] = $this->userAgentString;
}
if ($this->timeout) {
$data['timeout'] = $this->timeout;
}
if ($this->includedJsScripts) {
$data['includedJsScripts'] = $this->includedJsScripts;
}
if ($this->includedJsSnippets) {
$data['includedJsSnippets'] = $this->includedJsSnippets;
}
if ($deleteFileIfExists && file_exists($this->imageLocation) && is_writable($this->imageLocation)) {
unlink($this->imageLocation);
}
$jobName = md5(json_encode($data));
$jobPath = $this->jobs->getLocation() . $jobName . '.js';
if (!is_file($jobPath)) {
// Now we write the code to a js file
$resultString = $this->getTemplateResult('screen-capture', $data);
file_put_contents($jobPath, $resultString);
}
$command = sprintf("%sphantomjs %s %s", $this->binPath, $this->getOptionsString(), $jobPath);
// Run the command and ensure it executes successfully
$returnCode = null;
$output = [];
exec(sprintf("%s 2>&1", escapeshellcmd($command)), $output, $returnCode);
if ($returnCode !== 0) {
throw new PhantomJsException($output);
}
return file_exists($this->imageLocation);
}