2 // vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
3 // +--------------------------------------------------------------------+
5 // +--------------------------------------------------------------------+
6 // | This file is part of Hooks. |
8 // | Hooks is free software; you can redistribute it and/or modify it |
9 // | under the terms of the GNU General Public License as published by |
10 // | the Free Software Foundation; either version 2 of the License, or |
11 // | (at your option) any later version. |
13 // | Hooks is distributed in the hope that it will be useful, but |
14 // | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 // | General Public License for more details. |
18 // | You should have received a copy of the GNU General Public License |
19 // | along with Hooks; if not, write to the Free Software Foundation, |
20 // | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 // +--------------------------------------------------------------------+
23 // | Authors: Harpo Maxx <harpo@lugmen.org.ar> |
24 // | Groucho Marx <groucho@lugmen.org.ar> |
25 // | Leandro Lucarella <luca@lugmen.org.ar> |
26 // +--------------------------------------------------------------------+
31 require_once 'Image/Transform.php';
32 require_once 'HTML/Template/Sigma.php';
35 * Generates a photo album.
37 * This library generates a photo album dinamically.
41 * @author Leandro Lucarella <luca@lugmen.org.ar>
42 * @author Harpo Maxx <harpo@lugmen.org.ar>
43 * @author Groucho Marx <groucho@lugmen.org.ar>
59 * @param string $root Root directory.
61 * @param bool $recursive True if subdirectories must be
63 * @param string $thumbsdir Directory where the thumbnails are (or
65 * @param array $exts Comma separated extensions of photo files.
69 function Hook_Album($root = '.', $recursive = true, $thumbsformat = 'jpeg', $thumbsdir = '.thumbs', $exts = 'png,jpg,jpeg,gif') {
70 $this->root = $root ? $root : '.';
71 $this->recursive = $recursive;
72 $this->thumbsdir = $thumbsdir;
73 $this->thumbsformat = $thumbsformat;
74 $this->exts = preg_split('/\s*,\s*/', $exts);
78 * Shows the photo album.
80 * Shows the photo album.
82 * @param string $photo Selected photo.
83 * @param int $max Maximum quantity of thumbnails to show. 0 for
85 * @param int $cols How many thumbnails are shown in the
88 * @todo Implement the 'max' feature!
89 * @todo Make the thumbs cache before showing the images so the
90 * browser can find the with having to refresh.
92 function album(&$tpl, $photo = '', $max = 0, $cols = 4) {
93 $tpl->loadTemplateFile('bife_album.html');
94 $tpl->addBlockFile('ITEMS', 'ITEM', 'bife_album_item.html');
97 $list = $this->getList();
99 $rows = ceil($tot / $cols);
101 for ($row = 0; $row < $rows; $row++) {
102 for ($col = 0; $col < $cols; $col++) {
103 $cur = $row*$cols+$col;
104 if ($photo = @$list[$cur]) {
105 $selected = ($photo['file'] === $photo);
106 if (is_null($photo['thumb'])) {
107 $photo['thumb'] = $this->makeThumb($photo['file']);
111 'PHOTO' => $photo['file'],
112 'DESC' => $photo['desc'],
113 'THUMB' => $photo['thumb'],
119 $tpl->replaceBlockFile('ITEM',
120 'bife_album_emptyitem.html', true);
123 $tpl->touchBlock('ITEM');
129 $tpl->setVariable('DESC', $this->getDescription());
138 * @param string $photo Photo to show.
143 function photo($photo) {
144 list($path, $name, $ext) = File_Util::splitFilename($photo);
145 $tpl =& new HTML_Template_Sigma('.');
146 $tpl->loadTemplateFile('bife_album_photo.html');
157 * Makes a photo thumbnail.
159 * @param string $photo Source photo to create the thumbnail.
160 * @param int $size Maximum thumbnail size.
162 * @return bool True on success, false on failure.
167 function makeThumb($photo, $size = 100) {
168 $format = $this->thumbsformat;
169 $thumb = $this->getThumbName($photo);
170 list($path, $name, $ext) = File_Util::splitFilename($thumb);
172 $img =& Image_Transform::factory('GD');
175 // If image is larger than the maximum size, we resize it.
176 if ($img->img_x > $size or $img->img_y > $size ) {
177 if (!@is_dir($path) and !@mkdir($path)) {
180 if (PEAR::isError($img)) {
183 if (!$img->scale($size)) {
187 $img->save("$path/$name.$format", $format);
194 * Gets a list of photos with their descriptions and thumbnails.
196 * Returns an array of associative arrays with this keys:
198 * <li><b>file:</b> Photo filename.</li>
199 * <li><b>desc:</b> Photo Description.</li>
200 * <li><b>thumb:</b> Photo thumbnail filename.</li>
203 * @return array Array of associative arrays with file, desc and
211 $format = $this->thumbsformat;
215 while (($file = $d->read()) !== false) {
216 list($path, $name, $ext) = File_Util::splitFilename("$root/$file");
217 if (is_readable("$root/$file") and in_array($ext, $exts)) {
218 $thumb = $this->getThumbName("$root/$file");
220 'file' => "$root/$file",
222 'thumb' => is_readable($thumb) ? $thumb : null,
232 * Gets a photo thumbnail name.
234 * @param string $photo Source photo to create the thumbnail.
236 * @return string Thumbnail name.
241 function getThumbName($photo) {
243 $format = $this->thumbsformat;
244 $thumbsdir = $this->thumbsdir;
246 list($path, $name, $ext) = File_Util::splitFilename($photo);
248 return "$root/$thumbsdir/$name.$format";
252 * Creates album thumbnails.
257 function createThumbs() {
258 foreach ($this->getList() as $photo) {
259 if (is_null($photo['thumb'])) {
260 $photo['thumb'] = $this->makeThumb($photo['file']);
266 * Sets the album description.
268 * @param string $desc New album description.
270 * @returns bool True on success, false on failure.
275 function setDescription($desc) {
277 $out = fopen("$root/.album", 'w');
278 $ret = fputs($out, $desc);
280 return $ret ? true : false;
284 * Gets the album description.
286 * @returns string Album description.
291 function getDescription() {
293 return file_get_contents("$root/.album");