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',
81 // TODO - agregar atributo para el LINK, ver de hacer el
84 $attrs = array_merge($defaults, $attrs);
85 $attrs['EXTENSIONS'] = explode(',', $attrs['EXTENSIONS']);
86 parent::__construct($attrs);
94 * @param HTML_Template_HIT &$template Template to use to render the widget.
99 function render(&$template) // ~X2C
101 $template->pushGroup('album');
102 $list = $this->getList();
104 $rows = ceil($tot / $this->attrs['COLUMNS']);
105 for ($row = 0; $row < $rows; $row++) {
106 for ($col = 0; $col < $this->attrs['COLUMNS']; $col++) {
107 $cur = $row * $this->attrs['COLUMNS'] + $col;
108 if ($photo = @$list[$cur]) {
109 if (is_null($photo['THUMB'])) {
110 $photo['THUMB'] = $this->makeThumb($photo['FILE']);
112 $photo['URL'] = BIFE_Link::getURL(
114 'BIFE' => $this->attrs['LINK-BIFE'],
115 'URL' => $this->attrs['LINK-URL'],
116 'DATA-BIFE_ALBUM_FILE' => $photo['FILE'],
119 $cell = $template->parse('item', $photo);
121 $cell = $template->parse('empty');
123 $template->parseBuffered('cell', 'CONTENTS', $cell);
125 $template->parseBuffered('row', 'CONTENTS',
126 $template->popBuffer('cell'));
128 $out = $template->parse('body', array(
129 'DESC' => $this->getDescription(),
130 'CONTENTS' => $template->popBuffer('row')));
131 $template->popGroup();
138 * Gets a list of photos with their descriptions and thumbnails.
139 Returns an array of associative arrays with this keys:
141 <li><b>file:</b> Photo filename.</li>
142 <li><b>desc:</b> Photo Description.</li>
143 <li><b>thumb:</b> Photo thumbnail filename.</li>
149 function getList() // ~X2C
151 $root = $this->attrs['DIR'];
152 $exts = $this->attrs['EXTENSIONS'];
153 $format = $this->attrs['THUMBSFORMAT'];
157 while (($file = $d->read()) !== false) {
158 list($path, $name, $ext) = $this->splitFilename("$root/$file");
159 if (is_readable("$root/$file") and in_array($ext, $exts)) {
160 $thumb = $this->getThumbFilename("$root/$file");
162 'FILE' => "$root/$file",
164 'THUMB' => is_readable($thumb) ? $thumb : null,
176 * Creates an image thumbnail, returning his filename.
178 * @param string $filename Filename of the image to create the thumb.
179 * @param int $size Maximum thumbnail size.
184 function makeThumb($filename, $size = 100) // ~X2C
186 $format = $this->attrs['THUMBSFORMAT'];
187 $thumb = $this->getThumbFilename($filename);
188 list($path, $name, $ext) = $this->splitFilename($thumb);
189 $img =& Image_Transform::factory('GD');
190 $img->load($filename);
191 // If image is larger than the maximum size, we resize it.
192 if ($img->img_x > $size or $img->img_y > $size ) {
193 if (!@is_dir($path) and !@mkdir($path)) {
196 if (PEAR::isError($img)) {
199 if (!$img->scale($size)) {
203 $img->save("$path/$name.$format", $format);
212 * Returns the filename of an image thumb.
214 * @param string $filename Filename of the image to get the thumb name.
219 function getThumbFilename($filename) // ~X2C
221 $root = $this->attrs['DIR'];
222 $format = $this->attrs['THUMBSFORMAT'];
223 $thumbsdir = $this->attrs['THUMBSDIR'];
225 list($path, $name, $ext) = $this->splitFilename($filename);
227 return "$root/$thumbsdir/$name.$format";
231 // +X2C Operation 102
233 * Returns the description of the album.
238 function getDescription() // ~X2C
240 $root = $this->attrs['DIR'];
241 return @join('', file($file));
245 // +X2C Operation 100
247 * Splits a filename returning an array with the path, name and extension.
249 * @param string $filename Filename to split.
255 function splitFilename($filename) // ~X2C
257 $path = explode('/', $filename);
258 $file = array_pop($path);
260 if (strstr($file, '.')) {
261 preg_match('|([^/]+?)(\.([^\.]*))?$|', $file, $m);
262 $file = @$m[1] . ((@$m[2] == '.' ) ? '.' : '');
265 $dir = count($path) ? join('/', $path) : '';
266 return array($dir, $file, $ext);
270 } // -X2C Class :Thumbs