Can I show random photos from SPECIFIC album?

jhdesign

Joined: 2004-08-03
Posts: 26
Posted: Sat, 2004-08-14 16:24

I'm using block-random in Mambo and would like to show random photos from a specific album only. Is this doable?

Thanks.

 
verdon

Joined: 2002-08-19
Posts: 25
Posted: Tue, 2004-08-17 02:11

I'm not sure how you'd do that in Mambo, but I did something like that today in a non-CMS site. Pretty straight fwd, though there may be a better way...

...in the page of my site I am calling block-random from, I set a var for the album I want to draw from

$loadthisalbum = "scenes";
include("gallery/block-random.php");

...then in block-random.php look for

$album = chooseAlbum();

somewhere around line 71.

...comment that out and just below it, add the following

//$album = chooseAlbum();

if ($loadthisalbum) {
	$album = new Album();
	$album->load($loadthisalbum);
} 

Works for me :)

 
AlpineZone
AlpineZone's picture

Joined: 2004-01-21
Posts: 69
Posted: Tue, 2004-08-17 14:12

Is there a way to specifiy a particular album and then have block-random.php display images from nested albums in the specified album?

 
fatcuban

Joined: 2004-08-19
Posts: 3
Posted: Thu, 2004-08-19 14:52

alright, so i've just sorted this out myself, as it's exactly what i wanted...

i've only done this for the standalone version, and i've hardcoded in the album i want to change too... easily modified...

right at the bottom of block-random.php, inside the function scanAlbums() you have this bit of code...

if ($numPhotos > 0) {
    $cache[$name] = $numPhotos;
}

you want to replace it with this...

$myalbum="randompicturesinallsubalbumsofthis";
if ($numPhotos > 0) {
    if ($tmpAlbum->fields["parentAlbumName"]==$myalbum && $tmpAlbum->fields["parentAlbumName"]!="0") {
        $cache[$name] = $numPhotos;
    }
}

this will re-initialise the cache with only albums that are sub-albums of your album... however, it won't take sub-albums and sub-albums... i don't think...

you may have to declare $rebuild=1; at the top to rebuild the cache if you've been working on it recently...

 
AlpineZone
AlpineZone's picture

Joined: 2004-01-21
Posts: 69
Posted: Thu, 2004-08-19 17:29
fatcuban wrote:
i've only done this for the standalone version, and i've hardcoded in the album i want to change too... easily modified...

Is there a way to specify the album with a variable as in verdon's post above?

 
justchil
justchil's picture

Joined: 2003-02-07
Posts: 225
Posted: Thu, 2004-08-19 18:13

There is with an older version of random photo block. It allows you to do an include like this:

include_once("http://www.yourdomain.net/gallery/block-random.php?album=foo");

I don't think the random photo block in 1.4.4 allows this option.

 
justchil
justchil's picture

Joined: 2003-02-07
Posts: 225
Posted: Thu, 2004-08-19 18:15

Here is the code from the older block-random.php. It also has the option of setting the image size and the target.

<?

// Hack prevention.
if (!empty($HTTP_GET_VARS["GALLERY_BASEDIR"]) ||
        !empty($HTTP_POST_VARS["GALLERY_BASEDIR"]) ||
        !empty($HTTP_COOKIE_VARS["GALLERY_BASEDIR"])) {
    print "Security violation\n";
    exit;
}

require($GALLERY_BASEDIR . "init.php");

if ($profile) {
    $timer = time();
}

/* Initializing the seed */
srand ((double) microtime() * 1000000);

define(CACHE_FILE, $gallery->app->albumDir . "/block-random.cache");
define(CACHE_EXPIRED, 86400);

// Check the cache file to see if it's up to date
$rebuild = 1;
if (fs_file_exists(CACHE_FILE)) {
    $stat = fs_stat(CACHE_FILE);
    $mtime = $stat[9];
    if (time() - $mtime < CACHE_EXPIRED) {
    $rebuild = 0;
    }
}

if ($rebuild) {
    scanAlbums();
    saveCache();
} else {
    readCache();
}

// Parameter: size
// Specify that the thumbnail image should be resized when displayed, 
// to fit a specific dimension in both height and width. Aspect ratio is maintained.
// Intended to reduce the size of images, but if the size specified is larger than either thumbnail dimension, 
// the image will be enlarged (with a corresponding loss of quality.)
// Note: This does not create an additional thumbnail, nor does it alter the existing thumbnail; 
// the image is dynamically scaled by the browser when rendered.
// Example: size=75
// Default: 0, meaning no resize.

if (!empty($HTTP_GET_VARS["size"]))
	$size = $HTTP_GET_VARS["size"];
else
      $size = 200;

// Parameter: domain
// Specify the value to which document.domain should be set.
// Not needed if this page and the page calling it are on the same server.
// Allows the page embedding this random photo page to resize the containing IFRAME, 
// when the two pages are on different servers, but within a common domain (e.g. "www.foo.com" and "gallery.foo.com").
// The value specified must be the common suffix of the two server names, cannot be simply ".com",
// and the calling page must set its document.domain to the same value.
// Example: domain=foo.com
// Default: none. If not specified, no document.domain statement will be executed.

if (!empty($HTTP_GET_VARS["domain"]))
	$domain = $HTTP_GET_VARS["domain"];

// Parameter: target

if (!empty($HTTP_GET_VARS["target"]))
	$targetAttr = "target=".$HTTP_GET_VARS["target"];
else
      $targetAttr = "target='parent'";

// Parameter: album
// Limit the photo block to only one album (including its sub-albums). 
// Example: album=nytrip
// Default: none.  If not specified, all visible albums will be used.

if (!empty($HTTP_GET_VARS["album"]))
    {
    $album = new Album();
    $album->load($HTTP_GET_VARS["album"]);
    }
else
    $album = chooseAlbum();

if ($album) {
    $index = choosePhoto();
}

if (isset($index)) {
    $id = $album->getPhotoId($index);
    
    echo ""
        ."<a href=\"" . makeAlbumUrl($album->fields["name"], $id) . "\">"
        .$album->getThumbnailTag($index,$size)
        ."</a>";
    
    echo "";
} else 
   {
   // No photo chosen.  Provide link to album itself, otherwise to top-level of gallery.
   if ($album) {
      echo " "
          ."<a href=" .makeAlbumUrl($album->fields["name"]) ." $targetAttr>"
          .$album->getHighlightAsThumbnailTag($size)
          ."</a><span class=caption><br>"
          ."<a href=" .makeAlbumUrl($album->fields["name"]) ." $targetAttr>"
          .$album->fields["title"]
          ." ";
   } else {
      print  "<a href=\"" . makeGalleryUrl("albums.php") . "\" $targetAttr>" . $gallery->app->galleryTitle . "</a> "; 
   }

//    print "No photo chosen.";
}

if ($profile) {
    $elapsed = time() - $timer;
    print "<br>Elapsed: $elapsed secs";
}

/*
* --------------------------------------------------
* Support functions
* --------------------------------------------------
*/

function saveCache() {
    global $cache;
    if ($fd = fs_fopen(CACHE_FILE, "w")) {
    foreach ($cache as $key => $val) {
        fwrite($fd, "$key/$val\n");
    }
    fclose($fd);
    }
}

function readCache() {
    global $cache;
    if ($fd = fs_fopen(CACHE_FILE, "r")) {
    while ($line = fgets($fd, 4096)) {
        list($key, $val) = explode("/", $line);
        $cache[$key] = trim($val);
    }
    fclose($fd);
    }
}

function choosePhoto() {
    global $cache;
    global $album;

    $count = $cache[$album->fields["name"]];
    if ($count == 0) {
    // Shouldn't happen
    return null;
    } else if ($count == 1) {
    $choose = 1;
    } else {
    $choose = rand(1, (int) $count);
    $wrap = 0;

    // Skip a photo if it is hidden
    while ( $album->isHidden($choose) ) {
        $choose++;
        if ($choose > $count) {
           $choose = 1;
           $wrap++;

           if ($wrap == 2) {
              return null;
           }
        }
    }
    }

    /*
     * If we've picked a sub-album, then 
     * make it the chosen album, and 
     * recursively choose a photo from *it*
    */
    $isSubAlbum = FALSE;
    $subAlbumName = '';
    
    // Backwards compatibility was lost in v1.4.3     

    if (method_exists($album, 'isAlbum')) {
        // Gallery v1.4.3 or later
        if ( $album->isAlbum($choose) ) {
            $isSubAlbum = TRUE;
            $subAlbumName = $album->getAlbumName($choose);
        }
    } 
    else {
        // Gallery v1.4.2 or earlier
        if ( $album->isAlbumName($choose) ) {
            $isSubAlbum = TRUE;
            $subAlbumName = $album->isAlbumName($choose);
        }
    }
        
    if ( $isSubAlbum ) {
       $album->load($subAlbumName);
       return choosePhoto();
    } 

    return $choose;
}

function chooseAlbum() {
    global $cache;

    /*
     * The odds that an album will be selected is proportional
     * to the number of (visible) items in the album.
     */

    $total = 0;
    foreach ($cache as $name => $count) {
    if (!$choose) {
        $choose = $name;
    }
    
    $total += $count;
    if ($total != 0 && ($total == 1 || rand(1, $total) <= $count)) {
        $choose = $name;
    }
    }

    if ($choose) {
    $album = new Album();
    $album->load($choose);
    return $album;
    } else {
    return null;
    }
}

function scanAlbums() {
    global $cache;
    global $gallery;

    $cache = array();
    $everybody = $gallery->userDB->getEverybody();
    $albumDB = new AlbumDB();
    foreach ($albumDB->albumList as $tmpAlbum) {
    if ($everybody->canReadAlbum($tmpAlbum)) {
        $seeHidden = $everybody->canWriteToAlbum($tmpAlbum);
        $numPhotos = $tmpAlbum->numPhotos($seeHidden);
        $name = $tmpAlbum->fields["name"];
        if ($numPhotos > 0) {
        $cache[$name] = $numPhotos;
        }
    }
    }
}
?>
 
fatcuban

Joined: 2004-08-19
Posts: 3
Posted: Fri, 2004-08-20 08:05
AlpineZone wrote:
fatcuban wrote:
i've only done this for the standalone version, and i've hardcoded in the album i want to change too... easily modified...

Is there a way to specify the album with a variable as in verdon's post above?

well sure, instead of defining $myalbum, run a global on it, and then define $myalbum in your script before you include the random block... this should pass across the variables (i believe) although i haven't tested it.

i don't believe you can include files with a query string on the end of them. you could probably even define the specific album in the config file, but either way i think you'll have to make the variable global to allow the function to read it properly...

 
AlpineZone
AlpineZone's picture

Joined: 2004-01-21
Posts: 69
Posted: Fri, 2004-08-20 17:03

This doesn't seem to be working for me. This is what I did:

In block-random.php, Find:

if ($numPhotos > 0) { 
    $cache[$name] = $numPhotos; 
}

Replace with:

if ($numPhotos > 0) { 
    if ($tmpAlbum->fields["parentAlbumName"]==$nestedalbum && $tmpAlbum->fields["parentAlbumName"]!="0") { 
        $cache[$name] = $numPhotos; 
    } 
}

I included the block with this:

<? 
$nestedalbum = "hiking";
include ("http://forums.alpinezone.com/modules.php?op=modload&name=gallery&file=index&include=block-random.php"); 
?>

(My Gallery is embedded in phpBB.)

It just displays images like the regular random block. What am I missing?

 
justchil
justchil's picture

Joined: 2003-02-07
Posts: 225
Posted: Fri, 2004-08-20 17:14

You can include with a query string .. but the newest 1.4.4 block-random doesn't support this. You can hack it so it does..

It makes it nice because you can use the same block-random on many pages and show a different album on each page.

I may be misunderstanding you guys but I really think you're making it harder than it should be ;)

 
fatcuban

Joined: 2004-08-19
Posts: 3
Posted: Sat, 2004-08-21 06:45

well no, that's why i put that code up, it's an extra 3 lines and an extra variable...

alpinezone, did you put

global $nestedalbum

in that function? the scope of variables inside a function won't pickup the variable otherwise...

 
AlpineZone
AlpineZone's picture

Joined: 2004-01-21
Posts: 69
Posted: Sat, 2004-08-21 11:01
fatcuban wrote:
well no, that's why i put that code up, it's an extra 3 lines and an extra variable...

alpinezone, did you put

global $nestedalbum

in that function? the scope of variables inside a function won't pickup the variable otherwise...

In the include statement or in block-random.php?

 
AlpineZone
AlpineZone's picture

Joined: 2004-01-21
Posts: 69
Posted: Fri, 2004-08-27 18:00
AlpineZone wrote:
fatcuban wrote:
well no, that's why i put that code up, it's an extra 3 lines and an extra variable...

alpinezone, did you put

global $nestedalbum

in that function? the scope of variables inside a function won't pickup the variable otherwise...

In the include statement or in block-random.php?

Any thoughts here?