This is a little difficult without the page to debug, but I think something like this would work:
<?php
$curCount = 0;
function getSizeClass() {
global $curCount;
if ($curCount<3) {
$curClass = "bgdImage";
} else {
$curClass = "bgdImage twoSqaure";
}
$curCount++;
if ($curCount > 4) $curCount = 0;
return $curClass;
}
?>