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 'PEAR.php';
32 require_once 'Util.php';
33 require_once 'Image/Transform.php';
34 require_once 'HTML/Template/Sigma.php';
37 * Generates a photo album.
39 * This library generates a photo album dinamically.
43 * @author Harpo Maxx <harpo@lugmen.org.ar>
44 * @author Groucho Marx <groucho@lugmen.org.ar>
45 * @author Leandro Lucarella <luca@lugmen.org.ar>
61 * @param string $root Root directory.
63 * @param bool $recursive True if subdirectories must be
65 * @param string $thumbsdir Directory where the thumbnails are (or
67 * @param array $exts Comma separated extensions of photo files.
71 function Hook_Album($root = '.', $recursive = true, $thumbsformat = 'jpeg', $thumbsdir = '.thumbs', $exts = 'png,jpg,jpeg,gif') {
72 $this->root = $root ? $root : '.';
73 $this->recursive = $recursive;
74 $this->thumbsdir = $thumbsdir;
75 $this->thumbsformat = $thumbsformat;
76 $this->exts = preg_split('/\s*,\s*/', $exts);
80 * Shows the photo album.
82 * Shows the photo album.
84 * @param string $photo Selected photo.
85 * @param int $max Maximum quantity of thumbnails to show. 0 for
87 * @param int $cols How many thumbnails are shown in the
90 * @todo Implement the 'max' feature!
91 * @todo Make the thumbs cache before showing the images so the
92 * browser can find the with having to refresh.
94 function album(&$tpl, $photo = '', $max = 0, $cols = 4) {
95 $tpl->loadTemplateFile('bife_album.html');
96 $tpl->addBlockFile('ITEMS', 'ITEM', 'bife_album_item.html');
99 $list = $this->getList();
101 $rows = ceil($tot / $cols);
103 for ($row = 0; $row < $rows; $row++) {
104 for ($col = 0; $col < $cols; $col++) {
105 $cur = $row*$cols+$col;
106 if ($photo = @$list[$cur]) {
107 $selected = ($photo['file'] === $photo);
108 if (is_null($photo['thumb'])) {
109 $photo['thumb'] = $this->makeThumb($photo['file']);
113 'PHOTO' => $photo['file'],
114 'DESC' => $photo['desc'],
115 'THUMB' => $photo['thumb'],
121 $tpl->replaceBlockFile('ITEM',
122 'bife_album_emptyitem.html', true);
125 $tpl->touchBlock('ITEM');
131 $tpl->setVariable('DESC', $this->getDescription());
140 * @param string $photo Photo to show.
145 function photo($photo) {
146 list($path, $name, $ext) = File_Util::splitFilename($photo);
147 $tpl =& new HTML_Template_Sigma('.');
148 $tpl->loadTemplateFile('photo.html');
159 * Makes a photo thumbnail.
161 * @param string $photo Source photo to create the thumbnail.
162 * @param int $size Maximum thumbnail size.
164 * @return bool True on success, false on failure.
169 function makeThumb($photo, $size = 100) {
170 $format = $this->thumbsformat;
171 $thumb = $this->getThumbName($photo);
172 list($path, $name, $ext) = File_Util::splitFilename($thumb);
174 $img =& Image_Transform::factory('GD');
177 // If image is larger than the maximum size, we resize it.
178 if ($img->img_x > $size or $img->img_y > $size ) {
179 if (!is_dir($path) and !mkdir($path)) {
182 if (PEAR::isError($img)) {
185 if (!$img->scale($size)) {
189 $img->save("$path/$name.$format", $format);
196 * Gets a list of photos with their descriptions and thumbnails.
198 * Returns an array of associative arrays with this keys:
200 * <li><b>file:</b> Photo filename.</li>
201 * <li><b>desc:</b> Photo Description.</li>
202 * <li><b>thumb:</b> Photo thumbnail filename.</li>
205 * @return array Array of associative arrays with file, desc and
213 $format = $this->thumbsformat;
217 while (($file = $d->read()) !== false) {
218 list($path, $name, $ext) = File_Util::splitFilename("$root/$file");
219 if (is_readable("$root/$file") and in_array($ext, $exts)) {
220 $thumb = $this->getThumbName("$root/$file");
222 'file' => "$root/$file",
224 'thumb' => is_readable($thumb) ? $thumb : null,
234 * Gets a photo thumbnail name.
236 * @param string $photo Source photo to create the thumbnail.
238 * @return string Thumbnail name.
243 function getThumbName($photo) {
245 $format = $this->thumbsformat;
246 $thumbsdir = $this->thumbsdir;
248 list($path, $name, $ext) = File_Util::splitFilename($photo);
250 return "$root/$thumbsdir/$name.$format";
254 * Creates album thumbnails.
259 function createThumbs() {
260 foreach ($this->getList() as $photo) {
261 if (is_null($photo['thumb'])) {
262 $photo['thumb'] = $this->makeThumb($photo['file']);
268 * Sets the album description.
270 * @param string $desc New album description.
272 * @returns bool True on success, false on failure.
277 function setDescription($desc) {
279 $out = fopen("$root/.album", 'w');
280 $ret = fputs($out, $desc);
282 return $ret ? true : false;
286 * Gets the album description.
288 * @returns string Album description.
293 function getDescription() {
295 return file_get_contents("$root/.album");