Skip to content

Commit

Permalink
Format code and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehaertl committed Jul 22, 2017
1 parent bc47743 commit dccea43
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 83 deletions.
60 changes: 32 additions & 28 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand All @@ -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)) {
Expand All @@ -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()
{
Expand All @@ -99,4 +104,3 @@ public function getExecCommand()
return $command;
}
}

53 changes: 31 additions & 22 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Pdf
*
* 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
Expand All @@ -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';

Expand All @@ -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;

Expand All @@ -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();

Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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)) {
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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');
Expand Down
Loading

0 comments on commit dccea43

Please sign in to comment.