Here's a block that works like random image, but pulls the images from a chosen gallery/category.

  $gallery_name='Enter the name of the gallery here';
  $term = taxonomy_get_term_by_name($gallery_name);
  $tid = $term[0]->tid;
  $thumbs = 0;
  $images_arr = (image_get_random($count = $thumbs, $tid));
  foreach ($images_arr as $images){
      print l(image_display($images, 'thumbnail'),'node/'.$images->nid, array(), null, null, FALSE, TRUE);
  }

An alternative version slightly modified to display only the image. This function just shows the image without any links.

  $gallery_name='Enter the name of the gallery here';
  $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 image_display($images[0], 'preview');
    $thumbs++;
  }

Comments

isaev’s picture

<?php
  $gallery_name='Enter the name of the gallery here';
  $term = taxonomy_get_term_by_name($gallery_name);
  $tid = $term[0]->tid;
  $thumbs = 4;
  $images_arr = (image_get_random($count = $thumbs, $tid));
  foreach ($images_arr as $images){
      print l(image_display($images, 'thumbnail'),'node/'.$images->nid, array('html'=>TRUE));
  }
?>
marlowx’s picture

do include only showing images that have a certain 5-star rating? or perhaps images that are rated 3 stars or higher so that the random images are chosen from the better quality images?

this would be incredibly useful....

mogop’s picture

How to put html tags around the images so I can style them?

Deterius’s picture

I can't get it to work-
I create a block, enable PHP input- paste the following code, except I replace the name of the gallery in the right area.

  $gallery_name='The People';
  $term = taxonomy_get_term_by_name($gallery_name);
  $tid = $term[0]->tid;
  $thumbs = 0;
  $images_arr = (image_get_random($count = $thumbs, $tid));
  foreach ($images_arr as $images){
      print l(image_display($images, 'thumbnail'),'node/'.$images->nid, array(), null, null, FALSE, TRUE);
  }

But dosent work.

steff2009’s picture

Hi, I have exactly the same problem: created a new block with following php code but it's not visible anywhere:

  $gallery_name='Progettazione Hotel';
  $term = taxonomy_get_term_by_name($gallery_name);
  $tid = $term[0]->tid;
  $thumbs = 0;
  $images_arr = (image_get_random($count = $thumbs, $tid));
  foreach ($images_arr as $images){
      print l(image_display($images, 'thumbnail'),'node/'.$images->nid, array(), null, null, FALSE, TRUE);
  }

What else do I have to configure or add? I'm using Drupal 6 and Image module 6.x-1.0.

Thanks for your support.

giuseppe7x’s picture

It works on Drupal 6! You must enable the module PHP filter, then create a new block pasting the above code and selecting PHP Code as input format.