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]
43 class BIFE_Album_Thumbs extends BIFE_Widget {
50 * @param array $attrs Attributes.
55 function BIFE_Album_Thumbs($attrs) // ~X2C
57 $this->__construct($attrs);
65 * @param array $attrs Attributes.
70 function __construct($attrs) // ~X2C
72 // TODO - get defaults from an INI file.
76 'THUMBSFORMAT' => 'jpeg',
77 'THUMBSDIR' => '.thumbs',
78 'EXTENSIONS' => 'png,jpg,jpeg,PNG,JPG,JPEG',
81 'LINK' => '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(BIFE_Link::getFsPath() . $this->attrs['DIR'],
102 $this->attrs['EXTENSIONS'], $this->attrs['THUMBSFORMAT'],
103 $this->attrs['THUMBSDIR']);
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(
112 BIFE_Link::getFsPath() . $photo['FILE'],
113 $this->attrs['THUMBSFORMAT'],
114 $this->attrs['THUMBSDIR']);
116 $photo['THUMB'] = BIFE_Link::getWebPath()
117 . $this->attrs['DIR'] . '/' . $photo['THUMB'];
118 $photo['URL'] = BIFE_Link::getURL(
120 'URL' => $this->attrs['LINK'],
121 'DATA-BIFE_ALBUM_FILE' => BIFE_Link::getWebPath()
122 . $this->attrs['DIR'] . '/' . $photo['FILE'],
125 $cell = $template->parse('item', $photo);
127 $cell = $template->parse('empty');
129 $template->parseBuffered('cell', 'CONTENTS', $cell);
131 $template->parseBuffered('row', 'CONTENTS',
132 $template->popBuffer('cell'));
134 $out = $template->parse('body', array(
135 'DESC' => $this->getDescription(),
136 'CONTENTS' => $template->popBuffer('row')));
137 $template->popGroup();
144 * Gets a list of photos with their descriptions and thumbnails.
145 Returns an array of associative arrays with this keys:
147 <li><b>file:</b> Photo filename.</li>
148 <li><b>desc:</b> Photo Description.</li>
149 <li><b>thumb:</b> Photo thumbnail filename.</li>
155 function getList() // ~X2C
160 while (($file = $d->read()) !== false) {
161 list($path, $name, $ext) = $this->splitFilename("$root/$file");
162 if (is_readable("$root/$file") and in_array($ext, $exts)) {
163 $thumb = $this->getThumbFilename($file, $format,
180 * Creates an image thumbnail, returning his filename.
182 * @param string $filename Filename of the image to create the thumb.
183 * @param int $size Maximum thumbnail size.
188 function makeThumb($filename, $size = 100) // ~X2C
190 $thumb = $this->getThumbFilename($filename, $format, $thumbsdir);
191 list($path, $name, $ext) = $this->splitFilename($thumb);
192 $img =& Image_Transform::factory('GD');
193 $img->load($filename);
194 // If image is larger than the maximum size, we resize it.
195 if ($img->img_x > $size or $img->img_y > $size ) {
196 if (!@is_dir($path) and !@mkdir($path)) {
199 if (PEAR::isError($img)) {
202 if (!$img->scale($size)) {
206 $img->save("$path/$name.$format", $format);
215 * Returns the filename of an image thumb.
217 * @param string $filename Filename of the image to get the thumb name.
222 function getThumbFilename($filename) // ~X2C
224 list($path, $name, $ext) = $this->splitFilename($filename);
225 return "$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