1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-18 12:31:17 +02:00

added object as output format to the getDebugInfo

I figured out that this is useful to have for (automated) testing and debugging too. It is much easier to get a value for conditional coding via an object:
```
$dbgInfo = $image->getDebugInfo($options, 'object');
if($dbgInfo->engines->selectedEngine == ....
```
This commit is contained in:
horst-n
2019-05-04 11:21:58 +02:00
parent d361c6c11e
commit 88d4b11db4

View File

@@ -1915,11 +1915,11 @@ class Pageimage extends Pagefile {
* (without invoking the magic debug) * (without invoking the magic debug)
* *
* @param array $options The individual options you also passes with your image variation creation * @param array $options The individual options you also passes with your image variation creation
* @param bool $returnAsString default is true and returns markup or plain text, false returns multidim array * @param bool $returnType 'string'|'array'|'object', default is 'string' and returns markup or plain text
* @return array|string * @return array|object|string
* *
*/ */
public function getDebugInfo($options = array(), $returnAsString = true) { public function getDebugInfo($options = array(), $returnType = 'string') {
static $depth = 0; static $depth = 0;
$depth++; $depth++;
@@ -1929,9 +1929,9 @@ class Pageimage extends Pagefile {
$finalOptions = $oSizer->getOptions(); $finalOptions = $oSizer->getOptions();
// build some info parts and fetch some from parent (pagefile) // build some info parts and fetch some from parent (pagefile)
$thumb = array('THUMB' => "<img src='{$this->url}' style='max-width:120px; max-height:120px; ".($this->width() >= $this->height() ? 'width:100px; height:auto;' : 'height:100px; width:auto;')."' alt='' />"); $thumb = array('thumb' => "<img src='{$this->url}' style='max-width:120px; max-height:120px; ".($this->width() >= $this->height() ? 'width:100px; height:auto;' : 'height:100px; width:auto;')."' alt='' />");
$original = $this->original ? array('original' => $this->original->basename, 'basename' => $this->basename) : array('original' => '{SELF}', 'basename' => $this->basename); $original = $this->original ? array('original' => $this->original->basename, 'basename' => $this->basename) : array('original' => '{SELF}', 'basename' => $this->basename);
$parent = array('FILES' => array_merge( $parent = array('files' => array_merge(
$original, $original,
parent::__debugInfo(), parent::__debugInfo(),
array( array(
@@ -1940,8 +1940,8 @@ class Pageimage extends Pagefile {
) )
)); ));
// rearange parts // rearange parts
unset($parent['FILES']['filesize']); unset($parent['files']['filesize']);
$parent['FILES']['filesize'] = filesize($this->filename); $parent['files']['filesize'] = filesize($this->filename);
// VARIATIONS // VARIATIONS
if($depth < 2) { if($depth < 2) {
@@ -1954,37 +1954,38 @@ class Pageimage extends Pagefile {
// start collecting the $info // start collecting the $info
$info = array_merge($thumb, $parent, $info = array_merge($thumb, $parent,
array('VARIATIONS' => $variationArray), array('variations' => $variationArray),
array('IMAGEINFO' => array( array('imageinfo' => array(
'imageType' => $osInfo['info']['imageType'], 'imageType' => $osInfo['info']['imageType'],
'mime' => $osInfo['info']['mime'], 'mime' => $osInfo['info']['mime'],
'width' => $this->width(), 'width' => $this->width(),
'height' => $this->height(), 'height' => $this->height(),
'focus' => $this->hasFocus ? $this->focusStr : NULL, 'focus' => $this->hasFocus ? $this->focusStr : NULL,
'description' => $parent['FILES']['description'], 'description' => $parent['files']['description'],
'tags' => $parent['FILES']['tags'], 'tags' => $parent['files']['tags'],
)) ))
); );
unset($info['FILES']['tags'], $info['FILES']['description']); unset($info['files']['tags'], $info['files']['description']);
// beautify the output, remove unnecessary items // beautify the output, remove unnecessary items
if(isset($info['FILES']['filedata']) && isset($info['FILES']['filedata']['focus'])) unset($info['FILES']['filedata']['focus']); if(isset($info['files']['filedata']) && isset($info['files']['filedata']['focus'])) unset($info['files']['filedata']['focus']);
if(empty($info['FILES']['filedata'])) unset($info['FILES']['filedata']); if(empty($info['files']['filedata'])) unset($info['files']['filedata']);
unset($osInfo['info']['mime'], $osInfo['info']['imageType']); unset($osInfo['info']['mime'], $osInfo['info']['imageType']);
// add the rest from osInfo to the final $info array // add the rest from osInfo to the final $info array
foreach($osInfo['info'] as $k => $v) $info['IMAGEINFO'][$k] = $v; foreach($osInfo['info'] as $k => $v) $info['imageinfo'][$k] = $v;
$info['IMAGEINFO']['iptcRaw'] = $osInfo['iptcRaw']; $info['imageinfo']['iptcRaw'] = $osInfo['iptcRaw'];
unset($osInfo, $thumb, $original, $parent); unset($osInfo, $thumb, $original, $parent);
// WEBP // WEBP
$webp = array('WEBP COPY' => (!$this->hasWebp() ? array('hasWebp' => false) : array( $webp = array('webp_copy' => array(
'hasWebp' => true, 'hasWebp' => $this->hasWebp(),
'webpUrl' => $this->webpUrl, 'webpUrl' => (!$this->hasWebp() ? NULL : $this->webpUrl),
'webpQuality' => isset($finalOptions['webpQuality']) ? $finalOptions['webpQuality'] : NULL, 'webpQuality' => (!isset($finalOptions['webpQuality']) ? NULL : $finalOptions['webpQuality']),
'filesize' => filesize($this->webpFilename()), 'filesize' => (!$this->hasWebp() ? 0 : filesize($this->webpFilename())),
'savings in percent' => 100 - intval(filesize($this->webpFilename()) / ($info['FILES']['filesize'] / 100)) 'savings' => (!$this->hasWebp() ? 0 : intval($info['files']['filesize'] - filesize($this->webpFilename()))),
))); 'savings_percent' => (!$this->hasWebp() ? 0 : 100 - intval(filesize($this->webpFilename()) / ($info['files']['filesize'] / 100))),
));
// ENGINES // ENGINES
$a = []; $a = [];
@@ -2006,9 +2007,9 @@ class Pageimage extends Pagefile {
// merge all into $info // merge all into $info
$info = array_merge($info, $webp, $info = array_merge($info, $webp,
array('ENGINE(S)' => $enginesArray), array('engines' => $enginesArray),
// OPTIONS // OPTIONS
array('OPTIONS HIERARCHY' => array( array('options_hierarchy' => array(
'imageSizerOptions' => $this->wire('config')->imageSizerOptions, 'imageSizerOptions' => $this->wire('config')->imageSizerOptions,
'individualOptions' => $options, 'individualOptions' => $options,
'finalOptions' => $finalOptions 'finalOptions' => $finalOptions
@@ -2018,14 +2019,32 @@ class Pageimage extends Pagefile {
unset($variationArray, $webp, $enginesArray, $options, $finalOptions); unset($variationArray, $webp, $enginesArray, $options, $finalOptions);
// If not in browser environment, remove the thumb image // If not in browser environment, remove the thumb image
if(!isset($_SERVER['HTTP_HOST'])) unset($info['THUMB']); if(!isset($_SERVER['HTTP_HOST'])) unset($info['thumb']);
// return as array if('array' == $returnType) {
if(!$returnAsString) { // return as array
return $info; return $info;
} else if('object' == $returnType) {
// return as object
$object = new \stdClass();
foreach($info as $group => $array) {
$object->$group = new \stdClass();
if('thumb' == $group) {
$object->$group = $array;
continue;
}
$this->array_to_object($array, $object->$group);
}
return $object;
} }
// make a beautyfied var_dump // make a beautyfied var_dump
$tmp = $info;
$info = array();
foreach($tmp as $group => $array) {
$info[mb_strtoupper($group)] = $array;
}
unset($tmp, $group, $array);
ob_start(); ob_start();
var_dump($info); var_dump($info);
$content = ob_get_contents(); $content = ob_get_contents();
@@ -2055,6 +2074,27 @@ class Pageimage extends Pagefile {
return $return; return $return;
} }
/**
* Helper method that converts a multidim array to a multidim object for the getDebugInfo method
*
* @param array $array the input array
* @param object $object the initial object, gets passed recursive by reference through all loops
* @param bool $multidim set this to true to avoid multidimensional object
* @return object the final multidim object
*
*/
private function array_to_object($array, &$object, $multidim = true) {
foreach($array as $key => $value) {
if($multidim && is_array($value)) {
$object->$key = new \stdClass();
$this->array_to_object($value, $object->$key, false);
} else {
$object->$key = $value;
}
}
return $object;
}
/** /**