I would guess that you need to make the pic an anchor and have the tag read some thing like this:
<a href = template.php?image=xxx><img src = /images/xxx.gif></a>
This way you pass the pic id or whatever you use to uniquely id the pic back to the template page which can then take the variable and concatenate it into the template to show the image.
hth
Bastien