The code itself is self descriptive. If you still don’t understand how it works, then plz shoot me a comment.
Most probably I will get back to you ![]()
<?php
/**
* Directory2Gallery - Gallery maker
* Do you need to create photo gallery from all the images recursivley kept in
* different folders? Do you like to filter those files? Well, you can do it rite away!
* just copy this index.php file and paste it in your desired location. Point your browser to it
* And see the fun!!
*
* This file contains some code used only when you drop this file in any folder to access the
* folder for viewing as gallery. Optionally you may use jQuery tools overlay or something
* like that to add overlay effect. Owh by the way NO HTACCESS IS REQUIRED!
*
*
* @package Directory2Gallery
* @category Class
* @license LGPL
* @since Version 1.3
* @author Nurul Ferdous <nurul.ferdous@gmail.com>
* @link http://dynamicguy.com
* @todo Overlay implementation
* @see http://flowplayer.org/tools/demos/overlay/index.html
*
*/
class Dir2Gallery
{
private $_limit;
protected $_randomized;
protected $_maxWidth, $_maxHeight;
/**
*
* @param Array $whiteLists Allowed file extensions
* @param Integer $limit
* @param Boolean $randomize whether to randomize the resultset or not
* @param Integer $maxWidth maximum width of an image
* @param Integer $maxHeight maximum height of an image
*/
public function __construct($whiteLists, $limit, $randomize, $maxWidth, $maxHeight)
{
$this->_init($limit, $randomize, $maxWidth, $maxHeight);
$this->makeGallery($whiteLists);
}
/**
* Initializing common resources
*
* @param Integer $limit
* @param Boolean $randomize whether to randomize the resultset or not
* @param Integer $maxWidth maximum width of an image
* @param Integer $maxHeight maximum height of an image
*
*/
private function _init($limit, $randomize, $maxWidth, $maxHeight)
{
$this->_limit = $limit;
$this->_randomized = $randomize;
$this->_maxWidth = $maxWidth;
$this->_maxHeight = $maxHeight;
}
/**
* Display the Galllery. All the magic happens here!
*
* @param Array $whiteLists Allowed file extensions
* @return Void Alternatively you can return the value like this: {return $html;}
*/
public function makeGallery($whiteLists)
{
$images = $this->_getCleanedFiles($whiteLists);
if($images) {
$html = '<ul>';
foreach($images as $image) {
$html .= '<li style="display:inline;margin: 5px 10px 5px 0">';
$html .= $this->_scaleImage($image['path'], $this->_maxWidth, 100, $image['name']);
$html .= '</li>';
}
$html .= '<ul>';
}else {
$html = "Oops! our bad!! you haven't put any files in your directory. Please feed'em some files dude!!!";
}
echo $html;
}
/**
* Scale the image proportionately for the thumbnail image
*
* @param String $p Image path
* @param Integer $mw Maxumum width for an image
* @param Integer $mh Maxumum height for an image
* @param String $title
* @return String
*/
private function _scaleImage($p,$mw='',$mh='', $title='') {
if(list($w,$h) = @getimagesize($p)) {
foreach(array('w','h') as $v) { $m = "m{$v}";
if(${$v} > ${$m} && ${$m}) { $o = ($v == 'w') ? 'h' : 'w';
$r = ${$m} / ${$v}; ${$v} = ${$m}; ${$o} = ceil(${$o} * $r); } }
return("<a href='{$p}'><img style='padding:4px;border:1px solid #ddd;' src='{$p}' alt='{$title}' title='{$title}' width='{$w}' height='{$h}' /></a>");
}
}
/**
* A little house keeping
*
* @param Array $filter the whitelist of allowed files
* @return Array
*/
private function _getCleanedFiles($filter) {
$dirtyArray = $this->_directoryToArray('.', true);
foreach($dirtyArray as $data) {
if(is_file($data)) {
// get the file extension by taking everything after the last dot
$extension = end(explode('.',$data));
// if there is no filter set or the filter is set and matches
if(in_array($extension, $filter)) {
// add the file details to the file list
$cleanArray[] = array(
'path' => $data,
'name' => end(explode('/',$data)),
'extension' => $extension,
'size' => filesize($data),
'mime' => mime_content_type($data)
);
}
}
}
return $cleanArray;
}
/**
*
* @param String $directory the directory path
* @param Boolean $recursive whether to grab files recursively or not
* @return Array
*/
private function _directoryToArray($directory = '.', $recursive = TRUE) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory. "/" . $file)) {
if($recursive) {
$array_items = array_merge($array_items, $this->_directoryToArray($directory. "/" . $file, $recursive));
}
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
} else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
if($this->_randomized) shuffle($array_items);
return array_slice($array_items, 0, $this->_limit);
}
}
// initializing the class
$gallery = new Dir2Gallery( array('jpg', 'jpeg', 'gif', 'png'), 5, TRUE, 100, 100);
/* End of file index.php */
/* Location: ./index.php */
4 Responses
Leave a Reply
Can you add tis feature http://www.huddletogether.com/projects/lightbox2/ ???
@Lucian, yeah I can.
line 79 have to and the Tag and not open a new one.
and if your php version did not support the function “mime_content_type” you can easily commend this line out and the script will work
Great job
I made a photo gallery using this class and it’s working perfectly.