Custom made random foto script for very large gallery's

Roderik

Joined: 2002-11-17
Posts: 7
Posted: Sat, 2003-11-29 19:34

so, i've got a gallery with +5500 photo's in it. And it keeps rising +-2000 a year.

The random blocks here required just way to much php memory and were very slow for such a large number of photo's

Hence, i made my own script :)

it consists of 2 php files and uses a DB. It's linda hardcoded but ge nuts on it :)

first the DB:

CREATE TABLE `sidebar_randomphoto` (
  `ID` int(11) NOT NULL auto_increment,
  `foto` varchar(255) NOT NULL default '',
  `link` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`ID`)
)

the config.inc.php file with some settings, paths and db connection

<?php
// +----------------------------------------------------------------------+
// | DIANA.BE                                                             |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003-2004 Diana                                        |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, that  |
// | is bundled with this package in the file LICENSE, and is available   |
// | through the world-wide-web at http://www.php.net/license/2_02.txt.   |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// |  so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Roderik van der Veer <roderik@vanderveer.be>                |
// +----------------------------------------------------------------------+
//
// $Id: config.inc.php,v 1.1 2003/11/27 14:15:54 0102211 Exp $

// Path Settings

$docroot = '/home/org/diana/services/www/main/';
$wwwroot = '/';

// DB Settings

$DB_host    = "localhost";
$DB_user    = "xxx";
$DB_pass    = "xxx";
$DB_dbName  = "xxx";
$DB_dbType  = "mysql";

// DO NOT TOUCH

ini_set('include_path', ini_get('include_path') . ':' . $docroot . 'includes/pear/:' . ini_get('include_path') . ':' . $docroot . 'includes/:' . ini_get('include_path') . ':' . $docroot . 'includes/jpcache/');

require_once 'DB.php';
$dsn = $DB_dbType . "://" . $DB_user . ":" . $DB_pass . "@" . $DB_host . "/" . $DB_dbName;
$db = DB::connect($dsn, TRUE);
if (DB::isError($db)) {
    die($db->getMessage());
}
?>

then the db fill script (i crontab this every 30 minutes)

<?php
// +----------------------------------------------------------------------+
// | DIANA.BE                                                             |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003-2004 Diana                                        |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, that  |
// | is bundled with this package in the file LICENSE, and is available   |
// | through the world-wide-web at http://www.php.net/license/2_02.txt.   |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// |  so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Roderik van der Veer <roderik@vanderveer.be>                |
// +----------------------------------------------------------------------+
//
// $Id: randomFotoBig.php,v 1.6 2003/11/29 18:46:47 0102211 Exp $

 function findfile($location='',$fileregex='') {
 	GLOBAL $docroot;
   if (!$location or !is_dir($location) or !$fileregex) {
       return false;
   }
 
   $matchedfiles = array();
 
   $all = opendir($location);
   while ($file = readdir($all)) {
       if (is_dir($location.'/'.$file) and $file <> ".." and $file <> ".") {
         $subdir_matches = findfile($location.'/'.$file,$fileregex);
         $matchedfiles = array_merge($matchedfiles,$subdir_matches);
         unset($file);
       }
       elseif (!is_dir($location.'/'.$file)) {
         if (preg_match($fileregex,$file)) {
             array_push($matchedfiles,str_replace('//', '/', str_replace($docroot, '', $location.'/'.$file)));
         }
       }
   }
   closedir($all);
   unset($all);
   return $matchedfiles;
 }
 
 include_once('/home/org/diana/services/www/main/config.inc.php');
 $sql = ("DELETE FROM sidebar_randomphoto");
 $db->query($sql);
 
 $thumbnails = findfile($docroot . 'galerij/albums/','/\.(thumb.gif|thumb.jpg)$/');
 for ($i = 0; $i < 100; $i++) {
 		$foto = rand (0, sizeof($thumbnails));
		$link = str_replace('.thumb.gif', '',str_replace('.thumb.jpg', '', str_replace('galerij/albums/', '', $thumbnails[$foto])));
		//echo $link . '<br>';
 		$db->query("INSERT INTO sidebar_randomphoto (foto, link) VALUES ('".$thumbnails[$foto]."', '".$link."')");
 }
 	
?>

and finaly the "block" script

<?php
// +----------------------------------------------------------------------+
// | DIANA.BE                                                             |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003-2004 Diana                                        |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, that  |
// | is bundled with this package in the file LICENSE, and is available   |
// | through the world-wide-web at http://www.php.net/license/2_02.txt.   |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// |  so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Roderik van der Veer <roderik@vanderveer.be>                |
// +----------------------------------------------------------------------+
//
// $Id: randomFoto.php,v 1.16 2003/11/29 18:43:09 0102211 Exp $

class randomFoto {

  var $image;

  function randomFoto(){
		GLOBAL $db, $docroot;
		$fotos = array();
		$sql = "SELECT foto, link FROM sidebar_randomphoto ORDER BY foto desc LIMIT 0,90";
    $res = $db->query($sql);
    if (DB::isError($res)) { die($res->getMessage()); }
    while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) {
      if (DB::isError($row)) { die($row->getMessage()); }
			array_push($fotos, array($row->foto, $row->link));
		}
		$random = rand (0, 90);
		$this->image = '<center><a href="/galerij/'.$fotos[$random][1].'"><img src="/' . $fotos[$random][0] . '" alt="" border="1"></a></center>';
  }

	 function get(){
    return $this->image;  
  }
}

?>

(the site is coded Object Oriented so the block also)

usage in the site

include_once($docroot . 'includes/modules/sidebar/randomFoto.php');
$foto = new randomFoto();
$photo = $foto->get();

echo $photo;

Hope you guys can use this as a basis for your own sites, and thx to the gallery theme for this great script!

Demo: http://www.diana.be

 
SonicStang

Joined: 2004-01-18
Posts: 3
Posted: Sun, 2004-01-18 22:06

Can I somehow integrate it into a html-site, e.g. into a table?