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
98 function getDescription()
100 return _("Display a set of photos listed in a text file with optional descriptions.");
103 // Avoid nameclash, so it's disabled. We allow any url.
104 // define('allow_album_location', true);
105 // define('album_location', 'http://kw.jouwfeestje.com/foto/redactie');
106 // define('album_default_extension', '.jpg');
107 // define('desc_separator', ';');
109 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 // "row" - inline thumbnails
119 // "column" - photos full-size, displayed in 1 column
120 // "slide" - slideshow mode, needs javascript on client
121 'numcols' => 3, // photos per row, columns
122 'showdesc' => 'both', // none|name|desc|both
123 // "none" - No descriptions next to photos
124 // "name" - Only filename shown
125 // "desc" - Only description (from textfile) shown
126 // "both" - If no description found, then filename will be used
127 'link' => true, // show link to original sized photo
128 // If true, each image will be hyperlinked to a page where the single
129 // photo will be shown full-size. Only works when mode != 'normal'
130 'attrib' => '', // 'sort, nowrap, alt'
131 // attrib arg allows multiple attributes: attrib=sort,nowrap,alt
132 // 'sort' sorts alphabetically, 'nowrap' for cells, 'alt' to use
133 // descs instead of filenames in image ALT-tags
134 'bgcolor' => '#eae8e8', // cell bgcolor (lightgrey)
135 'hlcolor' => '#c0c0ff', // highlight color (lightblue)
136 'align' => 'center', // alignment of table
137 'height' => 'auto', // image height (auto|75|100%)
138 'width' => 'auto', // image width (auto|75|100%)
139 // Size of shown photos. Either absolute value (e.g. "50") or
140 // HTML style percentage (e.g. "75%") or "auto" for no special
142 'cellwidth' => 'image', // cell (auto|equal|image|75|100%)
143 // Width of cells in table. Either absolute value in pixels, HTML
144 // style percentage, "auto" (no special action), "equal" (where
145 // all columns are equally sized) or "image" (take height and
146 // width of the photo in that cell).
147 'tablewidth' => false, // table (75|100%)
148 'p' => false, // "displaythissinglephoto.jpg"
149 'h' => false, // "highlightcolorofthisphoto.jpg"
150 'duration' => 6, // in slide mode, in seconds
151 'thumbswidth' => 80 //width of thumbnails
155 // descriptions (instead of filenames) for image alt-tags
157 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) {
187 if (in_array("sort", $attributes))
194 if ($mode == "column") {
199 // set some fixed properties for each $mode
200 if ($mode == 'thumbs' || $mode == 'tiles') {
201 $attributes = array_merge($attributes, "alt");
202 $attributes = array_merge($attributes, "nowrap");
203 $cellwidth = 'auto'; // else cell won't nowrap
204 if ($width == 'auto') $width = 70;
205 } elseif ($mode == 'list') {
208 if ($width == 'auto') $width = 50;
209 } elseif ($mode == 'slide') {
212 $numcols = count($photos);
214 while (list($key, $value) = each($photos)) {
215 list($x, $y, $s, $t) = @getimagesize($value['src']);
216 if ($height != 'auto') $y = $this->newSize($y, $height);
217 if ($width != 'auto') $y = round($y * $this->newSize($x, $width) / $x);
218 if ($x > $cell_width) $cell_width = $x;
219 if ($y > $tableheight) $tableheight = $y;
223 unset ($x, $y, $s, $t, $key, $value, $keep);
227 $duration = 1000 * $duration;
228 if ($mode == 'slide')
229 $row->pushContent(JavaScript("
231 function display_slides() {
233 cell0 = document.getElementsByName('wikislide' + j);
234 cell = document.getElementsByName('wikislide' + i);
235 if (cell0.item(0) != null)
236 cell0.item(0).style.display='none';
237 if (cell.item(0) != null)
238 cell.item(0).style.display='block';
240 if (cell.item(0) == null) i = 0;
241 setTimeout('display_slides()',$duration);
243 display_slides();"));
245 while (list($key, $value) = each($photos)) {
246 if ($p && basename($value["name"]) != "$p") {
249 if ($h && basename($value["name"]) == "$h") {
250 $color = $hlcolor ? $hlcolor : $bgcolor;
254 // $params will be used for each <img > tag
255 $params = array('src' => $value["name"],
256 'src_tile' => $value["name_tile"],
257 'alt' => ($value["desc"] != "" and in_array("alt", $attributes))
259 : basename($value["name"]));
260 if (!@empty($value['location']))
261 $params = array_merge($params, array("location" => $value['location']));
268 $value["desc"] = basename($value["name"]);
273 if (!$value["desc"]) $value["desc"] = basename($value["name"]);
277 // FIXME: get getimagesize to work with names with spaces in it.
278 // convert $value["name"] from webpath to local path
279 $size = @getimagesize($value["name"]); // try " " => "\\ "
280 if (!$size and !empty($value["src"])) {
281 $size = @getimagesize($value["src"]);
283 trigger_error("Unable to getimagesize(" . $value["name"] . ")",
287 $newwidth = $this->newSize($size[0], $width);
288 if ($width != 'auto' && $newwidth > 0) {
289 $params = array_merge($params, array("width" => $newwidth));
291 if (($mode == 'thumbs' || $mode == 'tiles' || $mode == 'list')) {
292 if (!empty($size[0])) {
293 $newheight = round($newwidth * $size[1] / $size[0]);
294 $params['width'] = $newwidth;
295 $params['height'] = $newheight;
296 } else $newheight = '';
297 if ($height == 'auto') $height = 150;
299 $newheight = $this->newSize($size[1], $height);
300 if ($height != 'auto' && $newheight > 0) {
301 $params = array_merge($params, array("height" => $newheight));
307 'class' => 'photoalbum cell align-center top',
308 'bgcolor' => "$color");
309 if ($cellwidth != 'auto') {
310 if ($cellwidth == 'equal') {
311 $newcellwidth = round(100 / $numcols) . "%";
312 } elseif ($cellwidth == 'image') {
313 $newcellwidth = $newwidth;
315 $newcellwidth = $cellwidth;
317 $cell = array_merge($cell, array("width" => $newcellwidth));
319 if (in_array("nowrap", $attributes)) {
320 $cell = array_merge($cell, array("nowrap" => "nowrap"));
322 //create url to display single larger version of image on page
323 $url = WikiURL($request->getPage(),
324 array("p" => basename($value["name"])))
326 . basename($value["name"]);
328 $b_url = WikiURL($request->getPage(),
329 array("h" => basename($value["name"])))
331 . basename($value["name"]);
333 ? HTML::a(array("href" => "$url"), basename($value["desc"]))
334 : basename($value["name"]);
336 if ($mode == 'normal' || $mode == 'slide') {
337 if (!@empty($params['location'])) $params['src'] = $params['location'];
338 unset ($params['location'], $params['src_tile']);
339 $url_image = $link ? HTML::a(array("id" => basename($value["name"]),
340 "href" => "$url"), HTML::img($params))
341 : HTML::img($params);
344 if (!@empty ($params['src_tile']))
345 $params['src'] = $params['src_tile'];
346 unset ($params['location'], $params['src_tile']);
347 $url_image = $link ? HTML::a(array("id" => basename($value["name"]),
349 ImageTile::image_tile($params))
350 : HTML::img($params);
355 if (!@empty($params['location'])) $params['src'] = $params['location'];
356 unset ($params['location'], $params['src_tile']);
357 $url_image = $link ? HTML::a(array("id" => basename($value["name"]),
358 "href" => "$b_url"), HTML::img($params))
359 : HTML::img($params);
362 $url_text = HTML::a(array("id" => basename($value["name"])),
364 // here we use different modes
365 if ($mode == 'tiles') {
368 HTML::div(array('class' => 'top'), $url_image),
369 HTML::div(array('class' => 'bottom'),
370 HTML::div(array('class' => 'boldsmall'),
373 HTML::div(array('class' => 'gensmall'),
379 } elseif ($mode == 'list') {
380 $desc = ($showdesc != 'none') ? $value["desc"] : '';
382 HTML::td(array("class" => "top",
384 "bgcolor" => $color),
385 HTML::div(array('class' => 'boldsmall'), ($url_text))));
387 HTML::td(array("class" => "top",
389 "bgcolor" => $color),
390 HTML::div(array('class' => 'gensmall'),
398 HTML::td(array("class" => "top",
400 "bgcolor" => $color),
401 HTML::div(array('class' => 'gensmall'), $desc)));
403 } elseif ($mode == 'thumbs') {
404 $desc = ($showdesc != 'none') ?
405 HTML::p(HTML::a(array("href" => "$url"),
410 // FIXME: no HtmlElement for fontsizes?
411 // rurban: use ->setAttr("style","font-size:small;")
412 // but better use a css class
413 HTML::div(array('class' => 'gensmall'), $desc)
415 } elseif ($mode == 'normal') {
416 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
420 // FIXME: no HtmlElement for fontsizes?
421 HTML::div(array('class' => 'gensmall'), $desc)
423 } elseif ($mode == 'slide') {
424 if ($newwidth == 'auto' || !$newwidth)
425 $newwidth = $this->newSize($size[0], $width);
426 if ($newwidth == 'auto' || !$newwidth)
427 $newwidth = $size[0];
428 if ($newheight != 'auto') $newwidth = round($size[0] * $newheight / $size[1]);
429 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
431 $cell = array('style' => 'display: block; '
432 . 'position: absolute; '
434 . 'margin-left: -' . round($newwidth / 2) . 'px;'
435 . 'text-align: center; '
436 . 'vertical-align: top',
437 'name' => "wikislide" . $count);
439 $cell = array('style' => 'display: none; '
440 . 'position: absolute ;'
442 . 'margin-left: -' . round($newwidth / 2) . 'px;'
443 . 'text-align: center; '
444 . 'vertical-align: top',
445 'name' => "wikislide" . $count);
446 if ($align == 'left' || $align == 'right') {
448 $cell = array('style' => 'display: block; '
449 . 'position: absolute; '
450 . $align . ': 50px; '
451 . 'vertical-align: top',
452 'name' => "wikislide" . $count);
454 $cell = array('style' => 'display: none; '
455 . 'position: absolute; '
456 . $align . ': 50px; '
457 . 'vertical-align: top',
458 'name' => "wikislide" . $count);
463 HTML::div(array('class' => 'gensmall'), $desc)
466 } elseif ($mode == 'row') {
467 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
469 HTML::table(array("style" => "display: inline",
470 'class' > "photoalbum row"),
471 HTML::tr(HTML::td($url_image)),
472 HTML::tr(HTML::td(array("class" => "gensmall",
473 "style" => "text-align: center; "
474 . "background-color: $color"),
478 return $this->error(fmt("Invalid argument: %s=%s", 'mode', $mode));
481 // no more images in one row as defined by $numcols
482 if (($key + 1) % $numcols == 0 ||
483 ($key + 1) == count($photos) ||
487 $html->pushcontent(HTML::div($row));
489 $html->pushcontent(HTML::tr($row));
490 $row->setContent('');
495 $table_attributes = array(
496 "class" => "photoalbum",
497 "width" => $tablewidth ? $tablewidth : "100%");
499 if (!empty($tableheight))
500 $table_attributes = array_merge($table_attributes,
501 array("height" => $tableheight));
503 $html = HTML::table($table_attributes, $html);
505 return HTML::div(array("align" => $align), $html);
509 * Calculate the new size in pixels when the original size
510 * with a value is given.
512 * @param integer $oldSize Absolute no. of pixels
513 * @param mixed $value Either absolute no. or HTML percentage e.g. '50%'
514 * @return integer New size in pixels
516 function newSize($oldSize, $value)
518 if (trim(substr($value, strlen($value) - 1)) != "%") {
521 $value = str_replace("%", "", $value);
522 return round(($oldSize * $value) / 100);
526 * fromLocation - read only one picture from fixed album_location
527 * and return it in array $photos
529 * @param string $src Name of page
530 * @param array $photos
531 * @return string Error if fixed location is not allowed
533 function fromLocation($src, &$photos)
535 /*if (!allow_album_location) {
536 return $this->error(_("Fixed album location is not allowed. Please specify parameter src."));
539 if (!IsSafeURL($src)) {
540 return $this->error(_("Bad url in src: remove all of <, >, \""));
542 $photos[] = array("name" => $src, //album_location."/$src".album_default_extension,
548 * fromFile - read pictures & descriptions (separated by ;)
549 * from $src and return it in array $photos
551 * @param string $src path to dir or textfile (local or remote)
552 * @param array $photos
553 * @param string $webpath
554 * @return string Error when bad url or file couldn't be opened
556 function fromFile($src, &$photos, $webpath = '')
559 if (preg_match("/^Upload:(.*)$/", $src, $m)) {
560 $src = getUploadFilePath() . $m[1];
561 $webpath = getUploadDataPath() . $m[1];
563 //there has a big security hole... as loading config/config.ini !
564 if (!preg_match('/(\.csv|\.jpg|\.jpeg|\.png|\.gif|\/)$/', $src)) {
565 return $this->error(_("File extension for csv file has to be '.csv'"));
567 if (!IsSafeURL($src)) {
568 return $this->error(_("Bad url in src: remove all of <, >, \""));
570 if (preg_match('/^(http|ftp|https):\/\//i', $src)) {
571 $contents = url_get_contents($src);
575 if (string_ends_with($src, "/"))
576 $src = substr($src, 0, -1);
578 if (!file_exists($src) and @file_exists(PHPWIKI_DIR . "/$src")) {
579 $src = PHPWIKI_DIR . "/$src";
581 // check if src is a directory
582 if (file_exists($src) and filetype($src) == 'dir') {
585 foreach (array('jpeg', 'jpg', 'png', 'gif') as $ext) {
586 $fileset = new fileSet($src, "*.$ext");
587 $list = array_merge($list, $fileset->getFiles());
589 // convert dirname($src) (local fs path) to web path
592 // assume relative src. default: "themes/Hawaiian/images/pictures"
593 $webpath = DATA_PATH . '/' . $src_bak;
595 foreach ($list as $file) {
596 // convert local path to webpath
597 $photos[] = array("src" => $file,
598 "name" => $webpath . "/$file",
599 "name_tile" => $src . "/$file",
600 "src" => $src . "/$file",
605 // check if $src is an image
606 foreach (array('jpeg', 'jpg', 'png', 'gif') as $ext) {
607 if (preg_match("/\.$ext$/", $src)) {
608 if (!file_exists($src) and @file_exists(PHPWIKI_DIR . "/$src"))
609 $src = PHPWIKI_DIR . "/$src";
610 if ($web_location == 1 and !empty($contents)) {
611 $photos[] = array("src" => $src,
618 if (!file_exists($src))
619 return $this->error(fmt("Unable to find src=ā%sā", $src));
620 $photos[] = array("src" => $src,
621 "name" => "../" . $src,
628 if ($web_location == 0) {
629 $fp = @fopen($src, "r");
631 return $this->error(fmt("Unable to read src=ā%sā", $src));
633 while ($data = fgetcsv($fp, 1024, ';')) {
634 if (count($data) == 0 || empty($data[0])
635 || preg_match('/^#/', $data[0])
636 || preg_match('/^[[:space:]]*$/', $data[0])
639 if (empty($data[1])) $data[1] = '';
640 $photos[] = array("name" => dirname($src) . "/" . trim($data[0]),
641 "location" => "../" . dirname($src) . "/" . trim($data[0]),
642 "desc" => trim($data[1]),
643 "name_tile" => dirname($src) . "/" . trim($data[0]));
647 } elseif ($web_location == 1) {
648 //TODO: check if the file is an image
649 $contents = preg_split('/\n/', $contents);
650 while (list($key, $value) = each($contents)) {
651 $data = preg_split('/\;/', $value);
652 if (count($data) == 0 || empty($data[0])
653 || preg_match('/^#/', $data[0])
654 || preg_match('/^[[:space:]]*$/', $data[0])
657 if (empty($data[1])) $data[1] = '';
658 $photos[] = array("name" => dirname($src) . "/" . trim($data[0]),
659 "src" => dirname($src) . "/" . trim($data[0]),
660 "desc" => trim($data[1]),
661 "name_tile" => dirname($src) . "/" . trim($data[0]));
672 // c-hanging-comment-ender-p: nil
673 // indent-tabs-mode: nil