2 rcs_id('$Id: PhotoAlbum.php,v 1.14 2005-10-12 06:19:07 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|column|row|thumbs|tiles|list|slide]
39 * align=[center|left|right]
43 * "src": textfile of images or directory of images or a single image (local or remote)
44 * Local or remote e.g. http://myserver/images/MyPhotos.txt or http://myserver/images/
45 * Possible content of a valid textfile:
46 * photo-01.jpg; Me and my girlfriend
48 * christmas.gif; Merry Christmas!
50 * Inside textfile, filenames and optional descriptions are seperated by
51 * semi-colon on each line. Listed files must be in same directory as textfile
52 * itself, so don't use relative paths inside textfile.
54 * "url": defines the the webpath to the srcdir directory (formerly called weblocation)
59 * - specify picture(s) as parameter(s)
60 * - limit amount of pictures on one page
61 * - use PHP to really resize or greyscale images (only where GD library supports it)
62 * (quite done for resize with "ImageTile.php")
65 * - reading height and width from images with spaces in their names fails.
67 * Fixed album location idea by Philip J. Hollenback. Thanks!
70 class ImageTile extends HtmlElement
72 function image_tile (/*...*/) {
73 $el = new HTML ('img');
74 $tag = func_get_args();
75 $params = "<img src='../ImageTile.php?url=". $tag[0]['src'];
76 if (!@empty($tag[0]['width']))
77 $params .= "&width=" . $tag[0]['width'];
78 if (!@empty($tag[0]['height']))
79 $params .= "&height=" . $tag[0]['height'];
80 if (!@empty($tag[0]['width']))
81 $params .= "' width='" . $tag[0]['width'];
82 if (!@empty($tag[0]['height']))
83 $params .= "' height='" . $tag[0]['height'];
85 $params .= "' alt='" . $tag[0]['alt'] . "' />";
86 return $el->raw ($params);
90 class WikiPlugin_PhotoAlbum
94 return _("PhotoAlbum");
97 function getDescription () {
98 return _("Displays a set of photos listed in a text file with optional descriptions");
101 function getVersion() {
102 return preg_replace("/[Revision: $]/", '',
103 "\$Revision: 1.14 $");
106 // Avoid nameclash, so it's disabled. We allow any url.
107 // define('allow_album_location', true);
108 // define('album_location', 'http://kw.jouwfeestje.com/foto/redactie');
109 // define('album_default_extension', '.jpg');
110 // define('desc_separator', ';');
112 function getDefaultArguments() {
113 return array('src' => '', // textfile of image list, or local dir.
114 'url' => '', // if src=localfs, url prefix (webroot for the links)
115 'mode' => 'normal', // normal|thumbs|tiles|list
116 // "normal" - Normal table which shows photos full-size
117 // "thumbs" - WinXP thumbnail style
118 // "tiles" - WinXP tiles style
119 // "list" - WinXP list style
120 // "row" - inline thumbnails
121 // "column" - photos full-size, displayed in 1 column
122 // "slide" - slideshow mode, needs javascript on client
123 'numcols' => 3, // photos per row, columns
124 'showdesc' => 'both', // none|name|desc|both
125 // "none" - No descriptions next to photos
126 // "name" - Only filename shown
127 // "desc" - Only description (from textfile) shown
128 // "both" - If no description found, then filename will be used
129 'link' => true, // show link to original sized photo
130 // If true, each image will be hyperlinked to a page where the single
131 // photo will be shown full-size. Only works when mode != 'normal'
132 'attrib' => '', // 'sort, nowrap, alt'
133 // attrib arg allows multiple attributes: attrib=sort,nowrap,alt
134 // 'sort' sorts alphabetically, 'nowrap' for cells, 'alt' to use
135 // descs instead of filenames in image ALT-tags
136 'bgcolor' => '#eae8e8', // cell bgcolor (lightgrey)
137 'hlcolor' => '#c0c0ff', // highlight color (lightblue)
138 'align' => 'center', // alignment of table
139 'height' => 'auto', // image height (auto|75|100%)
140 'width' => 'auto', // image width (auto|75|100%)
141 // Size of shown photos. Either absolute value (e.g. "50") or
142 // HTML style percentage (e.g. "75%") or "auto" for no special
144 'cellwidth'=> 'image', // cell (auto|equal|image|75|100%)
145 // Width of cells in table. Either absolute value in pixels, HTML
146 // style percentage, "auto" (no special action), "equal" (where
147 // all columns are equally sized) or "image" (take height and
148 // width of the photo in that cell).
149 'tablewidth'=> false, // table (75|100%)
150 'p' => false, // "displaythissinglephoto.jpg"
151 'h' => false, // "highlightcolorofthisphoto.jpg"
152 'duration' => 6, // in slide mode, in seconds
153 'thumbswidth' => 80 //width of thumbnails
156 // descriptions (instead of filenames) for image alt-tags
158 function run($dbi, $argstr, &$request, $basepage) {
160 extract($this->getArgs($argstr, $request));
162 $attributes = $attrib ? explode(",", $attrib) : array();
166 // check all parameters
167 // what type do we have?
170 $src = $request->getArg('pagename');
171 $error = $this->fromLocation($src, $photos);
173 $error = $this->fromFile($src, $photos, $url);
176 return $this->error($error);
179 if ($numcols < 1) $numcols = 1;
180 if ($align != 'left' && $align != 'center' && $align != 'right') {
183 if (count($photos) == 0) return;
185 if (in_array("sort", $attributes))
192 if ($mode == "column") {
197 // set some fixed properties for each $mode
198 if ($mode == 'thumbs' || $mode == 'tiles') {
199 $attributes = array_merge($attributes, "alt");
200 $attributes = array_merge($attributes, "nowrap");
201 $cellwidth = 'auto'; // else cell won't nowrap
203 } elseif ($mode == 'list') {
207 } elseif ($mode == 'slide' ) {
210 $numcols = count($photos);
212 while (list($key, $value) = each($photos)) {
213 list($x,$y,$s,$t) = @getimagesize($value['src']);
214 if ($height != 'auto') $y = $this->newSize($y, $height);
215 if ($width != 'auto') $y = round($y * $this->newSize($x, $width) / $x);
216 if ($x > $cell_width) $cell_width = $x;
217 if ($y > $tableheight) $tableheight = $y;
221 unset ($x,$y,$s,$t,$key,$value,$keep);
225 $duration = 1000 * $duration;
226 if ($mode == 'slide')
227 $row->pushContent(JavaScript("
229 function display_slides() {
231 cell0 = document.getElementsByName('wikislide' + j);
232 cell = document.getElementsByName('wikislide' + i);
233 if (cell0.item(0) != null)
234 cell0.item(0).style.display='none';
235 if (cell.item(0) != null)
236 cell.item(0).style.display='block';
238 if (cell.item(0) == null) i = 0;
239 setTimeout('display_slides()',$duration);
241 display_slides();"));
243 while (list($key, $value) = each($photos)) {
244 if ($p && basename($value["name"]) != "$p") {
247 if ($h && basename($value["name"]) == "$h") {
248 $color = $hlcolor ? $hlcolor : $bgcolor;
252 // $params will be used for each <img > tag
253 $params = array('src' => $value["name"],
254 'src_tile' => $value["name_tile"],
256 'alt' => ($value["desc"] != "" and in_array("alt", $attributes))
258 : basename($value["name"]));
259 if (!@empty($value['location']))
260 $params = array_merge($params, array("location" => $value['location']));
267 $value["desc"] = basename($value["name"]);
272 if (!$value["desc"]) $value["desc"] = basename($value["name"]);
276 // FIXME: get getimagesize to work with names with spaces in it.
277 // convert $value["name"] from webpath to local path
278 $size = @getimagesize($value["name"]); // try " " => "\\ "
279 if (!$size and !empty($value["src"])) {
280 $size = @getimagesize($value["src"]);
282 trigger_error("Unable to getimagesize(".$value["name"].")",
287 $newwidth = $this->newSize($size[0], $width);
288 if (($mode == 'thumbs' || $mode == 'tiles' || $mode == 'list')) {
289 if (!empty($size[0]))
290 $newheight = round (50 * $size[1] / $size[0]);
291 else $newheight = '';
292 if ($height == 'auto') $height=150;
295 $newheight = $this->newSize($size[1], $height);
297 if ($width != 'auto' && $newwidth > 0) {
298 $params = array_merge($params, array("width" => $newwidth));
300 if ($height != 'auto' && $newheight > 0) {
301 $params = array_merge($params, array("height" => $newheight));
305 $cell = array('align' => "center",
307 'bgcolor' => "$color");
308 if ($cellwidth != 'auto') {
309 if ($cellwidth == 'equal') {
310 $newcellwidth = round(100/$numcols)."%";
311 } else if ($cellwidth == 'image') {
312 $newcellwidth = $newwidth;
314 $newcellwidth = $cellwidth;
316 $cell = array_merge($cell, array("width" => $newcellwidth));
318 if (in_array("nowrap", $attributes)) {
319 $cell = array_merge($cell, array("nowrap" => "nowrap"));
321 //create url to display single larger version of image on page
322 $url = WikiURL($request->getPage(),
323 array("p" => basename($value["name"])))
325 . basename($value["name"]);
327 $b_url = WikiURL($request->getPage(),
328 array("h" => basename($value["name"])))
330 . basename($value["name"]);
332 ? HTML::a(array("href" => "$url"), basename($value["desc"]))
333 : basename($value["name"]);
335 if ($mode == 'normal' || $mode == 'slide') {
336 if(!@empty($params['location'])) $params['src'] = $params['location'];
337 unset ($params['location'],$params['src_tile']);
338 $url_image = $link ? HTML::a(array("id" => basename($value["name"])),
339 HTML::a(array("href" => "$url"), HTML::img($params))) : HTML::img($params);
342 if (!@empty ($params['src_tile']))
343 $params['src'] = $params['src_tile'] ;
344 unset ($params['location'],$params['src_tile']);
345 $url_image = $link ? HTML::a(array("id" => basename($value["name"])),
346 HTML::a(array("href" => "$url"),
347 ImageTile::image_tile($params))) : HTML::img($params);
352 if(!@empty($params['location'])) $params['src'] = $params['location'];
353 unset ($params['location'],$params['src_tile']);
354 $url_image = $link ? HTML::a(array("id" => basename($value["name"])),
355 HTML::a(array("href" => "$b_url"), HTML::img($params))) : HTML::img($params);
358 $url_text = HTML::a(array("id" => basename($value["name"])),
360 // here we use different modes
361 if ($mode == 'tiles') {
364 HTML::table(array("cellpadding" => 1, "border" => 0),
366 HTML::td(array("valign" => "top", "rowspan" => 2),
368 HTML::td(array("valign" => "top", "nowrap" => 0),
369 HTML::span(array('class'=>'boldsmall'),
372 HTML::span(array('class'=>'gensmall'),
378 } elseif ($mode == 'list') {
379 $desc = ($showdesc != 'none') ? $value["desc"] : '';
381 HTML::td(array("valign" => "top",
383 "bgcolor" => $color),
384 HTML::span(array('class'=>'boldsmall'),($url_text))));
386 HTML::td(array("valign" => "top",
388 "bgcolor" => $color),
389 HTML::span(array('class'=>'gensmall'),
397 HTML::td(array("valign" => "top",
399 "bgcolor" => $color),
400 HTML::span(array('class'=>'gensmall'),$desc)));
402 } elseif ($mode == 'thumbs') {
403 $desc = ($showdesc != 'none') ?
404 HTML::p(HTML::a(array("href" => "$url"),
409 // FIXME: no HtmlElement for fontsizes?
410 // rurban: use ->setAttr("style","font-size:small;")
411 // but better use a css class
412 HTML::span(array('class'=>'gensmall'),$desc)
414 } elseif ($mode == 'normal') {
415 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
419 // FIXME: no HtmlElement for fontsizes?
420 HTML::span(array('class'=>'gensmall'),$desc)
422 } elseif ($mode == 'slide') {
423 if ($newwidth == 'auto' || !$newwidth)
424 $newwidth = $this->newSize($size[0],$width);
425 if ($newwidth == 'auto' || !$newwidth)
426 $newwidth = $size[0];
427 if ($newheight != 'auto') $newwidth = round($size[0] * $newheight / $size[1]);
428 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
430 $cell=array('style' => 'display: block; '
431 . 'position: absolute; '
433 . 'margin-left: -'.round($newwidth / 2).'px;'
434 . 'text-align: center; '
435 . 'vertical-align: top',
436 'name' => "wikislide".$count);
438 $cell=array('style' => 'display: none; '
439 . 'position: absolute ;'
441 . 'margin-left: -'.round($newwidth / 2).'px;'
442 . 'text-align: center; '
443 . 'vertical-align: top',
444 'name' => "wikislide".$count);
445 if ($align == 'left' || $align == 'right') {
447 $cell=array('style' => 'display: block; '
448 .'position: absolute; '
450 .'vertical-align: top',
451 'name' => "wikislide".$count);
453 $cell=array('style' => 'display: none; '
454 .'position: absolute; '
456 .'vertical-align: top',
457 'name' => "wikislide".$count);
462 HTML::span(array('class'=>'gensmall'), $desc)
465 } elseif ($mode == 'row') {
466 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
468 HTML::table(array("style" => "display: inline"),
469 HTML::tr(HTML::td($url_image)),
470 HTML::tr(HTML::td(array("class" => "gensmall",
471 "style" => "text-align: center; "
472 ."background-color: $color"),
476 return $this->error(fmt("Invalid argument: %s=%s", 'mode', $mode));
479 // no more images in one row as defined by $numcols
480 if ( ($key + 1) % $numcols == 0 ||
481 ($key + 1) == count($photos) ||
484 $html->pushcontent(HTML::span($row));
486 $html->pushcontent(HTML::tr($row));
487 $row->setContent('');
492 $table_attributes = array("border" => 0,
495 "width" => $tablewidth);
497 if (!@empty($tableheight))
498 $table_attributes = array_merge($table_attributes,
499 array("height" => $tableheight));
501 $html = HTML::table($table_attributes, $html);
503 return HTML::div(array("align" => $align), $html);
507 * Calculate the new size in pixels when the original size
508 * with a value is given.
510 * @param integer $oldSize Absolute no. of pixels
511 * @param mixed $value Either absolute no. or HTML percentage e.g. '50%'
512 * @return integer New size in pixels
514 function newSize($oldSize, $value) {
515 if (trim(substr($value,strlen($value)-1)) != "%") {
518 $value = str_replace("%", "", $value);
519 return round(($oldSize*$value)/100);
523 * fromLocation - read only one picture from fixed album_location
524 * and return it in array $photos
526 * @param string $src Name of page
527 * @param array $photos
528 * @return string Error if fixed location is not allowed
530 function fromLocation($src, &$photos) {
531 /*if (!allow_album_location) {
532 return $this->error(_("Fixed album location is not allowed. Please specify parameter src."));
535 if (! IsSafeURL($src)) {
536 return $this->error(_("Bad url in src: remove all of <, >, \""));
538 $photos[] = array ("name" => $src, //album_location."/$src".album_default_extension,
543 * fromFile - read pictures & descriptions (separated by ;)
544 * from $src and return it in array $photos
546 * @param string $src path to dir or textfile (local or remote)
547 * @param array $photos
548 * @return string Error when bad url or file couldn't be opened
550 function fromFile($src, &$photos, $webpath='') {
552 //there has a big security hole... as loading config/config.ini !
553 if (!preg_match('/(\.csv|\.jpg|\.jpeg|\.png|\.gif|\/)$/',$src)) {
554 return $this->error(_("File extension for csv file has to be '.csv'"));
556 if (! IsSafeURL($src)) {
557 return $this->error(_("Bad url in src: remove all of <, >, \""));
559 if (preg_match('/^(http|ftp|https):\/\//i', $src)) {
560 $contents = url_get_contents($src);
564 if (!file_exists($src) and @file_exists(PHPWIKI_DIR . "/$src")) {
565 $src = PHPWIKI_DIR . "/$src";
567 // check if src is a directory
568 if (file_exists($src) and filetype($src) == 'dir') {
571 foreach (array('jpeg','jpg','png','gif') as $ext) {
572 $fileset = new fileSet($src, "*.$ext");
573 $list = array_merge($list, $fileset->getFiles());
575 // convert dirname($src) (local fs path) to web path
578 // assume relative src. default: "themes/Hawaiian/images/pictures"
579 $webpath = DATA_PATH . '/' . $src_bak;
581 foreach ($list as $file) {
582 // convert local path to webpath
583 $photos[] = array ("src" => $file,
584 "name" => $webpath . "/$file",
585 "name_tile" => $src . "/$file",
586 "src" => $src . "/$file",
591 // check if $src is an image
592 foreach (array('jpeg','jpg','png','gif') as $ext) {
593 if (preg_match("/\.$ext$/", $src)) {
594 if (!file_exists($src) and @file_exists(PHPWIKI_DIR . "/$src"))
595 $src = PHPWIKI_DIR . "/$src";
596 if ($web_location == 1 and !empty($contents)) {
597 $photos[] = array ("src" => $src,
604 if (!file_exists($src))
605 return $this->error(fmt("Unable to find src='%s'", $src));
606 $photos[] = array ("src" => $src,
607 "name" => "../".$src,
614 if ($web_location == 0) {
615 $fp = @fopen($src, "r");
617 return $this->error(fmt("Unable to read src='%s'", $src));
619 while ($data = fgetcsv($fp, 1024, ';')) {
620 if (count($data) == 0 || empty($data[0])
621 || preg_match('/^#/',$data[0])
622 || preg_match('/^[[:space:]]*$/',$data[0]))
624 if (empty($data[1])) $data[1] = '';
625 $photos[] = array ("name" => dirname($src)."/".trim($data[0]),
626 "location" => "../".dirname($src)."/".trim($data[0]),
627 "desc" => trim($data[1]),
628 "name_tile" => dirname($src)."/".trim($data[0]));
632 } elseif ($web_location == 1) {
633 //TODO: checks if the file is an image
634 $contents = preg_split('/\n/',$contents);
635 while (list($key,$value) = each($contents)) {
636 $data = preg_split('/\;/',$value);
637 if (count($data) == 0 || empty($data[0])
638 || preg_match('/^#/',$data[0])
639 || preg_match('/^[[:space:]]*$/',$data[0]))
641 if (empty($data[1])) $data[1] = '';
642 $photos[] = array ("name" => dirname($src)."/".trim($data[0]),
643 "src" => dirname($src)."/".trim($data[0]),
644 "desc" => trim($data[1]),
645 "name_tile" => dirname($src)."/".trim($data[0]));
651 // $Log: not supported by cvs2svn $
652 // Revision 1.13 2005/09/26 06:39:55 rurban
653 // re-add lost mode=column|row. by Thomas Harding
655 // Revision 1.12 2005/09/20 19:34:51 rurban
656 // slide and thumbs mode by Thomas Harding
659 // Revision 1.14 2005/09/19 23:49:00 tharding
660 // added slide mode, correct url retrieving with url_get_contents
662 // Revision 1.13 2005/09/17 18:17:00 tharding
663 // add resized thumbnails (see ImageTile.php at top-level)
664 // comment url_get_contents (fopen can open a web location)
666 // Revision 1.11 2004/12/06 19:50:05 rurban
667 // enable action=remove which is undoable and seeable in RecentChanges: ADODB ony for now.
668 // renamed delete_page to purge_page.
669 // enable action=edit&version=-1 to force creation of a new version.
670 // added BABYCART_PATH config
671 // fixed magiqc in adodb.inc.php
672 // and some more docs
674 // Revision 1.10 2004/12/01 19:34:13 rurban
675 // Cleanup of CONSTANT pollution.
676 // renamed weblocation to url.
678 // use fixed ";" CSV seperator
679 // fix substr_replace usage bug.
681 // Revision 1.9 2004/07/08 20:30:07 rurban
682 // plugin->run consistency: request as reference, added basepage.
683 // encountered strange bug in AllPages (and the test) which destroys ->_dbi
685 // Revision 1.8 2004/06/01 15:28:01 rurban
686 // AdminUser only ADMIN_USER not member of Administrators
687 // some RateIt improvements by dfrankow
688 // edit_toolbar buttons
690 // Revision 1.7 2004/05/03 20:44:55 rurban
691 // fixed gettext strings
692 // new SqlResult plugin
693 // _WikiTranslation: fixed init_locale
695 // Revision 1.6 2004/04/18 00:19:30 rurban
696 // better default example with local src, don't require weblocation for
697 // the default setup, better docs, fixed ini_get => get_cfg_var("allow_url_fopen"),
698 // no HttpClient lib yet.
700 // Revision 1.5 2004/03/09 12:10:23 rurban
701 // fixed getimagesize problem with local dir.
703 // Revision 1.4 2004/02/28 21:14:08 rurban
704 // generally more PHPDOC docs
705 // see http://xarch.tu-graz.ac.at/home/rurban/phpwiki/xref/
706 // fxied WikiUserNew pref handling: empty theme not stored, save only
707 // changed prefs, sql prefs improved, fixed password update,
708 // removed REPLACE sql (dangerous)
709 // moved gettext init after the locale was guessed
710 // + some minor changes
712 // Revision 1.3 2004/02/27 08:03:35 rurban
713 // Update from version 1.2 by Ted Vinke
714 // implemented the localdir support
716 // Revision 1.2 2004/02/17 12:11:36 rurban
717 // 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, ...)
719 // Revision 1.1 2003/01/05 04:21:06 carstenklapp
720 // New plugin by Ted Vinke (sf tracker patch #661189)
728 // c-hanging-comment-ender-p: nil
729 // indent-tabs-mode: nil