2 // vim: set expandtab tabstop=4 shiftwidth=4 binary:
3 // +--------------------------------------------------------------------+
4 // | BIFE - Buil It FastEr |
5 // +--------------------------------------------------------------------+
6 // | This file is part of BIFE. |
8 // | BIFE 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 // | BIFE 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 // +--------------------------------------------------------------------+
22 // | Created: Wed May 17 18:16:54 ART 2003 |
23 // | Authors: Leandro Lucarella <luca@lugmen.org.ar> |
24 // +--------------------------------------------------------------------+
30 require_once 'BIFE/Widget.php';
33 require_once 'BIFE/Link.php';
34 require_once 'Image/Transform.php';
36 // +X2C Class 20 :Thumbs
38 * Photo album widget. [TODO: Make a better explanation]
43 class BIFE_Album_Thumbs extends BIFE_Widget {
50 * @param array $attrs Attributes.
55 function BIFE_Album_Thumbs($attrs) // ~X2C
57 $this->__construct($attrs);
65 * @param array $attrs Attributes.
70 function __construct($attrs) // ~X2C
72 // TODO - get defaults from an INI file.
76 'THUMBSFORMAT' => 'jpeg',
77 'THUMBSDIR' => '.thumbs',
78 'EXTENSIONS' => 'png,jpg,jpeg,PNG,JPG,JPEG',
81 'LINK' => 'photo.xbf',
83 $attrs = array_merge($defaults, $attrs);
84 $attrs['EXTENSIONS'] = explode(',', $attrs['EXTENSIONS']);
85 parent::__construct($attrs);
93 * @param HTML_Template_HIT &$template Template to use to render the widget.
98 function render(&$template) // ~X2C
100 $template->pushGroup('album');
101 $list = $this->getList(BIFE_Link::getFsPath() . $this->attrs['DIR'],
102 $this->attrs['EXTENSIONS'], $this->attrs['THUMBSFORMAT'],
103 $this->attrs['THUMBSDIR']);
105 $rows = ceil($tot / $this->attrs['COLUMNS']);
106 for ($row = 0; $row < $rows; $row++) {
107 for ($col = 0; $col < $this->attrs['COLUMNS']; $col++) {
108 $cur = $row * $this->attrs['COLUMNS'] + $col;
109 if ($photo = @$list[$cur]) {
110 if (is_null($photo['THUMB'])) {
111 $photo['THUMB'] = $this->makeThumb($photo['FILE'],
112 $this->attrs['THUMBSFORMAT'],
113 $this->attrs['THUMBSDIR'],
114 BIFE_Link::getFsPath() . $this->attrs['DIR']);
116 $photo['THUMB'] = BIFE_Link::getWebPath()
117 . $this->attrs['DIR'] . '/' . $photo['THUMB'];
118 $photo['URL'] = BIFE_Link::getURL(
120 'URL' => $this->attrs['LINK'],
121 'DATA-BIFE_ALBUM_FILE' => BIFE_Link::getWebPath()
122 . $this->attrs['DIR'] . '/' . $photo['FILE'],
125 $cell = $template->parse('item', $photo);
127 $cell = $template->parse('empty');
129 $template->parseBuffered('cell', 'CONTENTS', $cell);
131 $template->parseBuffered('row', 'CONTENTS',
132 $template->popBuffer('cell'));
134 $out = $template->parse('body', array(
135 'DESC' => $this->getDescription(),
136 'CONTENTS' => $template->popBuffer('row')));
137 $template->popGroup();
144 * Gets a list of photos with their descriptions and thumbnails.
145 Returns an array of associative arrays with this keys:
147 <li><b>file:</b> Photo filename.</li>
148 <li><b>desc:</b> Photo Description.</li>
149 <li><b>thumb:</b> Photo thumbnail filename.</li>
152 * @param string $root Directory where images are.
153 * @param array $exts Images extensions to accept.
154 * @param string $format Format to use to save the thumbs.
155 * @param string $thumbsdir Directory where the thumbs are.
160 function getList($root, $exts, $format, $thumbsdir) // ~X2C
165 while (($file = $d->read()) !== false) {
166 list($path, $name, $ext) = $this->splitFilename("$root/$file");
167 if (is_readable("$root/$file") and in_array($ext, $exts)) {
168 $thumb = $this->getThumbFilename($file, $format,
185 * Creates an image thumbnail, returning his filename.
187 * @param string $filename Filename of the image to create the thumb.
188 * @param string $format Thumbs format.
189 * @param string $thumbsdir Directory where to put the thumbs.
190 * @param string $root Root directory.
191 * @param int $size Maximum thumbnail size.
197 function makeThumb($filename, $format, $thumbsdir, $root, $size = 100) // ~X2C
199 list($path, $name, $ext) = $this->splitFilename($filename);
200 $img =& Image_Transform::factory('GD');
201 $img->load("$root/$filename");
202 // If image is larger than the maximum size, we resize it.
203 if ($img->img_x > $size or $img->img_y > $size ) {
204 if (!@is_dir("$root/$thumbsdir") and !@mkdir("$root/$thumbsdir")) {
207 if (PEAR::isError($img)) {
210 if (!$img->scale($size)) {
214 $img->save("$root/$thumbsdir/$name.$format", $format);
216 return "$thumbsdir/$name.$format";
222 * Returns the filename of an image thumb.
224 * @param string $filename Filename of the image to get the thumb name.
225 * @param string $format Thumbs format.
226 * @param string $thumbsdir Directory where the thumbs are.
227 * @param string $root Root directory.
232 function getThumbFilename($filename, $format, $thumbsdir, $root) // ~X2C
234 list($path, $name, $ext) = $this->splitFilename($filename);
235 if (is_readable("$root/$thumbsdir/$name.$format")) {
236 return "$thumbsdir/$name.$format";
243 // +X2C Operation 102
245 * Returns the description of the album.
250 function getDescription() // ~X2C
252 $root = $this->attrs['DIR'];
253 return @join('', file($file));
257 // +X2C Operation 100
259 * Splits a filename returning an array with the path, name and extension.
261 * @param string $filename Filename to split.
267 function splitFilename($filename) // ~X2C
269 $path = explode('/', $filename);
270 $file = array_pop($path);
272 if (strstr($file, '.')) {
273 preg_match('|([^/]+?)(\.([^\.]*))?$|', $file, $m);
274 $file = @$m[1] . ((@$m[2] == '.' ) ? '.' : '');
277 $dir = count($path) ? join('/', $path) : '';
278 return array($dir, $file, $ext);
282 } // -X2C Class :Thumbs