How about using an array for storage instead of 4 variables?:
$AutomaticallyOpenShow = $AutomaticallyOpen."/";
$images = scandir($AutomaticallyOpen,0);
$counter = 0;
$ImageView = array();
foreach($images as $curimg) {
// the condition could also be optimized
if (strpos($curimg, '.jpg')>0 || strpos($curimg, '.JPG')>0) {
// write result in array at appropriate position
$ImageView[$counter++] = $AutomaticallyOpenShow.$curimg;
}
}
The html code would be like the following:
<img src="<?php echo $ImageView[0]; ?>" width="500" height="500" />