| // | Groucho Marx | // | Leandro Lucarella | // +--------------------------------------------------------------------+ // // $Id$ // require_once 'PEAR.php'; require_once 'Util.php'; require_once 'Image/Transform.php'; require_once 'HTML/Template/Sigma.php'; /** * Generates a photo album. * * This library generates a photo album dinamically. * * @package Hooks * @subpackage Album * @author Harpo Maxx * @author Groucho Marx * @author Leandro Lucarella * @version $Rev$ * @since rev 130 * @access public */ class Hook_Album { var $root; var $recursive; var $thumbsdir; var $thumbsformat; var $exts; /** * Constructor. * * @param string $root Root directory. * same row. * @param bool $recursive True if subdirectories must be * proccessed. * @param string $thumbsdir Directory where the thumbnails are (or * will be generated). * @param array $exts Comma separated extensions of photo files. * * @access public */ function Hook_Album($root = '.', $recursive = true, $thumbsformat = 'jpeg', $thumbsdir = '.thumbs', $exts = 'png,jpg,jpeg,gif') { $this->root = $root ? $root : '.'; $this->recursive = $recursive; $this->thumbsdir = $thumbsdir; $this->thumbsformat = $thumbsformat; $this->exts = preg_split('/\s*,\s*/', $exts); } /** * Shows the photo album. * * Shows the photo album. * * @param string $photo Selected photo. * @param int $max Maximum quantity of thumbnails to show. 0 for * unlimited. * @param int $cols How many thumbnails are shown in the * * @access public * @todo Implement the 'max' feature! * @todo Make the thumbs cache before showing the images so the * browser can find the with having to refresh. */ function album(&$tpl, $photo = '', $max = 0, $cols = 4) { $tpl->loadTemplateFile('bife_album.html'); $tpl->addBlockFile('ITEMS', 'ITEM', 'bife_album_item.html'); $root = $this->root; $list = $this->getList(); $tot = count($list); $rows = ceil($tot / $cols); for ($row = 0; $row < $rows; $row++) { for ($col = 0; $col < $cols; $col++) { $cur = $row*$cols+$col; if ($photo = @$list[$cur]) { $selected = ($photo['file'] === $photo); if (is_null($photo['thumb'])) { $photo['thumb'] = $this->makeThumb($photo['file']); } $tpl->setVariable( array( 'PHOTO' => $photo['file'], 'DESC' => $photo['desc'], 'THUMB' => $photo['thumb'], ) ); $tpl->parse('ITEM'); } else { if (!@$empty) { $tpl->replaceBlockFile('ITEM', 'bife_album_emptyitem.html', true); $empty = true; } $tpl->touchBlock('ITEM'); $tpl->parse('ITEM'); } } $tpl->parse('FILA'); } $tpl->setVariable('DESC', $this->getDescription()); return $tpl->get(); } /** * Shows a photo. * * Shows a photo. * * @param string $photo Photo to show. * * @access public * @since rev 131 */ function photo($photo) { list($path, $name, $ext) = File_Util::splitFilename($photo); $tpl =& new HTML_Template_Sigma('.'); $tpl->loadTemplateFile('photo.html'); $tpl->setVariable( array( 'DESC' => $name, 'PHOTO' => $photo, ) ); $tpl->show(); } /** * Makes a photo thumbnail. * * @param string $photo Source photo to create the thumbnail. * @param int $size Maximum thumbnail size. * * @return bool True on success, false on failure. * * @access public * @since rev 131 */ function makeThumb($photo, $size = 100) { $format = $this->thumbsformat; $thumb = $this->getThumbName($photo); list($path, $name, $ext) = File_Util::splitFilename($thumb); $img =& Image_Transform::factory('GD'); $img->load($photo); // If image is larger than the maximum size, we resize it. if ($img->img_x > $size or $img->img_y > $size ) { if (!is_dir($path) and !mkdir($path)) { return false; } if (PEAR::isError($img)) { return false; } if (!$img->scale($size)) { return false; } } $img->save("$path/$name.$format", $format); $img->free(); return true; } /** * Gets a list of photos with their descriptions and thumbnails. * * Returns an array of associative arrays with this keys: *
    *
  • file: Photo filename.
  • *
  • desc: Photo Description.
  • *
  • thumb: Photo thumbnail filename.
  • *
* * @return array Array of associative arrays with file, desc and * thumb. * * @access public */ function getList() { $root = $this->root; $exts = $this->exts; $format = $this->thumbsformat; $return = array(); $d = dir($root); while (($file = $d->read()) !== false) { list($path, $name, $ext) = File_Util::splitFilename("$root/$file"); if (is_readable("$root/$file") and in_array($ext, $exts)) { $thumb = $this->getThumbName("$root/$file"); $return[] = array( 'file' => "$root/$file", 'desc' => $name, 'thumb' => is_readable($thumb) ? $thumb : null, ); } } $d->close(); return $return; } /** * Gets a photo thumbnail name. * * @param string $photo Source photo to create the thumbnail. * * @return string Thumbnail name. * * @access public * @since rev 131 */ function getThumbName($photo) { $root = $this->root; $format = $this->thumbsformat; $thumbsdir = $this->thumbsdir; list($path, $name, $ext) = File_Util::splitFilename($photo); return "$root/$thumbsdir/$name.$format"; } /** * Creates album thumbnails. * * @since rev 147 * @access public */ function createThumbs() { foreach ($this->getList() as $photo) { if (is_null($photo['thumb'])) { $photo['thumb'] = $this->makeThumb($photo['file']); } } } /** * Sets the album description. * * @param string $desc New album description. * * @returns bool True on success, false on failure. * * @since rev 147 * @access public */ function setDescription($desc) { $root = $this->root; $out = fopen("$root/.album", 'w'); $ret = fputs($out, $desc); fclose($out); return $ret ? true : false; } /** * Gets the album description. * * @returns string Album description. * * @since rev 149 * @access public */ function getDescription() { $root = $this->root; return file_get_contents("$root/.album"); } } // $Id$ ?>