Skip to content
This repository has been archived by the owner on Mar 29, 2021. It is now read-only.

Commit

Permalink
Add getArgByName
Browse files Browse the repository at this point in the history
  • Loading branch information
hxtree committed Mar 12, 2020
1 parent a79d2db commit 54cfc82
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
20 changes: 9 additions & 11 deletions modules/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,20 @@ class Image extends \LivingMarkup\Module
public function onLoad(){

// load source file info
$this->fetchSourceInfo();
$src = $this->getArgByName('src');
$this->fetchSourceInfo($src);

// set dimensions
$width = array_key_exists('width', $this->args) ? $this->args['width'] : NULL;
$height = array_key_exists('height', $this->args) ? $this->args['height'] : NULL;
$width = $this->getArgByName('width');
$height = $this->getArgByName('height');
$this->setDimensions($width, $height);

// set offset
$offset = array_key_exists('offset', $this->args) ? $this->args['offset'] : NULL;
$offset = $this->getArgByName('offset');
$this->setOffset($offset);

// set alt
$alt = array_key_exists('alt', $this->args) ? $this->args['alt'] : NULL;
$alt = $this->getArgByName('alt');
$this->setCacheURL($alt);
}

Expand All @@ -69,15 +70,11 @@ public function onRender()
HTML;
}

public function fetchSourceInfo()
public function fetchSourceInfo($source)
{
// image missing
if(!isset($this->args['src'])){
return false;
}

// TODO: add traversing prevention
$filepath = __DIR__ . '/..' . self::IMAGE_SOURCE_DIR . $this->args['src'];
$filepath = __DIR__ . '/..' . self::IMAGE_SOURCE_DIR . $source;

// get dimensions from original image file
[$this->source_width, $this->source_height, $this->source_type, $this->source_attr] = getimagesize($filepath);
Expand Down Expand Up @@ -133,6 +130,7 @@ public function setOffset($offset){
list($this->offset['x'], $this->offset['y']) = explode(',', $offset);
}
}

/**
* Set dimension (width x height) of output
*
Expand Down
10 changes: 10 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ public function getId()
return $this->module_id;
}

/**
* Get Arg by Name
*
* @param $name
* @return mixed|null
*/
public function getArgByName($name){
return array_key_exists($name, $this->args) ? $this->args[$name] : NULL;
}

/**
* Abstract output method called by magic method
*
Expand Down

0 comments on commit 54cfc82

Please sign in to comment.