Visto che non sai se ci sono foto verticali, potresti usare ob_start();
Più o meno così:
Codice PHP:
<?php
$result_photogallery = mysql_query($sql_photogallery,$conn) or die ("errore ".mysql_error());
$row_photogallery = mysql_num_rows($result_photogallery);
ob_start();
while($row_photogallery2=mysql_fetch_array($result_photogallery)):
$formatofoto2 = "images_photogallery/$row_photogallery2[photogallery]";
list($width2,$height2)=getimagesize($formatofoto2);
if (($width2<$height2) || ($width2==$height2)) : ?>
<p class="cropphotoartcorrel_vert">
<a href="images_photogallery/<?=$row_photogallery2['photogallery']?>"
class="fresco"
data-fresco-group="shared_options"
data-fresco-caption="<?=$row_photogallery2['titolo_foto']?>">
[img]images_photogallery/<?=$row_photogallery2['photogallery']?>[/img]
</a>
</p>
<?php endif;
endwhile;
$gallery = ob_get_clean();
if($gallery): ?>
<div class="rettangolo_grigioscuro5px">
<arancio_16px>PHOTOGALLERY</arancio_16px>
</div>
<div class="box_art_correlati">
<?php echo $gallery ?>
</div>
</div>
<?php endif; ?>