Drupal Random Image Block

I have a website devoted to Italian horror movie posters and I wanted a way to add a block in the righthand sidebar that would display random images.

There were a couple of problems with this. Just displaying a random image is not difficult using the image_get_random function but I wanted a way to specify galleries to use. I wanted to have one block that chose a random image from my giallo movie collection. I wanted a second block to pick movie images from two other photo galleries - either "Italian Horror Films" or "Other Italian Films".

For the first block, I used the following code to pull a random image from my Giallo films photo gallery:

<?php
$gallery_name='Giallo films';
$term = taxonomy_get_term_by_name($gallery_name);
$tid = $term[0]->tid;
$thumbs = 0;
while ($thumbs<1) {
$images = (image_get_random($count = 1, $tid));
print l(image_display($images[0], 'embedded'),'node/'.$images[0]->nid, array(), null, null, FALSE, TRUE);
$thumbs++;
}
?>

I then went to the Admin page in Drupal, then to Blocks, and chose Add Block. I pasted the code into the block and saved it then enabled it in the right sidebar.

I then wanted an additional block that would select from the other two specific galleries and display that image. To do that, I first pick a random number, either 1 or 2, and depending on that result, it decides which of the two galleries to pull images from. It then pulls a random image from whatever gallery it has selected:

<?php
srand(time());
$random = (rand()%2);
if ($random==0) {$gallery_name='Italian Horror films (non-giallo)';}else {$gallery_name='Other Italian films';}
$term = taxonomy_get_term_by_name($gallery_name);
$tid = $term[0]->tid;
$thumbs = 0;
while ($thumbs<1) {
$images = (image_get_random($count = 1, $tid));
print l(image_display($images[0], 'embedded'),'node/'.$images[0]->nid, array(), null, null, FALSE, TRUE);
$thumbs++;
}?>

I just added this code to a second block and placed it under the first one on the righthand sidebar.

To see the finished product you can check out http://www.gialloshots.com and you'll see the random image block in the righthand sidebar. Be warned that some of the images are NSFW as Italian film posters have more liberal standards for what can be publically displayed.