Scripts

Automatisch fotoalbum

Naar aanleiding van Arjan Kapteijn zijn fotoalbum, heb ik ook mijn eigen gallery geschreven die net zo gemakkelijk kan worden gebruikt als die van Arjan. Mijn script is alleen iets minder kort. Dit komt omdat ik het zoveel mogelijk m.b.v. OOP gedaan heb. Ik post het script niet volledig oop het forum, omdat er 17 essentiële bestanden zijn. Daarom heb ik een download gemaakt: http://moocanvasshadow.googlecode.com/files/photo_gallery.zip Een aantal voordelen van mijn script zijn: - Er wordt van templates gebruik gemaakt (Momenteel gewoon php templates) - Je kan deze template parser makkelijk vervangen voor smarty oid. - Zend/PEAR class naamgeving. (bv. Lib_Template_Interface) (dus autoload is makkelijk te gebruiken) - Je kan je eigen router/url parser eraan pluggen. Standaard wordt gebruik gemaakt van de $_GET array. - (semi)-OOP - Dankzij Boaz heb je gelijk een mooie image class - Foto's in een mapje plaatsen, en het wordt automatisch herkent - Dus geen database oid nodig. - Gebruik van PHP5 - De fotos worden automatisch geresized en opgeslagen zodat een foto slechts één keer hoeft te worden geresized. Het gebruik is vrij gemakkelijk. Upload een mapje met daarin foto's naar het mapje 'photos/orig'. En als je dan index.php opent, heb je gelijk resultaat. Je kunt een array maken met allerlei opties, die je aan het Lib_Gallery object kan meegeven. In het bestand lib/gallery.php staat deze volledige array met de standaard waardes. met de Lib_Gallery::setView() kan je je eigen template parser doorgeven. Dit moet dan wel een instance van Lib_Template_Interface, zodat Lib_Gallery weet dat het wel die methods kan gebruiken. Dat kan je natuurlijk vrij gemakkelijk doen door een nieuwe class te maken, de interface implementeren en Smarty/Dwoo/etc... te extenden. Met de method Lib_Gallery::setUrlParams() kan je de parameters doorgeven. Deze kan je bijvoorbeeld op deze manier parsen/gebruiken in de map Lib/Gallery vind je 3 bestanden: Albums.php, Dir.php en Photo.php. Dit zijn een soort Controllers. Albums.php zorgt voor de pagina met het overzicht van de mappen, Dir.php zorgt voor de thumbs en Photo.php zorgt voor de pagina met de grotere foto. Bij elke class hoort dan weer een template die je kunt vinden in de map templates.

automatisch-fotoalbum
[b]Let op: dit is niet het gehele script![/b]

[code]
<?php

/**
 * index.php
 *
 */

function galleryAutoload($cls_name){
 	require_once str_replace('_','/',$cls_name).'.php';
}

spl_autoload_register('galleryAutoload');


$gallery = new Lib_Gallery();
$gallery->dispatch();



/**
 * Lib/Gallery.php
 *
 */


class Lib_Gallery {
	
	/**
	 * Enter description here...
	 *
	 * @var array
	 */
	protected $options = array(
		'dir' => 'photos/',
		'thumb' => array( // Thumbs options
			'dir' => 'thumbs/', // in the $this->options['dir'] dir
			'size'  => 150
		),
		'medium' => array( // Medium photo size options
			'dir' => 'medium/', // in the $this->options['dir'] dir
			'size' => 500
		),
		'orig' => array( // The origional photos
			'dir' => 'orig/' // in the $this->options['dir'] dir
		),
		'extensions' => array('.jpg','.gif','.png','.jpeg'), // lowercase file extension
		'view_options' => array( // The view options
			'dir' => 'templates/' // The template dir
		),
		'photos_per_page' => 12,
		'gallery_title' => 'My Gallery'
	);
	
	/**
	 * The params parsed from the URL
	 *
	 * @var array
	 */
	protected $urlParams = array();
	
	/**
	 * The view object / template parser
	 *
	 * @var Lib_Template_Interface
	 */
	protected $view;
	
	public function __construct($options=null){
		if(is_array($options)){
			$this->setOptions($options);
		}elseif(!empty($options)){
			throw new InvalidArgumentException('The $options argument must be an array or must be empty');
		}
		$this->urlParams = (array) $_GET;
	}
	
	/**
	 * Set the options recursively
	 *
	 * @param array $options
	 * @param int $level (recursive level)
	 * @return array
	 */
	protected function setOptions(array $options,$level=0){
		$newOptions = array();
		
		foreach($options as $key => $option){
			if(is_array($option)){
				$newOptions[$key] = $this->setOptions($option,$level++);
			}else{
				$newOptions[$key] = $option;
			}
		}
		if($level === 0){
			$this->options = $newOptions;
		}
		return $newOptions;
	}
	
	/**
	 * Set the URL params. 
	 * By default the $this->urlParams is the $_GET array
	 *
	 * @param array $params
	 */
	public function setUrlParams(array $params){
		$this->urlParams = $params;
	}
	
	/**
	 * If you would like to use smarty/dwoo or an other template engine.
	 * you can extend smarty en implement Lib_Template_Interface and use
	 * smarty/dwoo/templatepower
	 *
	 * @param Lib_Template_Interface $view
	 */
	public function setView(Lib_Template_Interface $view){
		$this->view = $view;
	}
	
	/**
	 * Return or show the gallery
	 *
	 * @param bool $return
	 */
	public function dispatch($return = false){
		if(!($this->view instanceof Lib_Template_Interface)){
			$this->view = new Lib_Template($this->options['view_options']);
		}

		if(!empty($this->urlParams['dir']) && !empty($this->urlParams['photo'])){
			// A dir and a photo is selected
			$page = new Lib_Gallery_Photo($this->urlParams['dir'],$this->urlParams['photo']);
		}elseif(!empty($this->urlParams['dir'])){
			// only a dir is selected
			$page = new Lib_Gallery_Dir($this->urlParams['dir']);
		}else{
			// Nothing at all is selected
			$page = new Lib_Gallery_Albums();
		}
		
		if($page instanceof Lib_Gallery_Page_Interface){		
			$page->setOptions($this->options);
			$page->setUrlParams($this->urlParams);
			$page->setView($this->view);
			$page->init();
			
			if($return){
				return $page->fetch();
			}else{
				echo $page->fetch();
			}
		}else{
			throw new Exception(get_class($page).' is not an instance of Lib_Gallery_Page_Interface');
		}
	}	
	
}
?>
[/code]

Reacties

0
Nog geen reacties.