Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Random Images from ImageGalleryPage on another page type


Reply


4 Posts   1135 Views

Avatar
Chucky2k

Community Member, 32 Posts

29 May 2010 at 12:11am

I am trying to get a random selection of images from all albums on my site to display on the home page. I had some help with the page controller function but I am sure there is a better way to achieve what I am after.

function Pictures() {
    $nrtoshow = (0<$this->NumberToShow) ? $this->NumberToShow : $this->defaults["NumberToShow"];
    $pictures = DB::query("SELECT f.`ID`, f.`Filename`, f.`Name`, g.`Caption`, s.`URLSegment`, s.`MetaTitle`, s.`ParentID`, a.`AlbumName` FROM `ImageGalleryItem` g LEFT JOIN `SiteTree_Live` s ON g.`ImageGalleryPageID` = s.`ID` LEFT JOIN `File` f ON f.`ID` = g.`ImageID` LEFT JOIN ImageGalleryAlbum a on g.`AlbumID` = a.`ID` WHERE f.`ClassName` = 'ImageGalleryImage' ORDER BY RAND() LIMIT 0,$nrtoshow;");
$thumbnails = new DataObjectSet;
foreach ($pictures as $picture){
$thumbnail = new Image($picture);
$thumbnail = $thumbnail->getFormattedImage( "PaddedImage", $this->ThumbnailWidth, $this->ThumbnailHeight);
$thumbnail->setField("GalleryLink", Director::baseURL().$picture["URLSegment"]."/album/".str_replace(" ", "-", strtolower($picture["AlbumName"])));
$thumbnail->setField("Name", $picture["Name"]);
$thumbnail->setField("Title", "Go to the".$picture["MetaTitle"]." - ".$picture["AlbumName"]."album");
         $thumbnails->push($thumbnail);
}
unset($pictures);
      return $thumbnails;
   }

ThumbnailWidth, ThumbnailHeight and NumberToShow are fields that I have added to the page type.
The code above works for the most part although sometimes the thumbnails displayed are not the same as the GalleryLink and Title that is attached to the thumbnail.

Any assistance in correcting/improving this code would be greatly appreciated.

Thanks
David

Avatar
Chucky2k

Community Member, 32 Posts

29 May 2010 at 12:51am

I have since discovered the cause of the mismatched thumbnails and GalleryLink that is retrieved. The original image names that were uploaded all start with the same name, eg: one ImageGalleryPage has several albums with images that all have a file named photo01.jpg. When the thumbnail is being created, not enough of a unique name is being generated and if a thumbnail of the same name already exists it does not get overwritten.

As I said in my previous post I am sure there is a smarter way to do this.

Thanks again.
David

Avatar
UncleCheese

Forum Moderator, 4094 Posts

29 May 2010 at 2:26am

Try:

$number_of_images = 5;
$gallery = DataObject::get_one("ImageGalleryPage"); // if you have more than one, add a filter here
$items = DataObject::get("ImageGalleryItem","ImageGalleryPageID = {$gallery->ID}","RAND()",null,$number_of_images);
return $gallery->GalleryItems(null,$items);

Avatar
Chucky2k

Community Member, 32 Posts

31 May 2010 at 9:39pm

Thanks Uncle Cheese... This works great for one ImageGalleryPage and I can set a filter to choose a gallery, but I was hoping to return random images from all ImageGalleryPages with the function. Is it possible to get this function to do this.

If you have any nudges in the right direction it would be greatly appreciated.

Thanks again
David