-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc47743
commit dccea43
Showing
3 changed files
with
108 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ | |
/** | ||
* Command | ||
* | ||
* This class is an extension of mikehaertl\shellcommand\Command and adds | ||
* wk* specific features like xvfb support and proper argument handling. | ||
* This class is an extension of mikehaertl\shellcommand\Command and adds `wk*` | ||
* specific features like xvfb support and proper argument handling. | ||
* | ||
* @author Michael Härtl <[email protected]> | ||
* @license http://www.opensource.org/licenses/MIT | ||
|
@@ -20,43 +20,47 @@ class Command extends BaseCommand | |
public $enableXvfb = false; | ||
|
||
/** | ||
* @var string the name of the xvfb-run comand. Default is `xvfb-run`. | ||
* You can also configure a full path here. | ||
* @var string the name of the xvfb-run comand. Default is `xvfb-run`. You | ||
* can also configure a full path here. | ||
*/ | ||
public $xvfbRunBinary = 'xvfb-run'; | ||
|
||
/** | ||
* @var string options to pass to the xfvb-run command. Default is `--server-args="-screen 0, 1024x768x24"`. | ||
* @var string options to pass to the xfvb-run command. Default is | ||
* `--server-args="-screen 0, 1024x768x24"`. | ||
*/ | ||
public $xvfbRunOptions = '-a --server-args="-screen 0, 1024x768x24"'; | ||
|
||
/** | ||
* @param array $args args to add to the command. These can be: | ||
* array( | ||
* // Special argument 'input' will not get prepended with '--'. | ||
* 'input' => 'cover', | ||
* ``` | ||
* [ | ||
* // Special argument 'input' will not get prepended with '--'. | ||
* 'input' => 'cover', | ||
* | ||
* // Special argument 'inputArg' is treated like 'input' but will get escaped | ||
* // Both 'input' and 'inputArg' can be used in combination | ||
* 'inputArg' => '/tmp/tmpFileName.html', | ||
* // Special argument 'inputArg' is treated like 'input' but will get escaped | ||
* // Both 'input' and 'inputArg' can be used in combination | ||
* 'inputArg' => '/tmp/tmpFileName.html', | ||
* | ||
* 'no-outline', // option without argument | ||
* 'encoding' => 'UTF-8', // option with argument | ||
* 'no-outline', // option without argument | ||
* 'encoding' => 'UTF-8', // option with argument | ||
* | ||
* // Option with 2 arguments | ||
* 'cookie' => array('name'=>'value'), | ||
* // Option with 2 arguments | ||
* 'cookie' => array('name'=>'value'), | ||
* | ||
* // Repeatable options with single argument | ||
* 'run-script' => array( | ||
* 'local1.js', | ||
* 'local2.js', | ||
* ), | ||
* // Repeatable options with single argument | ||
* 'run-script' => array( | ||
* 'local1.js', | ||
* 'local2.js', | ||
* ), | ||
* | ||
* // Repeatable options with 2 arguments | ||
* 'replace' => array( | ||
* '{page}' => $page++, | ||
* '{title}' => $pageTitle, | ||
* ), | ||
* // Repeatable options with 2 arguments | ||
* 'replace' => array( | ||
* '{page}' => $page++, | ||
* '{title}' => $pageTitle, | ||
* ), | ||
* ] | ||
* ``` | ||
*/ | ||
public function addArgs($args) | ||
{ | ||
|
@@ -70,7 +74,7 @@ public function addArgs($args) | |
$this->addArg((string) $args['inputArg'], null, true); | ||
unset($args['inputArg']); | ||
} | ||
foreach($args as $key=>$val) { | ||
foreach($args as $key => $val) { | ||
if (is_numeric($key)) { | ||
$this->addArg("--$val"); | ||
} elseif (is_array($val)) { | ||
|
@@ -88,7 +92,8 @@ public function addArgs($args) | |
} | ||
|
||
/** | ||
* @return string|bool the command to execute with optional Xfvb wrapper applied. Null if none set. | ||
* @return string|bool the command to execute with optional Xfvb wrapper | ||
* applied. Null if none set. | ||
*/ | ||
public function getExecCommand() | ||
{ | ||
|
@@ -99,4 +104,3 @@ public function getExecCommand() | |
return $command; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/** | ||
* | ||
* This class is a slim wrapper around wkhtmltoimage. | ||
* This class is a slim wrapper around `wkhtmltoimage`. | ||
* | ||
* @author Michael Härtl <[email protected]> | ||
* @license http://www.opensource.org/licenses/MIT | ||
|
@@ -20,13 +20,14 @@ class Image | |
const TMP_PREFIX = 'tmp_wkhtmlto_pdf_'; | ||
|
||
/** | ||
* @var string the name of the `wkhtmltoimage` binary. Default is `wkhtmltoimage`. You can also | ||
* configure a full path here. | ||
* @var string the name of the `wkhtmltoimage` binary. Default is | ||
* `wkhtmltoimage`. You can also configure a full path here. | ||
*/ | ||
public $binary = 'wkhtmltoimage'; | ||
|
||
/** | ||
* @var string the image type. Default is 'png'. Other options are 'jpg' and 'bmp'. | ||
* @var string the image type. Default is 'png'. Other options are 'jpg' | ||
* and 'bmp'. | ||
*/ | ||
public $type = 'png'; | ||
|
||
|
@@ -36,12 +37,14 @@ class Image | |
public $commandOptions = array(); | ||
|
||
/** | ||
* @var string|null the directory to use for temporary files. If null (default) the dir is autodetected. | ||
* @var string|null the directory to use for temporary files. If `null` | ||
* (default) the dir is autodetected. | ||
*/ | ||
public $tmpDir; | ||
|
||
/** | ||
* @var bool whether to ignore any errors if some PDF file was still created. Default is false. | ||
* @var bool whether to ignore any errors if some PDF file was still | ||
* created. Default is `false`. | ||
*/ | ||
public $ignoreWarnings = false; | ||
|
||
|
@@ -51,12 +54,14 @@ class Image | |
protected $_isCreated = false; | ||
|
||
/** | ||
* @var \mikehaertl\tmp\File|string the page input or a File instance for HTML string inputs | ||
* @var \mikehaertl\tmp\File|string the page input or a `File` instance for | ||
* HTML string inputs | ||
*/ | ||
protected $_page; | ||
|
||
/** | ||
* @var array options for wkhtmltoimage as array('--opt1', '--opt2'=>'val', ...) | ||
* @var array options for `wkhtmltoimage` as `['--opt1', '--opt2' => 'val', | ||
* ...]` | ||
*/ | ||
protected $_options = array(); | ||
|
||
|
@@ -76,9 +81,10 @@ class Image | |
protected $_error = ''; | ||
|
||
/** | ||
* @param array|string $options global options for wkhtmltoimage, a page URL, a HTML string or a filename | ||
* @param array|string $options global options for wkhtmltoimage, a page | ||
* URL, a HTML string or a filename | ||
*/ | ||
public function __construct($options=null) | ||
public function __construct($options = null) | ||
{ | ||
if (is_array($options)) { | ||
$this->setOptions($options); | ||
|
@@ -119,14 +125,17 @@ public function saveAs($filename) | |
} | ||
|
||
/** | ||
* Send image to client, either inline or as download (triggers image creation) | ||
* Send image to client, either inline or as download (triggers image | ||
* creation) | ||
* | ||
* @param string|null $filename the filename to send. If empty, the PDF is streamed inline. Note, that | ||
* the file extension must match what you configured as $type (png, jpg, ...). | ||
* @param bool $inline whether to force inline display of the image, even if filename is present. | ||
* @param string|null $filename the filename to send. If empty, the PDF is | ||
* streamed inline. Note, that the file extension must match what you | ||
* configured as $type (png, jpg, ...). | ||
* @param bool $inline whether to force inline display of the image, even | ||
* if filename is present. | ||
* @return bool whether image was created successfully | ||
*/ | ||
public function send($filename=null,$inline=false) | ||
public function send($filename = null,$inline = false) | ||
{ | ||
if (!$this->_isCreated && !$this->createImage()) { | ||
return false; | ||
|
@@ -141,9 +150,9 @@ public function send($filename=null,$inline=false) | |
* @param array $options list of image options to set as name/value pairs | ||
* @return static the Image instance for method chaining | ||
*/ | ||
public function setOptions($options=array()) | ||
public function setOptions($options = array()) | ||
{ | ||
foreach ($options as $key=>$val) { | ||
foreach ($options as $key => $val) { | ||
if (is_int($key)) { | ||
$this->_options[] = $val; | ||
} elseif ($key[0]!=='_' && property_exists($this, $key)) { | ||
|
@@ -160,7 +169,7 @@ public function setOptions($options=array()) | |
*/ | ||
public function getCommand() | ||
{ | ||
if ($this->_command===null) { | ||
if ($this->_command === null) { | ||
$options = $this->commandOptions; | ||
if (!isset($options['command'])) { | ||
$options['command'] = $this->binary; | ||
|
@@ -183,7 +192,7 @@ public function getError() | |
*/ | ||
public function getImageFilename() | ||
{ | ||
if ($this->_tmpImageFile===null) { | ||
if ($this->_tmpImageFile === null) { | ||
$this->_tmpImageFile = new File('', '.'.$this->type, self::TMP_PREFIX); | ||
} | ||
return $this->_tmpImageFile->getFileName(); | ||
|
@@ -195,11 +204,11 @@ public function getImageFilename() | |
*/ | ||
public function getMimeType() | ||
{ | ||
if ($this->type==='jpg') { | ||
if ($this->type === 'jpg') { | ||
return 'image/jpeg'; | ||
} elseif ($this->type==='png') { | ||
} elseif ($this->type === 'png') { | ||
return 'image/png'; | ||
} elseif ($this->type==='bmp') { | ||
} elseif ($this->type === 'bmp') { | ||
return 'image/bmp'; | ||
} else { | ||
throw new \Exception('Invalid image type'); | ||
|
Oops, something went wrong.