2 // vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
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]
42 class BIFE_Album_Thumbs extends BIFE_Widget {
49 * @param array $attrs Attributes.
54 function BIFE_Album_Thumbs($attrs) // ~X2C
56 $this->__construct($attrs);
64 * @param array $attrs Attributes.
69 function __construct($attrs) // ~X2C
71 // TODO - get defaults from an INI file.
75 'THUMBSFORMAT' => 'jpeg',
76 'THUMBSDIR' => '.thumbs',
77 'EXTENSIONS' => 'png,jpg,jpeg,PNG,JPG,JPEG',
80 'LINK-BIFE' => '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();
103 $rows = ceil($tot / $this->attrs['COLUMNS']);
104 for ($row = 0; $row < $rows; $row++) {
105 for ($col = 0; $col < $this->attrs['COLUMNS']; $col++) {
106 $cur = $row * $this->attrs['COLUMNS'] + $col;
107 if ($photo = @$list[$cur]) {
108 if (is_null($photo['THUMB'])) {
109 $photo['THUMB'] = $this->makeThumb($photo['FILE']);
111 $photo['URL'] = BIFE_Link::getURL(
113 'BIFE' => $this->attrs['LINK-BIFE'],
114 'URL' => $this->attrs['LINK-URL'],
115 'DATA-BIFE_ALBUM_FILE' => $photo['FILE'],
118 $cell = $template->parse('item', $photo);
120 $cell = $template->parse('empty');
122 $template->parseBuffered('cell', 'CONTENTS', $cell);
124 $template->parseBuffered('row', 'CONTENTS',
125 $template->popBuffer('cell'));
127 $out = $template->parse('body', array(
128 'DESC' => $this->getDescription(),
129 'CONTENTS' => $template->popBuffer('row')));
130 $template->popGroup();
137 * Gets a list of photos with their descriptions and thumbnails.
138 Returns an array of associative arrays with this keys:
140 <li><b>file:</b> Photo filename.</li>
141 <li><b>desc:</b> Photo Description.</li>
142 <li><b>thumb:</b> Photo thumbnail filename.</li>
148 function getList() // ~X2C
150 $root = $this->attrs['DIR'];
151 $exts = $this->attrs['EXTENSIONS'];
152 $format = $this->attrs['THUMBSFORMAT'];
156 while (($file = $d->read()) !== false) {
157 list($path, $name, $ext) = $this->splitFilename("$root/$file");
158 if (is_readable("$root/$file") and in_array($ext, $exts)) {
159 $thumb = $this->getThumbFilename("$root/$file");
161 'FILE' => "$root/$file",
163 'THUMB' => is_readable($thumb) ? $thumb : null,
175 * Creates an image thumbnail, returning his filename.
177 * @param string $filename Filename of the image to create the thumb.
178 * @param int $size Maximum thumbnail size.
183 function makeThumb($filename, $size = 100) // ~X2C
185 $format = $this->attrs['THUMBSFORMAT'];
186 $thumb = $this->getThumbFilename($filename);
187 list($path, $name, $ext) = $this->splitFilename($thumb);
188 $img =& Image_Transform::factory('GD');
189 $img->load($filename);
190 // If image is larger than the maximum size, we resize it.
191 if ($img->img_x > $size or $img->img_y > $size ) {
192 if (!@is_dir($path) and !@mkdir($path)) {
195 if (PEAR::isError($img)) {
198 if (!$img->scale($size)) {
202 $img->save("$path/$name.$format", $format);
211 * Returns the filename of an image thumb.
213 * @param string $filename Filename of the image to get the thumb name.
218 function getThumbFilename($filename) // ~X2C
220 $root = $this->attrs['DIR'];
221 $format = $this->attrs['THUMBSFORMAT'];
222 $thumbsdir = $this->attrs['THUMBSDIR'];
224 list($path, $name, $ext) = $this->splitFilename($filename);
226 return "$root/$thumbsdir/$name.$format";
230 // +X2C Operation 102
232 * Returns the description of the album.
237 function getDescription() // ~X2C
239 $root = $this->attrs['DIR'];
240 return @join('', file($file));
244 // +X2C Operation 100
246 * Splits a filename returning an array with the path, name and extension.
248 * @param string $filename Filename to split.
254 function splitFilename($filename) // ~X2C
256 $path = explode('/', $filename);
257 $file = array_pop($path);
259 if (strstr($file, '.')) {
260 preg_match('|([^/]+?)(\.([^\.]*))?$|', $file, $m);
261 $file = @$m[1] . ((@$m[2] == '.' ) ? '.' : '');
264 $dir = count($path) ? join('/', $path) : '';
265 return array($dir, $file, $ext);
269 } // -X2C Class :Thumbs