Here's a quick draft. Copy the function theme_display_image from themes/sample/theme.php to your theme's theme.php file, if it doesn't exist. Then, find
starttable();
echo $picture;
endtable();
and below, add
// Display random pictures from the same album
if ($superCage->get->testInt('cat')) {
$album = abs($superCage->get->getInt('cat'));
} elseif ($superCage->get->testInt('album')) {
$album = $superCage->get->getInt('album');
} else {
$album = mysql_result(cpg_db_query("SELECT aid FROM {$CONFIG['TABLE_PICTURES']} WHERE pid = ".$superCage->get->getInt('pid')), 0);
}
global $RESTRICTEDWHERE;
$result = cpg_db_query("SELECT * FROM {$CONFIG['TABLE_PICTURES']} AS r INNER JOIN {$CONFIG['TABLE_ALBUMS']} AS a ON a.aid = r.aid $RESTRICTEDWHERE AND pid != ".$superCage->get->getInt('pid')." AND r.aid = $album AND approved = 'YES' ORDER BY RAND() LIMIT 4");
if (mysql_num_rows($result)) {
starttable(-1, 'Similar pictures');
echo '<tr width="100%">';
while ($row = mysql_fetch_assoc($result)) {
$pic_url = get_pic_url($row, 'thumb');
echo "<td class=\"thumbnails\" align=\"center\"><a href=\"displayimage.php?pid={$row['pid']}#top_display_media\"><img class=\"image\" src=\"$pic_url\" /></a></td>";
}
endtable();
}
mysql_free_result($result);