Craig McCoy

Programmer / Developer & Zombie Survivalist

MODX Revo Snippet to get the size of a TV image

Feb/2011 23

So to start out with, we have some TVs like title1, description1, image1, title2, image2, description2, etc.

We wanted to be able to do something like this:

[cc lang="php"]
[[!getSize? &type=`height`&tv=`image1`]]
[/cc]

And this would return the height of the TV image1

Here is the getSize snippet I came up with:

[cc lang="php"]
//return 0 if we don't have a valid file
$ret = 0;
//this is where the tv images get stored
$base_image_path = "assets/images/";
//get the id of the current resource
$id = $modx->resource->get('id');
//the ouput of the template variable will be the relative image path
$tempv = $modx->getObject('modTemplateVar',array('name'=> $tv));
$output = $tempv->renderOutput($id);
//get the size of the image
$size = getimagesize($base_image_path.$output);
//if a size exists, default to the height
if($size) {
$ret = $size[1];
if($type == "width") {
$ret = $size[0];
}
}
return ($ret);
[/cc]

[cc lang="php"]
[[!getSize? &type=`height`&tv=`image1`]]
[[!getSize? &type=`width`&tv=`image1`]]
[/cc]
So with this you could get the height of width of any of the image TVs. This is just a small simple thing, but I'm fairly new to MODX, and I think this might help those of you out there just getting started like me.

Also note that this is not part of GD but should just be part of base php 4 & 5.