<?php
// PHPThumb Output Filter - The Dumb way (I'm sure)
//
// By Jared Loman a.k.a jtech - http://www.jaredloman.com
//
// Revision by Djordje Dimitrijev (dj13 on Modx forum)
//
// Revision Notes:
//
// Added possibility use or not high security password from phpThumb config.
// Possibility to add as much as you want default image attributes through the array.
// New way to add attributes in filter:
// attribute_html_name || attribute_value
//
// Example: [[*mytvImage:phpthumb=`&w=100&h=50&zc=1,class||myClass,title||myTitle,style||border: 1px solid #333`]]
//
// Version 0.2 beta
//
// Current Version Notes:
// Added array to handle "class" & "alt" options with defaults.
// Added a space removal fool-proof in case someone trys to skip the class and add an alt.
// Added Readme
// Still haven't tested all phpthumb modifiers.
//
// Previous Version Notes:
// v.0.1: I haven't tested all of the phpthumb modifiers, but I think they should work
// ReadMe:
//
// Notes:
// I created the PHPThumb output filter so that I could have some flexibility with my images that an output modifier
// couldn't offer me. This output filter is essentially useless for anything other than images (Obviously!), but I
// figured I'd at least mention this.
//
// This does not support PHPThumbs ability to create new images from nothing. It would be kind of silly to use a
// TV and Output filter for this purpose anyhow.
//
// INSTRUCTIONS:
// Step 1.) Create a TV with an input type of "IMAGE" and an output type of "TEXT".
// Step 2.) Add an image to the TV.
// Step 3.) Add your TV to your page content or template and modify in the format of the example.
//
// OPTIONS:
// Option 1.) The first option is the phpthumb modifications. You can add any modifier that phpthumb supports. (AFIK)
// Option 2.) Image Class (optional) - Supply a class for your image. Defaults to "phpthumb" if you don't put anything in.
// Option 3.) Image Alt (optional) - Supply text for the alt tag. Defaults to "phpthumb" if you don't put anything in.
//
// IMPORTANT:
// Options MUST be put in specified order!
// You Must specify a class if you would like a custom ALT. (e.g. [[*myTV:phpthumb=`&w=100,My Alt Text`]] ) will
// return <img src=".." class="MyAltText" alt="phpthumb" />.. Sorry, it's just the nature of the beast.
//
// TV Output Type MUST be "TEXT"!!
//
// Example: [[*mytvImage:phpthumb=`&w=100&h=50&zc=1,imgClass,imgAlt`]]
$phpthumb_path = 'assets/components/phpthumb';
// Define attribute name - value separator
$attribute_sep = '||';
$base = $modx->config['base_url'];
// Default array of attributes with key name like html attribute name. Lowercase for keys.
// Examples:
// $attributes['style'] = 'border:1px solid #ccc;'
// $attributes['title'] = 'My Title';
// $attributes['class'] = 'myClass';
$error = "You need to specify at least one option, otherwise this filter is pointless!";
if(!empty($options)){$phpthmbOptions = explode(",", $options);}else{$output = $error; return $output;}
// Include phpThumb config file.
require_once($modx->getOption('base_path') . $phpthumb_path . '/phpThumb.config.php');
if(isset($PHPTHUMB_CONFIG['high_security_password'])) $_SESSION['high_security_password'] = $PHPTHUMB_CONFIG['high_security_password'];
if(isset($PHPTHUMB_CONFIG['high_security_enabled'])) $_SESSION['high_security_enabled'] = $PHPTHUMB_CONFIG['high_security_enabled'];
$att_out = '';
$src = '';
foreach($phpthmbOptions as $k => $v){
// Skip phpThumb modifications
if($k==0) continue;
// Explode rest of options
$m = explode($attribute_sep, $v);
$m_att = strtolower(trim($m[0]));
// Join attributes except src
$att_out .= ' '.$m_att.'="'.trim($m[1]).'"';
// Remove default attribute if exist in phpThumb options
if(isset($attributes[$m_att])) unset($attributes[$m_att]);
}
// Add rest of unused default attributes
if(isset($attributes)) {
foreach($attributes as $k => $v){
$att_out .= ' '.$k.'="'.$v.'"';
}
}
// Image url + phpthumb modifications
$src = 'src=' . $base . $input . $phpthmbOptions[0];
// If high security is enabled define hash
if($_SESSION['high_security_enabled']){
$output = $base . $phpthumb_path . '/phpThumb.php?' . $src . '&hash=' . md5($src.$_SESSION['high_security_password']);
} else {
$output = $base . $phpthumb_path . '/phpThumb.php?' . $src;
}
// Final join
$output = '<img src="' . $output .'"'. $att_out.'>';
return $output;
?>
[[phpthumb? &input=`[[getResourceField? &id=`33` &field=`imageTV` &isTV=`1`]]` &options=`&w=100&h=50&zc=1,class||myClass,title||myTitle,style||border: 1px solid #333`]]
Thanks Jtech
Awesome additions DJ13! I’ll add these to the main package and github. Wish I had your skills
<?php
// PHPThumb Output Filter - The Dumb way (I'm sure)
//
// By Jared Loman a.k.a jtech - http://www.jaredloman.com
//
// Revision by Djordje Dimitrijev (dj13 on Modx forum)
//
// Revision Notes:
//
// Choose the output from a script:
// 1. Full image tag.
// 2. Only src value (additional attributes will not be used).
// Added possibility use or not high security password from phpThumb config.
// Possibility to add as much as you want default image attributes through the array.
// New way to add attributes in filter:
// attribute_html_name || attribute_value
//
// Examples:
//
// Filter full image tag: [[*mytvImage:phpthumb=`&w=100&h=50&zc=1,class||myClass,title||myTitle,style||border: 1px solid #333`]]
// Filter only src output: [[*mytvImage:phpthumb=`&w=100&h=50&zc=1,onlysrc||1`]]
//
// Snippet full image tag: [[phpthumb? &input=`files/myImage.jpg` &options=`&w=100&h=50&zc=1,class||myClass,title||myTitle`]]
// Snippet only src output v1 (best way if You want only src): [[phpthumb? &input=`files/myImage.jpg` &options=`&w=100&h=50&zc=1` &onlysrc=`1`]]
// Snippet only src output v2: [[phpthumb? &input=`files/myImage.jpg` &options=`&w=100&h=50&zc=1,onlysrc||1`]]
//
// Version 0.2 beta
//
// Current Version Notes:
// Added array to handle "class" & "alt" options with defaults.
// Added a space removal fool-proof in case someone trys to skip the class and add an alt.
// Added Readme
// Still haven't tested all phpthumb modifiers.
//
// Previous Version Notes:
// v.0.1: I haven't tested all of the phpthumb modifiers, but I think they should work
// ReadMe:
//
// Notes:
// I created the PHPThumb output filter so that I could have some flexibility with my images that an output modifier
// couldn't offer me. This output filter is essentially useless for anything other than images (Obviously!), but I
// figured I'd at least mention this.
//
// This does not support PHPThumbs ability to create new images from nothing. It would be kind of silly to use a
// TV and Output filter for this purpose anyhow.
//
// INSTRUCTIONS:
// Step 1.) Create a TV with an input type of "IMAGE" and an output type of "TEXT".
// Step 2.) Add an image to the TV.
// Step 3.) Add your TV to your page content or template and modify in the format of the example.
//
// OPTIONS:
// Option 1.) The first option is the phpthumb modifications. You can add any modifier that phpthumb supports. (AFIK)
// Option 2.) Image Class (optional) - Supply a class for your image. Defaults to "phpthumb" if you don't put anything in.
// Option 3.) Image Alt (optional) - Supply text for the alt tag. Defaults to "phpthumb" if you don't put anything in.
//
// IMPORTANT:
// Options MUST be put in specified order!
// You Must specify a class if you would like a custom ALT. (e.g. [[*myTV:phpthumb=`&w=100,My Alt Text`]] ) will
// return <img src=".." class="MyAltText" alt="phpthumb" />.. Sorry, it's just the nature of the beast.
//
// TV Output Type MUST be "TEXT"!!
//
// Example: [[*mytvImage:phpthumb=`&w=100&h=50&zc=1,imgClass,imgAlt`]]
$phpthumb_path = 'assets/components/phpthumb';
// Define attribute name - value separator
$attribute_sep = '||';
$base = $modx->config['base_url'];
// Default array of attributes with key name like html attribute name. Lowercase for keys.
// Examples:
// $attributes['style'] = 'border:1px solid #ccc;'
// $attributes['title'] = 'My Title';
// $attributes['class'] = 'myClass';
//
// IMPORTANT! If You set:
// $output_onlysrc = 1;
// all returns from snippet will be only src
$output_onlysrc = false;
$error = "You need to specify at least one option, otherwise this filter is pointless!";
if(!empty($options)){$phpthmbOptions = explode(",", $options);}else{$output = $error; return $output;}
// Include phpThumb config file.
require_once($modx->getOption('base_path') . $phpthumb_path . '/phpThumb.config.php');
if(isset($PHPTHUMB_CONFIG['high_security_password'])) $_SESSION['high_security_password'] = $PHPTHUMB_CONFIG['high_security_password'];
if(isset($PHPTHUMB_CONFIG['high_security_enabled'])) $_SESSION['high_security_enabled'] = $PHPTHUMB_CONFIG['high_security_enabled'];
$att_out = '';
$src = '';
// From snippet call
if($onlysrc == 1) {
$output_onlysrc = 1;
}
// If onlysrc is set in snippet call or as default skip secound explode and adding rest of attributes
if($output_onlysrc !=1 ) {
foreach($phpthmbOptions as $k => $v){
// Skip phpThumb modifications
if($k==0) continue;
// Explode rest of options
$m = explode($attribute_sep, $v);
$m_att = strtolower(trim($m[0]));
// Search for onlysrc in options
if($m_att=='onlysrc' && trim($m[1])==1) {
$output_onlysrc = 1;
break;
}
// Join attributes except src
$att_out .= ' '.$m_att.'="'.trim($m[1]).'"';
// Remove default attribute if exist in phpThumb options
if(isset($attributes[$m_att])) unset($attributes[$m_att]);
}
// Add rest of unused default attributes
if(isset($attributes)) {
foreach($attributes as $k => $v){
$att_out .= ' '.$k.'="'.$v.'"';
}
}
}
// Image url + phpthumb modifications
$src = 'src=' . $base . $input . $phpthmbOptions[0];
// If high security is enabled define hash
if($_SESSION['high_security_enabled']){
$output = $base . $phpthumb_path . '/phpThumb.php?' . $src . '&hash=' . md5($src.$_SESSION['high_security_password']);
} else {
$output = $base . $phpthumb_path . '/phpThumb.php?' . $src ;
}
// Final join and output only src or full image tag
if($output_onlysrc ==1) {
return $output;
} else {
$output = '<img src="' . $output .'"'. $att_out.'>';
return $output;
}
?>
Hey DJ13,
Great work again! I just re-submitted the package to the modx repo with your last changes. What might be a practical use of only outputting the image source as opposed to just calling the TV unaltered by scripts? Flexibility is always nice, but I’m a bit slow today so I’m having trouble thinking of a way to use the "source only" feature.
Perhaps I can pick your brain a little bit on something.. The last feature that this snippet is missing is the ability to connect to the phpthumb installation that is already a part of modx. My original implementation of including the whole phpthumb script is a bit bulky and unnecessary. Thankfully phpthumb is a light weight script so it isn’t too big of a deal, but it would be nice to utilize what is already available (similar to Gallery).
I have been studying the modx docs trying to familiarize myself with "connectors" (I think they are what is needed?) but haven’t figured out much. Do you have any ideas on how to accomplish this?
Again, great work! It is really neat to see little ideas grow into useful products when placed in the hands of an excellent community!