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 :Album
37 * Photo album widget. [TODO: Make a better explanation]
41 class BIFE_Album extends BIFE_Widget {
48 * @param array $attrs Attributes.
53 function BIFE_Album($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_Sigma &$template Template to use to render the widget.
99 function render(&$template) // ~X2C
101 $template->group = 'album';
102 $root = $this->attrs['DIR'];
103 $list = $this->getList();
105 $rows = ceil($tot / $this->attrs['COLUMNS']);
107 for ($row = 0; $row < $rows; $row++) {
109 for ($col = 0; $col < $this->attrs['COLUMNS']; $col++) {
110 $cur = $row * $this->attrs['COLUMNS'] + $col;
111 if ($photo = @$list[$cur]) {
112 if (is_null($photo['THUMB'])) {
113 $photo['THUMB'] = $this->makeThumb($photo['FILE']);
115 // FIXME - Si no se pudo crear el thumb, devuelve null
116 // (ver si se agrega otro template para indicar error
118 $photo['URL'] = $this->attrs['LINK-URL'] .
119 '?BIFE_ALBUM_FILE=' . urlencode($photo['FILE']);
120 if ($this->attrs['LINK-BIFE']) {
121 $photo['URL'] .= '&BIFE=' .
122 urlencode($this->attrs['LINK-BIFE']);
124 $html_cell = $template->parse('item', $photo);
126 $html_cell = $template->parse('empty');
128 $html_cells .= $template->parse('cell', 'CONTENTS', $html_cell);
130 $html_rows .= $template->parse('row', 'CONTENTS', $html_cells);
132 return $template->parse(
134 array('DESC' => $this->getDescription(), 'CONTENTS' => $html_rows));
140 * Gets a list of photos with their descriptions and thumbnails.
141 Returns an array of associative arrays with this keys:
143 <li><b>file:</b> Photo filename.</li>
144 <li><b>desc:</b> Photo Description.</li>
145 <li><b>thumb:</b> Photo thumbnail filename.</li>
151 function getList() // ~X2C
153 $root = $this->attrs['DIR'];
154 $exts = $this->attrs['EXTENSIONS'];
155 $format = $this->attrs['THUMBSFORMAT'];
159 while (($file = $d->read()) !== false) {
160 list($path, $name, $ext) = $this->splitFilename("$root/$file");
161 if (is_readable("$root/$file") and in_array($ext, $exts)) {
162 $thumb = $this->getThumbFilename("$root/$file");
164 'FILE' => "$root/$file",
166 'THUMB' => is_readable($thumb) ? $thumb : null,
178 * Creates an image thumbnail, returning his filename.
180 * @param string $filename Filename of the image to create the thumb.
181 * @param int $size Maximum thumbnail size.
186 function makeThumb($filename, $size = 100) // ~X2C
188 $format = $this->attrs['THUMBSFORMAT'];
189 $thumb = $this->getThumbFilename($filename);
190 list($path, $name, $ext) = $this->splitFilename($thumb);
191 $img =& Image_Transform::factory('GD');
192 $img->load($filename);
193 // If image is larger than the maximum size, we resize it.
194 if ($img->img_x > $size or $img->img_y > $size ) {
195 if (!@is_dir($path) and !@mkdir($path)) {
198 if (PEAR::isError($img)) {
201 if (!$img->scale($size)) {
205 $img->save("$path/$name.$format", $format);
214 * Returns the filename of an image thumb.
216 * @param string $filename Filename of the image to get the thumb name.
221 function getThumbFilename($filename) // ~X2C
223 $root = $this->attrs['DIR'];
224 $format = $this->attrs['THUMBSFORMAT'];
225 $thumbsdir = $this->attrs['THUMBSDIR'];
227 list($path, $name, $ext) = $this->splitFilename($filename);
229 return "$root/$thumbsdir/$name.$format";
233 // +X2C Operation 102
235 * Returns the description of the album.
240 function getDescription() // ~X2C
242 $root = $this->attrs['DIR'];
243 return @join('', file($file));
247 // +X2C Operation 100
249 * Splits a filename returning an array with the path, name and extension.
251 * @param string $filename Filename to split.
257 function splitFilename($filename) // ~X2C
259 $path = explode('/', $filename);
260 $file = array_pop($path);
262 if (strstr($file, '.')) {
263 preg_match('|([^/]+?)(\.([^\.]*))?$|', $file, $m);
264 $file = @$m[1] . ((@$m[2] == '.' ) ? '.' : '');
267 $dir = count($path) ? join('/', $path) : '';
268 return array($dir, $file, $ext);
272 } // -X2C Class :Album