Recently I played with the ModX Revolution add-on phpThumbOf. I thought I would share the code and open it up if anyone has suggestions for a different approach.
My pages have a template variable called mainImage. The input type for this variable is Image, it outputs a path to a selected image.
To place the image on a page that uses the template variable I use the basic code for calling a template variable:
<img src="[[*mainImage]]" />
If I want to use phpThumbOf to create and display a 240x120 version of the image I use:
<img src="[[*mainImage:phpthumbof=`w=240&h=120`]]" />
Now let’s say I want to find out the mainImage template variable from a different page and display a thumbnail version of it. First off, to be able to get the template variable from another resource I install the getResourceField add-on. For this example we can say I am trying to get the mainImage variable from the resource with the id 8. If I just wanted to display the full image I could use:
<img src="[[getResourceField? &id=`8` &field=`mainImage` &isTV=`1`]]" />
I can now use both getResourceField and phpthumbof to create the thumbnail:
<img src="[[getResourceField:phpthumbof=`w=240&h=120`? &id=`8` &field=`mainImage` &isTV=`1`]]" />
For extra credit I am going to slip in the option with getResourceField to have a default image in case the mainImage template variable is empty:
<img src="[[getResourceField:default=`/images/placeholder.jpg`:phpthumbof=`w=240&h=120`? &id=`8` &field=`mainImage` &isTV=`1`]]" />
Hope this helps!
The code above was updated April 12, 2011 to incorporate the solution from Mark H. to properly use the output modifier. The previous version used the snippet below which you will see referred to in the comments.
My solution was to create a snippet to do the thumbnail processing. I created a snippet called createThumb and gave it the code:
$options = "w=$width&h=$height";
$thumbnail = $modx->runSnippet("phpthumbof",array('input' => $input, 'options' => $options));
echo $thumbnail;
Now I can use this to create the thumbnail:
<img src="[[!createThumb? &input=`[[getResourceField? &id=`8` &field=`mainImage` &isTV=`1`]]` &width=`240` &height=`120`]]" />