2 rcs_id('$Id: PhotoAlbum.php,v 1.12 2005-09-20 19:34:51 rurban Exp $');
4 Copyright 2003, 2004, 2005 $ThePhpWikiProgrammingTeam
6 This file is part of PhpWiki.
8 PhpWiki is free software; you can redistribute it and/or modify
9 it 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 PhpWiki is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Display an album of a set of photos with optional descriptions.
26 * @author: Ted Vinke <teddy@jouwfeestje.com>
27 * Reini Urban (local fs)
28 * Thomas Harding (slides mode, real thumbnails)
32 * src="http://server/textfile" or localfile or localdir
33 * mode=[normal|thumbs|tiles|list|slide]
38 * align=[center|left|right]
41 * "src": textfile of images or directory of images or a single image (local or remote)
42 * Local or remote e.g. http://myserver/images/MyPhotos.txt or http://myserver/images/
43 * Possible content of a valid textfile:
44 * photo-01.jpg; Me and my girlfriend
46 * christmas.gif; Merry Christmas!
48 * Inside textfile, filenames and optional descriptions are seperated by
49 * semi-colon on each line. Listed files must be in same directory as textfile
50 * itself, so don't use relative paths inside textfile.
52 * "url": defines the the webpath to the srcdir directory (formerly called weblocation)
57 * - specify picture(s) as parameter(s)
58 * - limit amount of pictures on one page
59 * - use PHP to really resize or greyscale images (only where GD library supports it)
60 * (quite done for resize with "ImageTile.php")
63 * - reading height and width from images with spaces in their names fails.
65 * Fixed album location idea by Philip J. Hollenback. Thanks!
68 class ImageTile extends HtmlElement
70 function image_tile (/*...*/) {
71 $el = new HTML ('img');
72 $tag = func_get_args();
73 $params = "<img src='../ImageTile.php?url=". $tag[0]['src'];
74 if (!@empty($tag[0]['width']))
75 $params .= "&width=" . $tag[0]['width'];
76 if (!@empty($tag[0]['height']))
77 $params .= "&height=" . $tag[0]['height'];
78 if (!@empty($tag[0]['width']))
79 $params .= "' width='" . $tag[0]['width'];
80 if (!@empty($tag[0]['height']))
81 $params .= "' height='" . $tag[0]['height'];
83 $params .= "' alt='" . $tag[0]['alt'] . "' />";
84 return $el->raw ($params);
88 class WikiPlugin_PhotoAlbum
92 return _("PhotoAlbum");
95 function getDescription () {
96 return _("Displays a set of photos listed in a text file with optional descriptions");
99 function getVersion() {
100 return preg_replace("/[Revision: $]/", '',
101 "\$Revision: 1.12 $");
104 // Avoid nameclash, so it's disabled. We allow any url.
105 // define('allow_album_location', true);
106 // define('album_location', 'http://kw.jouwfeestje.com/foto/redactie');
107 // define('album_default_extension', '.jpg');
108 // define('desc_separator', ';');
110 function getDefaultArguments() {
111 return array('src' => '', // textfile of image list, or local dir.
112 'url' => '', // if src=localfs, url prefix (webroot for the links)
113 'mode' => 'normal', // normal|thumbs|tiles|list
114 // "normal" - Normal table which shows photos full-size
115 // "thumbs" - WinXP thumbnail style
116 // "tiles" - WinXP tiles style
117 // "list" - WinXP list style
118 // "slide" - slideshow mode, needs javascript on client
119 'numcols' => 3, // photos per row, columns
120 'showdesc' => 'both', // none|name|desc|both
121 // "none" - No descriptions next to photos
122 // "name" - Only filename shown
123 // "desc" - Only description (from textfile) shown
124 // "both" - If no description found, then filename will be used
125 'link' => true, // show link to original sized photo
126 // If true, each image will be hyperlinked to a page where the single
127 // photo will be shown full-size. Only works when mode != 'normal'
128 'attrib' => '', // 'sort, nowrap, alt'
129 // attrib arg allows multiple attributes: attrib=sort,nowrap,alt
130 // 'sort' sorts alphabetically, 'nowrap' for cells, 'alt' to use
131 // descs instead of filenames in image ALT-tags
132 'bgcolor' => '#eae8e8', // cell bgcolor (lightgrey)
133 'hlcolor' => '#c0c0ff', // highlight color (lightblue)
134 'align' => 'center', // alignment of table
135 'height' => 'auto', // image height (auto|75|100%)
136 'width' => 'auto', // image width (auto|75|100%)
137 // Size of shown photos. Either absolute value (e.g. "50") or
138 // HTML style percentage (e.g. "75%") or "auto" for no special
140 'cellwidth'=> 'image', // cell (auto|equal|image|75|100%)
141 // Width of cells in table. Either absolute value in pixels, HTML
142 // style percentage, "auto" (no special action), "equal" (where
143 // all columns are equally sized) or "image" (take height and
144 // width of the photo in that cell).
145 'tablewidth'=> false, // table (75|100%)
146 'p' => false, // "displaythissinglephoto.jpg"
147 'h' => false, // "highlightcolorofthisphoto.jpg"
148 'duration' => 6 // in slide mode, in seconds
151 // descriptions (instead of filenames) for image alt-tags
153 function run($dbi, $argstr, &$request, $basepage) {
155 extract($this->getArgs($argstr, $request));
157 $attributes = $attrib ? explode(",", $attrib) : array();
161 // check all parameters
162 // what type do we have?
165 $src = $request->getArg('pagename');
166 $error = $this->fromLocation($src, $photos);
168 $error = $this->fromFile($src, $photos, $url);
171 return $this->error($error);
174 if ($numcols < 1) $numcols = 1;
175 if ($align != 'left' && $align != 'center' && $align != 'right') {
178 if (count($photos) == 0) return;
180 if (in_array("sort", $attributes))
187 // set some fixed properties for each $mode
188 if ($mode == 'thumbs' || $mode == 'tiles') {
189 $attributes = array_merge($attributes, "alt");
190 $attributes = array_merge($attributes, "nowrap");
191 $cellwidth = 'auto'; // else cell won't nowrap
193 } elseif ($mode == 'list') {
197 } elseif ($mode == 'slide' ) {
200 $numcols = count($photos);
202 while (list($key, $value) = each($photos)) {
203 list($x,$y,$s,$t) = @getimagesize($value['src']);
204 if ($height != 'auto') $y = $this->newSize($y, $height);
205 if ($width != 'auto') $y = round($y * $this->newSize($x, $width) / $x);
206 if ($x > $cell_width) $cell_width = $x;
207 if ($y > $tableheight) $tableheight = $y;
211 unset ($x,$y,$s,$t,$key,$value,$keep);
215 $duration = 1000 * $duration;
216 if ($mode == 'slide')
217 $row->pushContent(JavaScript("
219 function display_slides() {
221 cell0 = document.getElementsByName('wikislide' + j);
222 cell = document.getElementsByName('wikislide' + i);
223 if (cell0.item(0) != null)
224 cell0.item(0).style.display='none';
225 if (cell.item(0) != null)
226 cell.item(0).style.display='block';
228 if (cell.item(0) == null) i = 0;
229 setTimeout('display_slides()',$duration);
231 display_slides();"));
233 while (list($key, $value) = each($photos)) {
234 if ($p && basename($value["name"]) != "$p") {
237 if ($h && basename($value["name"]) == "$h") {
238 $color = $hlcolor ? $hlcolor : $bgcolor;
242 // $params will be used for each <img > tag
243 $params = array('src' => $value["name"],
244 'src_tile' => $value["name_tile"],
246 'alt' => ($value["desc"] != "" and in_array("alt", $attributes))
248 : basename($value["name"]));
249 if (!@empty($value['location']))
250 $params = array_merge($params, array("location" => $value['location']));
257 $value["desc"] = basename($value["name"]);
262 if (!$value["desc"]) $value["desc"] = basename($value["name"]);
266 // FIXME: get getimagesize to work with names with spaces in it.
267 // convert $value["name"] from webpath to local path
268 $size = @getimagesize($value["name"]); // try " " => "\\ "
269 if (!$size and !empty($value["src"])) {
270 $size = getimagesize($value["src"]);
272 trigger_error("Unable to getimagesize(".$value["name"].")",
277 $newwidth = $this->newSize($size[0], $width);
278 if (($mode == 'thumbs' || $mode == 'tiles' || $mode == 'list')) {
279 if (!empty($size[0]))
280 $newheight = round (50 * $size[1] / $size[0]);
281 else $newheight = '';
282 if ($height == 'auto') $height=150;
285 $newheight = $this->newSize($size[1], $height);
287 if ($width != 'auto' && $newwidth > 0) {
288 $params = array_merge($params, array("width" => $newwidth));
290 if ($height != 'auto' && $newheight > 0) {
291 $params = array_merge($params, array("height" => $newheight));
295 $cell = array('align' => "center",
297 'bgcolor' => "$color");
298 if ($cellwidth != 'auto') {
299 if ($cellwidth == 'equal') {
300 $newcellwidth = round(100/$numcols)."%";
301 } else if ($cellwidth == 'image') {
302 $newcellwidth = $newwidth;
304 $newcellwidth = $cellwidth;
306 $cell = array_merge($cell, array("width" => $newcellwidth));
308 if (in_array("nowrap", $attributes)) {
309 $cell = array_merge($cell, array("nowrap" => "nowrap"));
311 //create url to display single larger version of image on page
312 $url = WikiURL($request->getPage(),
313 array("p" => basename($value["name"])));
315 $b_url = WikiURL($request->getPage(),
316 array("h" => basename($value["name"])))
318 . basename($value["name"]);
320 ? HTML::a(array("href" => "$url"), basename($value["desc"]))
321 : basename($value["name"]);
323 if ($mode == 'normal' || $mode == 'slide') {
324 if(!@empty($params['location'])) $params['src'] = $params['location'];
325 unset ($params['location'],$params['src_tile']);
326 $url_image = $link ? HTML::a(array("href" => "$url"), HTML::img($params)) : HTML::img($params);
329 if (!@empty ($params['src_tile']))
330 $params['src'] = $params['src_tile'] ;
331 unset ($params['location'],$params['src_tile']);
332 $url_image = $link ? HTML::a(array("href" => "$url"),
333 ImageTile::image_tile($params)) : HTML::img($params);
338 if(!@empty($params['location'])) $params['src'] = $params['location'];
339 unset ($params['location'],$params['src_tile']);
340 $url_image = $link ? HTML::a(array("href" => "$b_url"), HTML::img($params)) : HTML::img($params);
342 $url_text = HTML::a(array("name" => basename($value["name"])),
344 // here we use different modes
345 if ($mode == 'tiles') {
348 HTML::table(array("cellpadding" => 1, "border" => 0),
350 HTML::td(array("valign" => "top", "rowspan" => 2),
352 HTML::td(array("valign" => "top", "nowrap" => 0),
353 HTML::span(array('class'=>'boldsmall'),
356 HTML::span(array('class'=>'gensmall'),
362 } elseif ($mode == 'list') {
363 $desc = ($showdesc != 'none') ? $value["desc"] : '';
365 HTML::td(array("valign" => "top",
367 "bgcolor" => $color),
368 HTML::span(array('class'=>'boldsmall'),($url_text))));
370 HTML::td(array("valign" => "top",
372 "bgcolor" => $color),
373 HTML::span(array('class'=>'gensmall'),
381 HTML::td(array("valign" => "top",
383 "bgcolor" => $color),
384 HTML::span(array('class'=>'gensmall'),$desc)));
386 } elseif ($mode == 'thumbs') {
387 $desc = ($showdesc != 'none') ?
388 HTML::p(HTML::a(array("href" => "$url"),
393 // FIXME: no HtmlElement for fontsizes?
394 // rurban: use ->setAttr("style","font-size:small;")
395 // but better use a css class
396 HTML::span(array('class'=>'gensmall'),$desc)
398 } elseif ($mode == 'normal') {
399 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
403 // FIXME: no HtmlElement for fontsizes?
404 HTML::span(array('class'=>'gensmall'),$desc)
406 } elseif ($mode == 'slide') {
407 if ($newwidth == 'auto' || !$newwidth)
408 $newwidth = $this->newSize($size[0],$width);
409 if ($newwidth == 'auto' || !$newwidth)
410 $newwidth = $size[0];
411 if ($newheight != 'auto') $newwidth = round($size[0] * $newheight / $size[1]);
412 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
414 $cell=array('style' => 'display: block; '
415 . 'position: absolute; '
417 . 'margin-left: -'.round($newwidth / 2).'px;'
418 . 'text-align: center; '
419 . 'vertical-align: top',
420 'name' => "wikislide".$count);
422 $cell=array('style' => 'display: none; '
423 . 'position: absolute ;'
425 . 'margin-left: -'.round($newwidth / 2).'px;'
426 . 'text-align: center; '
427 . 'vertical-align: top',
428 'name' => "wikislide".$count);
429 if ($align == 'left' || $align == 'right') {
431 $cell=array('style' => 'display: block; '
432 .'position: absolute; '
434 .'vertical-align: top',
435 'name' => "wikislide".$count);
437 $cell=array('style' => 'display: none; '
438 .'position: absolute; '
440 .'vertical-align: top',
441 'name' => "wikislide".$count);
446 HTML::span(array('class'=>'gensmall'), $desc)
450 return $this->error(fmt("Invalid argument: %s=%s", 'mode', $mode));
453 // no more images in one row as defined by $numcols
454 if ( ($key + 1) % $numcols == 0 ||
455 ($key + 1) == count($photos) ||
457 $html->pushcontent(HTML::tr($row));
458 $row->setContent('');
463 $table_attributes = array("border" => 0,
466 "width" => $tablewidth);
468 if (!@empty($tableheight))
469 $table_attributes = array_merge($table_attributes,
470 array("height" => $tableheight));
471 $html = HTML::table($table_attributes, $html);
473 return HTML::div(array("align" => $align), $html);
477 * Calculate the new size in pixels when the original size
478 * with a value is given.
480 * @param integer $oldSize Absolute no. of pixels
481 * @param mixed $value Either absolute no. or HTML percentage e.g. '50%'
482 * @return integer New size in pixels
484 function newSize($oldSize, $value) {
485 if (trim(substr($value,strlen($value)-1)) != "%") {
488 $value = str_replace("%", "", $value);
489 return round(($oldSize*$value)/100);
493 * fromLocation - read only one picture from fixed album_location
494 * and return it in array $photos
496 * @param string $src Name of page
497 * @param array $photos
498 * @return string Error if fixed location is not allowed
500 function fromLocation($src, &$photos) {
501 /*if (!allow_album_location) {
502 return $this->error(_("Fixed album location is not allowed. Please specify parameter src."));
505 if (! IsSafeURL($src)) {
506 return $this->error(_("Bad url in src: remove all of <, >, \""));
508 $photos[] = array ("name" => $src, //album_location."/$src".album_default_extension,
513 * fromFile - read pictures & descriptions (separated by ;)
514 * from $src and return it in array $photos
516 * @param string $src path to dir or textfile (local or remote)
517 * @param array $photos
518 * @return string Error when bad url or file couldn't be opened
520 function fromFile($src, &$photos, $webpath='') {
522 if (! IsSafeURL($src)) {
523 return $this->error(_("Bad url in src: remove all of <, >, \""));
525 if (preg_match('/^(http|ftp|https):\/\//i', $src)) {
526 $contents = url_get_contents($src);
530 if (!file_exists($src) and @file_exists(PHPWIKI_DIR . "/$src")) {
531 $src = PHPWIKI_DIR . "/$src";
533 // check if src is a directory
534 if (file_exists($src) and filetype($src) == 'dir') {
537 foreach (array('jpeg','jpg','png','gif') as $ext) {
538 $fileset = new fileSet($src, "*.$ext");
539 $list = array_merge($list, $fileset->getFiles());
541 // convert dirname($src) (local fs path) to web path
544 // assume relative src. default: "themes/Hawaiian/images/pictures"
545 $webpath = DATA_PATH . '/' . $src_bak;
547 foreach ($list as $file) {
548 // convert local path to webpath
549 $photos[] = array ("src" => $file,
550 "name" => $webpath . "/$file",
551 "name_tile" => $src . "/$file",
552 "src" => $src . "/$file",
557 // check if $src is an image
558 foreach (array('jpeg','jpg','png','gif') as $ext) {
559 if (preg_match("/\.$ext$/", $src)) {
560 if (!file_exists($src) and @file_exists(PHPWIKI_DIR . "/$src"))
561 $src = PHPWIKI_DIR . "/$src";
562 if ($web_location == 1 and !empty($contents)) {
563 $photos[] = array ("src" => $src,
570 if (!file_exists($src))
571 return $this->error(fmt("Unable to find src='%s'", $src));
572 $photos[] = array ("src" => $src,
573 "name" => "../".$src,
580 if ($web_location == 0) {
581 $fp = @fopen($src, "r");
583 return $this->error(fmt("Unable to read src='%s'", $src));
585 while ($data = fgetcsv($fp, 1024, ';')) {
586 if (count($data) == 0 || empty($data[0]) || preg_match('/^#/',$data[0]))
588 if (empty($data[1])) $data[1] = '';
589 $photos[] = array ("name" => dirname($src)."/".trim($data[0]),
590 "location" => "../".dirname($src)."/".trim($data[0]),
591 "desc" => trim($data[1]),
592 "name_tile" => dirname($src)."/".trim($data[0]));
596 } elseif ($web_location == 1) {
597 //TODO: checks if the file is an image
598 $contents = preg_split('/\n/',$contents);
599 while (list($key,$value) = each($contents)) {
600 $data = preg_split('/\;/',$value);
601 if (count($data) == 0 || empty($data[0]) || preg_match('/^#/',$data[0]))
603 if (empty($data[1])) $data[1] = '';
604 $photos[] = array ("name" => dirname($src)."/".trim($data[0]),
605 "desc" => trim($data[1]),
606 "name_tile" => dirname($src)."/".trim($data[0]));
612 // $Log: not supported by cvs2svn $
614 // Revision 1.14 2005/09/19 23:49:00 tharding
615 // added slide mode, correct url retrieving with url_get_contents
617 // Revision 1.13 2005/09/17 18:17:00 tharding
618 // add resized thumbnails (see ImageTile.php at top-level)
619 // comment url_get_contents (fopen can open a web location)
621 // Revision 1.11 2004/12/06 19:50:05 rurban
622 // enable action=remove which is undoable and seeable in RecentChanges: ADODB ony for now.
623 // renamed delete_page to purge_page.
624 // enable action=edit&version=-1 to force creation of a new version.
625 // added BABYCART_PATH config
626 // fixed magiqc in adodb.inc.php
627 // and some more docs
629 // Revision 1.10 2004/12/01 19:34:13 rurban
630 // Cleanup of CONSTANT pollution.
631 // renamed weblocation to url.
633 // use fixed ";" CSV seperator
634 // fix substr_replace usage bug.
636 // Revision 1.9 2004/07/08 20:30:07 rurban
637 // plugin->run consistency: request as reference, added basepage.
638 // encountered strange bug in AllPages (and the test) which destroys ->_dbi
640 // Revision 1.8 2004/06/01 15:28:01 rurban
641 // AdminUser only ADMIN_USER not member of Administrators
642 // some RateIt improvements by dfrankow
643 // edit_toolbar buttons
645 // Revision 1.7 2004/05/03 20:44:55 rurban
646 // fixed gettext strings
647 // new SqlResult plugin
648 // _WikiTranslation: fixed init_locale
650 // Revision 1.6 2004/04/18 00:19:30 rurban
651 // better default example with local src, don't require weblocation for
652 // the default setup, better docs, fixed ini_get => get_cfg_var("allow_url_fopen"),
653 // no HttpClient lib yet.
655 // Revision 1.5 2004/03/09 12:10:23 rurban
656 // fixed getimagesize problem with local dir.
658 // Revision 1.4 2004/02/28 21:14:08 rurban
659 // generally more PHPDOC docs
660 // see http://xarch.tu-graz.ac.at/home/rurban/phpwiki/xref/
661 // fxied WikiUserNew pref handling: empty theme not stored, save only
662 // changed prefs, sql prefs improved, fixed password update,
663 // removed REPLACE sql (dangerous)
664 // moved gettext init after the locale was guessed
665 // + some minor changes
667 // Revision 1.3 2004/02/27 08:03:35 rurban
668 // Update from version 1.2 by Ted Vinke
669 // implemented the localdir support
671 // Revision 1.2 2004/02/17 12:11:36 rurban
672 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
674 // Revision 1.1 2003/01/05 04:21:06 carstenklapp
675 // New plugin by Ted Vinke (sf tracker patch #661189)
683 // c-hanging-comment-ender-p: nil
684 // indent-tabs-mode: nil