different approach to random block

Half-Dead

Joined: 2002-09-16
Posts: 7
Posted: Tue, 2002-09-17 18:41

Ok the following code won't work with your installation out of the box, but is mainly to show the approach i took to make a fast loading block ..only 2 files to load normaly, and the rest is plain array shuffling.

albumdb.dat is a simple serialized array which contains the names of the galleries.

And in each gallery you have album.dat .. but unfortunately this is a serialized object and without including all the class files from gallery there's no way to access it in a simple manner .. so i do a preg_match_all() and extract all thumbnail names from the code and then play with that:

<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE>
<?php

define("XGAL_BASE_URL" , XOOPS_URL . "/modules/xgallery");
define("XGAL_BASE_PATH", XOOPS_ROOT_PATH . "/modules/xgallery");

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function xGallery_show($options) {

$tmp = @join('', @file(XGAL_BASE_PATH."/cache/albums/albumdb.dat") );
$tmp = @unserialize($tmp);

if ( is_array($tmp) ) {
shuffle($tmp);
$image = parse_albums($tmp);
}

if ( $image['name'] &amp;&amp; $image['gallery'] &amp;&amp; $image['height'] &amp;&amp; $image['width'] ) {
if ( empty($options[0]) || !is_numeric($options[0]) ) {
$options[0] = 150;
}
if ( $image['width'] > $options[0] ) {
$image['height'] = round( ( $image['height'] / ($image['width'] / $options[0]) ) );
$image['width'] = $options[0];
}
$content = "<img src='".XGAL_BASE_URL."/cache/albums/".$image['gallery']."/".$image['name']."' border='0' width='".$image['width']."' height='".$image['height']."' alt='".sprintf(_BL_XGAL_VISIT, $image['gallery'])."'>";
} else {
$content = _BL_XGAL_NOIMG;
}

if ( !empty($image['gallery']) ) {
$link = "view_album.php?set_albumName=".$image['gallery'];
}

$block = array();
$block['title'] = sprintf(_BL_XGAL, $image['gallery']);
$block['content'] = "<div align='center'><a href='".XGAL_BASE_URL."/".$link."'>";
$block['content'] .= $content;
$block['content'] .= "</a></div>";

return $block;
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function parse_albums($albums) {

$acount = count($albums);
for ($i=0; $i<$acount; $i++) {
$image = parse_album_images($albums[$i]);
if ( is_array($image) ) {
$image['gallery'] = $albums[$i];
return $image;
}
}
return false;
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function parse_album_images($album) {

$tmp1 = @join('', @file(XGAL_BASE_PATH."/cache/albums/".$album."/album.dat") );
$pattern = "/s:4:"name";s:9:"([a-z0-9._-]+.thumb)";s:4:"type";s:3:"([a-z]+)";/i";
preg_match_all($pattern, trim($tmp1), $matches);

$mcount = count($matches[1]);
for ($i=0; $i<$mcount; $i++) {
$img[] = $matches[1][$i] .".". $matches[2][$i];
}

if ( is_array($img) ) {
shuffle($img);
$icount = count($img);
for ($i=0; $i<$icount; $i++) {
if ( !is_dir(XGAL_BASE_PATH."/cache/albums/".$album."/".$img[$i]) ) {
$size = @getimagesize(XGAL_BASE_PATH."/cache/albums/".$album."/".$img[$i]);
if ( is_numeric($size[0]) &amp;&amp; is_numeric($size[1]) ) {
$image['name'] = $img[$i];
$image['width'] = $size[0];
$image['height'] = $size[1];
return $image;
}
}
}
}
return false;
}
?>
</TD></TR></TABLE><!-- BBCode End -->

Advantages of above code is that it should be easy to use with any CMS and is extremely fast, disadvantage, is that images are completely random regardless of if they are supposed to be hidded or whatever.

Note: if you include all the class files &amp; config.php like in init.php, you can restructure the whole gallery db and play with that .. but i found it a tad heavy and prefer the preg_match method ..even if i loose on permissions (tho if there was a way to transform the serialized object into a simple array ..that would be even better).

Happy fiddling :grin: