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->loadTemplateFile('bife_album.html');
102 $root = $this->attrs['DIR'];
103 $list = $this->getList();
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']);
113 // FIXME - Si no se pudo crear el thumb, devuelve null
114 // (ver si se agrega otro template para indicar error
116 $photo['URL'] = $this->attrs['LINK-URL'] . '?BIFE_ALBUM_FILE=' .
117 urlencode($photo['FILE']);
118 if ($this->attrs['LINK-BIFE']) {
119 $photo['URL'] .= '&BIFE=' . urlencode($this->attrs['LINK-BIFE']);
121 $template->setVariable($photo);
122 $template->parse('ITEM');
124 $template->touchBlock('EMPTY');
125 $template->parse('EMPTY');
128 $template->parse('ROW');
130 $template->setVariable('DESC', $this->getDescription());
131 return $template->get();
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/$name",
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 :Album