4 * Copyright 2003,2004,2005,2007 $ThePhpWikiProgrammingTeam
5 * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Display an album of a set of photos with optional descriptions.
27 * @author: Ted Vinke <teddy@jouwfeestje.com>
28 * Reini Urban (local fs)
29 * Thomas Harding (slides mode, real thumbnails)
33 * src="http://server/textfile" or localfile or localdir
34 * mode=[normal|column|row|thumbs|tiles|list|slide]
40 * align=[center|left|right]
44 * "src": textfile of images or directory of images or a single image (local or remote)
45 * Local or remote e.g. http://myserver/images/MyPhotos.txt or http://myserver/images/
46 * or /images/ or Upload:photos/
47 * Possible content of a valid textfile:
48 * photo-01.jpg; Me and my girlfriend
50 * christmas.gif; Merry Christmas!
52 * Inside textfile, filenames and optional descriptions are separated by
53 * semi-colon on each line. Listed files must be in same directory as textfile
54 * itself, so don't use relative paths inside textfile.
56 * "url": defines the the webpath to the srcdir directory (formerly called weblocation)
61 * - specify picture(s) as parameter(s)
62 * - limit amount of pictures on one page
63 * - use PHP to really resize or greyscale images (only where GD library supports it)
64 * (quite done for resize with "ImageTile.php")
67 * - reading height and width from images with spaces in their names fails.
69 * Fixed album location idea by Philip J. Hollenback. Thanks!
72 class ImageTile extends HtmlElement
75 function image_tile( /*...*/)
77 $el = new HTML ('img');
78 $tag = func_get_args();
79 $path = DATA_PATH . "/ImageTile.php";
80 $params = "<img src=\"$path?url=" . $tag[0]['src'];
81 if (!@empty($tag[0]['width']))
82 $params .= "&width=" . $tag[0]['width'];
83 if (!@empty($tag[0]['height']))
84 $params .= "&height=" . $tag[0]['height'];
85 if (!@empty($tag[0]['width']))
86 $params .= '" width="' . $tag[0]['width'];
87 if (!@empty($tag[0]['height']))
88 $params .= '" height="' . $tag[0]['height'];
90 $params .= '" alt="' . $tag[0]['alt'] . '" />';
91 return $el->raw($params);
95 class WikiPlugin_PhotoAlbum
100 return _("PhotoAlbum");
103 function getDescription()
105 return _("Display a set of photos listed in a text file with optional descriptions.");
108 // Avoid nameclash, so it's disabled. We allow any url.
109 // define('allow_album_location', true);
110 // define('album_location', 'http://kw.jouwfeestje.com/foto/redactie');
111 // define('album_default_extension', '.jpg');
112 // define('desc_separator', ';');
114 function getDefaultArguments()
116 return array('src' => '', // textfile of image list, or local dir.
117 'url' => '', // if src=localfs, url prefix (webroot for the links)
118 'mode' => 'normal', // normal|thumbs|tiles|list
119 // "normal" - Normal table which shows photos full-size
120 // "thumbs" - WinXP thumbnail style
121 // "tiles" - WinXP tiles style
122 // "list" - WinXP list style
123 // "row" - inline thumbnails
124 // "column" - photos full-size, displayed in 1 column
125 // "slide" - slideshow mode, needs javascript on client
126 'numcols' => 3, // photos per row, columns
127 'showdesc' => 'both', // none|name|desc|both
128 // "none" - No descriptions next to photos
129 // "name" - Only filename shown
130 // "desc" - Only description (from textfile) shown
131 // "both" - If no description found, then filename will be used
132 'link' => true, // show link to original sized photo
133 // If true, each image will be hyperlinked to a page where the single
134 // photo will be shown full-size. Only works when mode != 'normal'
135 'attrib' => '', // 'sort, nowrap, alt'
136 // attrib arg allows multiple attributes: attrib=sort,nowrap,alt
137 // 'sort' sorts alphabetically, 'nowrap' for cells, 'alt' to use
138 // descs instead of filenames in image ALT-tags
139 'bgcolor' => '#eae8e8', // cell bgcolor (lightgrey)
140 'hlcolor' => '#c0c0ff', // highlight color (lightblue)
141 'align' => 'center', // alignment of table
142 'height' => 'auto', // image height (auto|75|100%)
143 'width' => 'auto', // image width (auto|75|100%)
144 // Size of shown photos. Either absolute value (e.g. "50") or
145 // HTML style percentage (e.g. "75%") or "auto" for no special
147 'cellwidth' => 'image', // cell (auto|equal|image|75|100%)
148 // Width of cells in table. Either absolute value in pixels, HTML
149 // style percentage, "auto" (no special action), "equal" (where
150 // all columns are equally sized) or "image" (take height and
151 // width of the photo in that cell).
152 'tablewidth' => false, // table (75|100%)
153 'p' => false, // "displaythissinglephoto.jpg"
154 'h' => false, // "highlightcolorofthisphoto.jpg"
155 'duration' => 6, // in slide mode, in seconds
156 'thumbswidth' => 80 //width of thumbnails
160 // descriptions (instead of filenames) for image alt-tags
162 function run($dbi, $argstr, &$request, $basepage)
165 extract($this->getArgs($argstr, $request));
167 $attributes = $attrib ? explode(",", $attrib) : array();
171 // check all parameters
172 // what type do we have?
175 $src = $request->getArg('pagename');
176 $error = $this->fromLocation($src, $photos);
178 $error = $this->fromFile($src, $photos, $url);
181 return $this->error($error);
184 if ($numcols < 1) $numcols = 1;
185 if ($align != 'left' && $align != 'center' && $align != 'right') {
188 if (count($photos) == 0) return;
190 if (in_array("sort", $attributes))
197 if ($mode == "column") {
202 // set some fixed properties for each $mode
203 if ($mode == 'thumbs' || $mode == 'tiles') {
204 $attributes = array_merge($attributes, "alt");
205 $attributes = array_merge($attributes, "nowrap");
206 $cellwidth = 'auto'; // else cell won't nowrap
207 if ($width == 'auto') $width = 70;
208 } elseif ($mode == 'list') {
211 if ($width == 'auto') $width = 50;
212 } elseif ($mode == 'slide') {
215 $numcols = count($photos);
217 while (list($key, $value) = each($photos)) {
218 list($x, $y, $s, $t) = @getimagesize($value['src']);
219 if ($height != 'auto') $y = $this->newSize($y, $height);
220 if ($width != 'auto') $y = round($y * $this->newSize($x, $width) / $x);
221 if ($x > $cell_width) $cell_width = $x;
222 if ($y > $tableheight) $tableheight = $y;
226 unset ($x, $y, $s, $t, $key, $value, $keep);
230 $duration = 1000 * $duration;
231 if ($mode == 'slide')
232 $row->pushContent(JavaScript("
234 function display_slides() {
236 cell0 = document.getElementsByName('wikislide' + j);
237 cell = document.getElementsByName('wikislide' + i);
238 if (cell0.item(0) != null)
239 cell0.item(0).style.display='none';
240 if (cell.item(0) != null)
241 cell.item(0).style.display='block';
243 if (cell.item(0) == null) i = 0;
244 setTimeout('display_slides()',$duration);
246 display_slides();"));
248 while (list($key, $value) = each($photos)) {
249 if ($p && basename($value["name"]) != "$p") {
252 if ($h && basename($value["name"]) == "$h") {
253 $color = $hlcolor ? $hlcolor : $bgcolor;
257 // $params will be used for each <img > tag
258 $params = array('src' => $value["name"],
259 'src_tile' => $value["name_tile"],
260 'alt' => ($value["desc"] != "" and in_array("alt", $attributes))
262 : basename($value["name"]));
263 if (!@empty($value['location']))
264 $params = array_merge($params, array("location" => $value['location']));
271 $value["desc"] = basename($value["name"]);
276 if (!$value["desc"]) $value["desc"] = basename($value["name"]);
280 // FIXME: get getimagesize to work with names with spaces in it.
281 // convert $value["name"] from webpath to local path
282 $size = @getimagesize($value["name"]); // try " " => "\\ "
283 if (!$size and !empty($value["src"])) {
284 $size = @getimagesize($value["src"]);
286 trigger_error("Unable to getimagesize(" . $value["name"] . ")",
290 $newwidth = $this->newSize($size[0], $width);
291 if ($width != 'auto' && $newwidth > 0) {
292 $params = array_merge($params, array("width" => $newwidth));
294 if (($mode == 'thumbs' || $mode == 'tiles' || $mode == 'list')) {
295 if (!empty($size[0])) {
296 $newheight = round($newwidth * $size[1] / $size[0]);
297 $params['width'] = $newwidth;
298 $params['height'] = $newheight;
299 } else $newheight = '';
300 if ($height == 'auto') $height = 150;
302 $newheight = $this->newSize($size[1], $height);
303 if ($height != 'auto' && $newheight > 0) {
304 $params = array_merge($params, array("height" => $newheight));
309 $cell = array('align' => "center",
311 'class' => 'photoalbum cell',
312 'bgcolor' => "$color");
313 if ($cellwidth != 'auto') {
314 if ($cellwidth == 'equal') {
315 $newcellwidth = round(100 / $numcols) . "%";
316 } elseif ($cellwidth == 'image') {
317 $newcellwidth = $newwidth;
319 $newcellwidth = $cellwidth;
321 $cell = array_merge($cell, array("width" => $newcellwidth));
323 if (in_array("nowrap", $attributes)) {
324 $cell = array_merge($cell, array("nowrap" => "nowrap"));
326 //create url to display single larger version of image on page
327 $url = WikiURL($request->getPage(),
328 array("p" => basename($value["name"])))
330 . basename($value["name"]);
332 $b_url = WikiURL($request->getPage(),
333 array("h" => basename($value["name"])))
335 . basename($value["name"]);
337 ? HTML::a(array("href" => "$url"), basename($value["desc"]))
338 : basename($value["name"]);
340 if ($mode == 'normal' || $mode == 'slide') {
341 if (!@empty($params['location'])) $params['src'] = $params['location'];
342 unset ($params['location'], $params['src_tile']);
343 $url_image = $link ? HTML::a(array("id" => basename($value["name"]),
344 "href" => "$url"), HTML::img($params))
345 : HTML::img($params);
348 if (!@empty ($params['src_tile']))
349 $params['src'] = $params['src_tile'];
350 unset ($params['location'], $params['src_tile']);
351 $url_image = $link ? HTML::a(array("id" => basename($value["name"]),
353 ImageTile::image_tile($params))
354 : HTML::img($params);
359 if (!@empty($params['location'])) $params['src'] = $params['location'];
360 unset ($params['location'], $params['src_tile']);
361 $url_image = $link ? HTML::a(array("id" => basename($value["name"]),
362 "href" => "$b_url"), HTML::img($params))
363 : HTML::img($params);
366 $url_text = HTML::a(array("id" => basename($value["name"])),
368 // here we use different modes
369 if ($mode == 'tiles') {
372 HTML::div(array('valign' => 'top'), $url_image),
373 HTML::div(array('valign' => 'bottom'),
374 HTML::div(array('class' => 'boldsmall'),
377 HTML::div(array('class' => 'gensmall'),
383 } elseif ($mode == 'list') {
384 $desc = ($showdesc != 'none') ? $value["desc"] : '';
386 HTML::td(array("valign" => "top",
388 "bgcolor" => $color),
389 HTML::div(array('class' => 'boldsmall'), ($url_text))));
391 HTML::td(array("valign" => "top",
393 "bgcolor" => $color),
394 HTML::div(array('class' => 'gensmall'),
402 HTML::td(array("valign" => "top",
404 "bgcolor" => $color),
405 HTML::div(array('class' => 'gensmall'), $desc)));
407 } elseif ($mode == 'thumbs') {
408 $desc = ($showdesc != 'none') ?
409 HTML::p(HTML::a(array("href" => "$url"),
414 // FIXME: no HtmlElement for fontsizes?
415 // rurban: use ->setAttr("style","font-size:small;")
416 // but better use a css class
417 HTML::div(array('class' => 'gensmall'), $desc)
419 } elseif ($mode == 'normal') {
420 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
424 // FIXME: no HtmlElement for fontsizes?
425 HTML::div(array('class' => 'gensmall'), $desc)
427 } elseif ($mode == 'slide') {
428 if ($newwidth == 'auto' || !$newwidth)
429 $newwidth = $this->newSize($size[0], $width);
430 if ($newwidth == 'auto' || !$newwidth)
431 $newwidth = $size[0];
432 if ($newheight != 'auto') $newwidth = round($size[0] * $newheight / $size[1]);
433 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
435 $cell = array('style' => 'display: block; '
436 . 'position: absolute; '
438 . 'margin-left: -' . round($newwidth / 2) . 'px;'
439 . 'text-align: center; '
440 . 'vertical-align: top',
441 'name' => "wikislide" . $count);
443 $cell = array('style' => 'display: none; '
444 . 'position: absolute ;'
446 . 'margin-left: -' . round($newwidth / 2) . 'px;'
447 . 'text-align: center; '
448 . 'vertical-align: top',
449 'name' => "wikislide" . $count);
450 if ($align == 'left' || $align == 'right') {
452 $cell = array('style' => 'display: block; '
453 . 'position: absolute; '
454 . $align . ': 50px; '
455 . 'vertical-align: top',
456 'name' => "wikislide" . $count);
458 $cell = array('style' => 'display: none; '
459 . 'position: absolute; '
460 . $align . ': 50px; '
461 . 'vertical-align: top',
462 'name' => "wikislide" . $count);
467 HTML::div(array('class' => 'gensmall'), $desc)
470 } elseif ($mode == 'row') {
471 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
473 HTML::table(array("style" => "display: inline",
474 'class' > "photoalbum row"),
475 HTML::tr(HTML::td($url_image)),
476 HTML::tr(HTML::td(array("class" => "gensmall",
477 "style" => "text-align: center; "
478 . "background-color: $color"),
482 return $this->error(fmt("Invalid argument: %s=%s", 'mode', $mode));
485 // no more images in one row as defined by $numcols
486 if (($key + 1) % $numcols == 0 ||
487 ($key + 1) == count($photos) ||
491 $html->pushcontent(HTML::div($row));
493 $html->pushcontent(HTML::tr($row));
494 $row->setContent('');
499 $table_attributes = array("border" => 0,
502 "class" => "photoalbum",
503 "width" => $tablewidth ? $tablewidth : "100%");
505 if (!empty($tableheight))
506 $table_attributes = array_merge($table_attributes,
507 array("height" => $tableheight));
509 $html = HTML::table($table_attributes, $html);
511 return HTML::div(array("align" => $align), $html);
515 * Calculate the new size in pixels when the original size
516 * with a value is given.
518 * @param integer $oldSize Absolute no. of pixels
519 * @param mixed $value Either absolute no. or HTML percentage e.g. '50%'
520 * @return integer New size in pixels
522 function newSize($oldSize, $value)
524 if (trim(substr($value, strlen($value) - 1)) != "%") {
527 $value = str_replace("%", "", $value);
528 return round(($oldSize * $value) / 100);
532 * fromLocation - read only one picture from fixed album_location
533 * and return it in array $photos
535 * @param string $src Name of page
536 * @param array $photos
537 * @return string Error if fixed location is not allowed
539 function fromLocation($src, &$photos)
541 /*if (!allow_album_location) {
542 return $this->error(_("Fixed album location is not allowed. Please specify parameter src."));
545 if (!IsSafeURL($src)) {
546 return $this->error(_("Bad url in src: remove all of <, >, \""));
548 $photos[] = array("name" => $src, //album_location."/$src".album_default_extension,
553 * fromFile - read pictures & descriptions (separated by ;)
554 * from $src and return it in array $photos
556 * @param string $src path to dir or textfile (local or remote)
557 * @param array $photos
558 * @param string $webpath
559 * @return string Error when bad url or file couldn't be opened
561 function fromFile($src, &$photos, $webpath = '')
564 if (preg_match("/^Upload:(.*)$/", $src, $m)) {
565 $src = getUploadFilePath() . $m[1];
566 $webpath = getUploadDataPath() . $m[1];
568 //there has a big security hole... as loading config/config.ini !
569 if (!preg_match('/(\.csv|\.jpg|\.jpeg|\.png|\.gif|\/)$/', $src)) {
570 return $this->error(_("File extension for csv file has to be '.csv'"));
572 if (!IsSafeURL($src)) {
573 return $this->error(_("Bad url in src: remove all of <, >, \""));
575 if (preg_match('/^(http|ftp|https):\/\//i', $src)) {
576 $contents = url_get_contents($src);
580 if (string_ends_with($src, "/"))
581 $src = substr($src, 0, -1);
583 if (!file_exists($src) and @file_exists(PHPWIKI_DIR . "/$src")) {
584 $src = PHPWIKI_DIR . "/$src";
586 // check if src is a directory
587 if (file_exists($src) and filetype($src) == 'dir') {
590 foreach (array('jpeg', 'jpg', 'png', 'gif') as $ext) {
591 $fileset = new fileSet($src, "*.$ext");
592 $list = array_merge($list, $fileset->getFiles());
594 // convert dirname($src) (local fs path) to web path
597 // assume relative src. default: "themes/Hawaiian/images/pictures"
598 $webpath = DATA_PATH . '/' . $src_bak;
600 foreach ($list as $file) {
601 // convert local path to webpath
602 $photos[] = array("src" => $file,
603 "name" => $webpath . "/$file",
604 "name_tile" => $src . "/$file",
605 "src" => $src . "/$file",
610 // check if $src is an image
611 foreach (array('jpeg', 'jpg', 'png', 'gif') as $ext) {
612 if (preg_match("/\.$ext$/", $src)) {
613 if (!file_exists($src) and @file_exists(PHPWIKI_DIR . "/$src"))
614 $src = PHPWIKI_DIR . "/$src";
615 if ($web_location == 1 and !empty($contents)) {
616 $photos[] = array("src" => $src,
623 if (!file_exists($src))
624 return $this->error(fmt("Unable to find src=ā%sā", $src));
625 $photos[] = array("src" => $src,
626 "name" => "../" . $src,
633 if ($web_location == 0) {
634 $fp = @fopen($src, "r");
636 return $this->error(fmt("Unable to read src=ā%sā", $src));
638 while ($data = fgetcsv($fp, 1024, ';')) {
639 if (count($data) == 0 || empty($data[0])
640 || preg_match('/^#/', $data[0])
641 || preg_match('/^[[:space:]]*$/', $data[0])
644 if (empty($data[1])) $data[1] = '';
645 $photos[] = array("name" => dirname($src) . "/" . trim($data[0]),
646 "location" => "../" . dirname($src) . "/" . trim($data[0]),
647 "desc" => trim($data[1]),
648 "name_tile" => dirname($src) . "/" . trim($data[0]));
652 } elseif ($web_location == 1) {
653 //TODO: check if the file is an image
654 $contents = preg_split('/\n/', $contents);
655 while (list($key, $value) = each($contents)) {
656 $data = preg_split('/\;/', $value);
657 if (count($data) == 0 || empty($data[0])
658 || preg_match('/^#/', $data[0])
659 || preg_match('/^[[:space:]]*$/', $data[0])
662 if (empty($data[1])) $data[1] = '';
663 $photos[] = array("name" => dirname($src) . "/" . trim($data[0]),
664 "src" => dirname($src) . "/" . trim($data[0]),
665 "desc" => trim($data[1]),
666 "name_tile" => dirname($src) . "/" . trim($data[0]));
676 // c-hanging-comment-ender-p: nil
677 // indent-tabs-mode: nil