ImagePlusValue output modifier.
<?php
/**
* imageplusvalue is a Snippet/Output Modifier to parse
* the ImagePlus stored JSON and return values or thumbs.
*
* @author YJ Tso <yj@modx.com>
*
* Example usage:
* <img src="[[*image:imageplusvalue=`croppedImg:w=840&zc=1`]]" alt="[[*image:imageplusvalue=`altTag`]]">
*
*/
// Input
$input = $modx->getOption('input', $scriptProperties, '');
$input = $modx->fromJSON($input);
if (empty($input) || !is_array($input)) return $input;
// Options
$options = array_map('trim', explode('.', $modx->getOption('options', $scriptProperties, '')));
if (empty($options) || !is_array($options)) return $input;
// More properties
$toPlaceholder = $modx->getOption('toPlaceholder', $scriptProperties, '');
$debug = $modx->getOption('debug', $scriptProperties, false, true);
if ($debug) return print_r($input, true);
// Process $input
foreach ($options as $key) {
// If requirements are met, call pThumb
if ((strpos($key, 'croppedImg') !== false) && isset($input['sourceImg']['src']) && $modx->getCount('modSnippet', array('name' => 'pthumb'))) {
// Prepare arguments for pthumb snippet call
$cropParams = array(
'sx' => $input['crop']['x'],
'sy' => $input['crop']['y'],
'sw' => $input['crop']['width'],
'sh' => $input['crop']['height'],
);
// Merge in additional params from ImagePlus
$params = array();
if ($input['targetWidth']) {
$params = array_merge($params, array(
'w' => $input['targetWidth']
));
}
if ($input['targetHeight']) {
$params = array_merge($params, array(
'h' => $input['targetHeight']
));
}
// Fetch runtime params
$fetchedParamsArray = array();
// Use ':' to separate 'croppedImg' flag from params
if (strpos($key, ':')) {
$fetchedParams = explode(':', $key)[1];
parse_str($fetchedParams, $fetchedParamsArray);
}
if (!empty($fetchedParamsArray) && is_array($fetchedParamsArray)) $params = array_merge($params, $fetchedParamsArray);
// This could be optional. It's taken from the ImagePlus class
if ($params['w'] && $params['h']) {
$params = array_merge($params, array(
'far' => true
));
}
// Merge all params
$params = array_merge($cropParams, $params);
$params = http_build_query($params);
// Call pThumb
$thumb = $modx->runSnippet('pthumb', array(
'input' => $input['sourceImg']['src'],
'options' => $params,
));
$input = $thumb;
// In this case we're done
break;
}
// Otherwise recursively get the value from $input array
$input = $input[$key];
}
// Output
if (empty($toPlaceholder)) return $input;
$modx->setPlaceholder($toPlaceholder, $input);