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 'Image/Transform.php';
35 // +X2C Class 20 :Thumbs
37 * Photo album widget. [TODO: Make a better explanation]
41 class BIFE_Album_Thumbs extends BIFE_Widget {
48 * @param array $attrs Attributes.
53 function BIFE_Album_Thumbs($attrs) // ~X2C
55 $this->__construct($attrs);
63 * @param array $attrs Attributes.
68 function __construct($attrs) // ~X2C
70 // TODO - get defaults from an INI file.
74 'THUMBSFORMAT' => 'jpeg',
75 'THUMBSDIR' => '.thumbs',
76 'EXTENSIONS' => 'png,jpg,jpeg,PNG,JPG,JPEG',
79 'LINK-BIFE' => 'photo.xbf',
82 $attrs = array_merge($defaults, $attrs);
83 $attrs['EXTENSIONS'] = explode(',', $attrs['EXTENSIONS']);
84 parent::__construct($attrs);
92 * @param HTML_Template_HIT &$template Template to use to render the widget.
97 function render(&$template) // ~X2C
99 $template->pushGroup('album');
100 $list = $this->getList();
102 $rows = ceil($tot / $this->attrs['COLUMNS']);
103 for ($row = 0; $row < $rows; $row++) {
104 for ($col = 0; $col < $this->attrs['COLUMNS']; $col++) {
105 $cur = $row * $this->attrs['COLUMNS'] + $col;
106 if ($photo = @$list[$cur]) {
107 if (is_null($photo['THUMB'])) {
108 $photo['THUMB'] = $this->makeThumb($photo['FILE']);
110 $photo['URL'] = BIFE_Link::getURL(
112 'BIFE' => $this->attrs['LINK-BIFE'],
113 'URL' => $this->attrs['LINK-URL'],
114 'DATA-BIFE_ALBUM_FILE' => $photo['FILE'],
117 $cell = $template->parse('item', $photo);
119 $cell = $template->parse('empty');
121 $template->parseBuffered('cell', 'CONTENTS', $cell);
123 $template->parseBuffered('row', 'CONTENTS',
124 $template->popBuffer('cell'));
126 $out = $template->parse('body', array(
127 'DESC' => $this->getDescription(),
128 'CONTENTS' => $template->popBuffer('row')));
129 $template->popGroup();
136 * Gets a list of photos with their descriptions and thumbnails.
137 Returns an array of associative arrays with this keys:
139 <li><b>file:</b> Photo filename.</li>
140 <li><b>desc:</b> Photo Description.</li>
141 <li><b>thumb:</b> Photo thumbnail filename.</li>
147 function getList() // ~X2C
149 $root = $this->attrs['DIR'];
150 $exts = $this->attrs['EXTENSIONS'];
151 $format = $this->attrs['THUMBSFORMAT'];
155 while (($file = $d->read()) !== false) {
156 list($path, $name, $ext) = $this->splitFilename("$root/$file");
157 if (is_readable("$root/$file") and in_array($ext, $exts)) {
158 $thumb = $this->getThumbFilename("$root/$file");
160 'FILE' => "$root/$file",
162 'THUMB' => is_readable($thumb) ? $thumb : null,
174 * Creates an image thumbnail, returning his filename.
176 * @param string $filename Filename of the image to create the thumb.
177 * @param int $size Maximum thumbnail size.
182 function makeThumb($filename, $size = 100) // ~X2C
184 $format = $this->attrs['THUMBSFORMAT'];
185 $thumb = $this->getThumbFilename($filename);
186 list($path, $name, $ext) = $this->splitFilename($thumb);
187 $img =& Image_Transform::factory('GD');
188 $img->load($filename);
189 // If image is larger than the maximum size, we resize it.
190 if ($img->img_x > $size or $img->img_y > $size ) {
191 if (!@is_dir($path) and !@mkdir($path)) {
194 if (PEAR::isError($img)) {
197 if (!$img->scale($size)) {
201 $img->save("$path/$name.$format", $format);
210 * Returns the filename of an image thumb.
212 * @param string $filename Filename of the image to get the thumb name.
217 function getThumbFilename($filename) // ~X2C
219 $root = $this->attrs['DIR'];
220 $format = $this->attrs['THUMBSFORMAT'];
221 $thumbsdir = $this->attrs['THUMBSDIR'];
223 list($path, $name, $ext) = $this->splitFilename($filename);
225 return "$root/$thumbsdir/$name.$format";
229 // +X2C Operation 102
231 * Returns the description of the album.
236 function getDescription() // ~X2C
238 $root = $this->attrs['DIR'];
239 return @join('', file($file));
243 // +X2C Operation 100
245 * Splits a filename returning an array with the path, name and extension.
247 * @param string $filename Filename to split.
253 function splitFilename($filename) // ~X2C
255 $path = explode('/', $filename);
256 $file = array_pop($path);
258 if (strstr($file, '.')) {
259 preg_match('|([^/]+?)(\.([^\.]*))?$|', $file, $m);
260 $file = @$m[1] . ((@$m[2] == '.' ) ? '.' : '');
263 $dir = count($path) ? join('/', $path) : '';
264 return array($dir, $file, $ext);
268 } // -X2C Class :Thumbs