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
20 * along with PhpWiki; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 seperated 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 (/*...*/) {
76 $el = new HTML ('img');
77 $tag = func_get_args();
78 $path = DATA_PATH . "/ImageTile.php";
79 $params = "<img src=\"$path?url=". $tag[0]['src'];
80 if (!@empty($tag[0]['width']))
81 $params .= "&width=" . $tag[0]['width'];
82 if (!@empty($tag[0]['height']))
83 $params .= "&height=" . $tag[0]['height'];
84 if (!@empty($tag[0]['width']))
85 $params .= '" width="' . $tag[0]['width'];
86 if (!@empty($tag[0]['height']))
87 $params .= '" height="' . $tag[0]['height'];
89 $params .= '" alt="' . $tag[0]['alt'] . '" />';
90 return $el->raw ($params);
94 class WikiPlugin_PhotoAlbum
98 return _("PhotoAlbum");
101 function getDescription () {
102 return _("Displays a set of photos listed in a text file with optional descriptions");
105 function getVersion() {
106 return preg_replace("/[Revision: $]/", '',
110 // Avoid nameclash, so it's disabled. We allow any url.
111 // define('allow_album_location', true);
112 // define('album_location', 'http://kw.jouwfeestje.com/foto/redactie');
113 // define('album_default_extension', '.jpg');
114 // define('desc_separator', ';');
116 function getDefaultArguments() {
117 return array('src' => '', // textfile of image list, or local dir.
118 'url' => '', // if src=localfs, url prefix (webroot for the links)
119 'mode' => 'normal', // normal|thumbs|tiles|list
120 // "normal" - Normal table which shows photos full-size
121 // "thumbs" - WinXP thumbnail style
122 // "tiles" - WinXP tiles style
123 // "list" - WinXP list style
124 // "row" - inline thumbnails
125 // "column" - photos full-size, displayed in 1 column
126 // "slide" - slideshow mode, needs javascript on client
127 'numcols' => 3, // photos per row, columns
128 'showdesc' => 'both', // none|name|desc|both
129 // "none" - No descriptions next to photos
130 // "name" - Only filename shown
131 // "desc" - Only description (from textfile) shown
132 // "both" - If no description found, then filename will be used
133 'link' => true, // show link to original sized photo
134 // If true, each image will be hyperlinked to a page where the single
135 // photo will be shown full-size. Only works when mode != 'normal'
136 'attrib' => '', // 'sort, nowrap, alt'
137 // attrib arg allows multiple attributes: attrib=sort,nowrap,alt
138 // 'sort' sorts alphabetically, 'nowrap' for cells, 'alt' to use
139 // descs instead of filenames in image ALT-tags
140 'bgcolor' => '#eae8e8', // cell bgcolor (lightgrey)
141 'hlcolor' => '#c0c0ff', // highlight color (lightblue)
142 'align' => 'center', // alignment of table
143 'height' => 'auto', // image height (auto|75|100%)
144 'width' => 'auto', // image width (auto|75|100%)
145 // Size of shown photos. Either absolute value (e.g. "50") or
146 // HTML style percentage (e.g. "75%") or "auto" for no special
148 'cellwidth'=> 'image', // cell (auto|equal|image|75|100%)
149 // Width of cells in table. Either absolute value in pixels, HTML
150 // style percentage, "auto" (no special action), "equal" (where
151 // all columns are equally sized) or "image" (take height and
152 // width of the photo in that cell).
153 'tablewidth'=> false, // table (75|100%)
154 'p' => false, // "displaythissinglephoto.jpg"
155 'h' => false, // "highlightcolorofthisphoto.jpg"
156 'duration' => 6, // in slide mode, in seconds
157 'thumbswidth' => 80 //width of thumbnails
160 // descriptions (instead of filenames) for image alt-tags
162 function run($dbi, $argstr, &$request, $basepage) {
164 extract($this->getArgs($argstr, $request));
166 $attributes = $attrib ? explode(",", $attrib) : array();
170 // check all parameters
171 // what type do we have?
174 $src = $request->getArg('pagename');
175 $error = $this->fromLocation($src, $photos);
177 $error = $this->fromFile($src, $photos, $url);
180 return $this->error($error);
183 if ($numcols < 1) $numcols = 1;
184 if ($align != 'left' && $align != 'center' && $align != 'right') {
187 if (count($photos) == 0) return;
189 if (in_array("sort", $attributes))
196 if ($mode == "column") {
201 // set some fixed properties for each $mode
202 if ($mode == 'thumbs' || $mode == 'tiles') {
203 $attributes = array_merge($attributes, "alt");
204 $attributes = array_merge($attributes, "nowrap");
205 $cellwidth = 'auto'; // else cell won't nowrap
206 if ($width == 'auto') $width = 70;
207 } elseif ($mode == 'list') {
210 if ($width == 'auto') $width = 50;
211 } elseif ($mode == 'slide' ) {
214 $numcols = count($photos);
216 while (list($key, $value) = each($photos)) {
217 list($x,$y,$s,$t) = @getimagesize($value['src']);
218 if ($height != 'auto') $y = $this->newSize($y, $height);
219 if ($width != 'auto') $y = round($y * $this->newSize($x, $width) / $x);
220 if ($x > $cell_width) $cell_width = $x;
221 if ($y > $tableheight) $tableheight = $y;
225 unset ($x,$y,$s,$t,$key,$value,$keep);
229 $duration = 1000 * $duration;
230 if ($mode == 'slide')
231 $row->pushContent(JavaScript("
233 function display_slides() {
235 cell0 = document.getElementsByName('wikislide' + j);
236 cell = document.getElementsByName('wikislide' + i);
237 if (cell0.item(0) != null)
238 cell0.item(0).style.display='none';
239 if (cell.item(0) != null)
240 cell.item(0).style.display='block';
242 if (cell.item(0) == null) i = 0;
243 setTimeout('display_slides()',$duration);
245 display_slides();"));
247 while (list($key, $value) = each($photos)) {
248 if ($p && basename($value["name"]) != "$p") {
251 if ($h && basename($value["name"]) == "$h") {
252 $color = $hlcolor ? $hlcolor : $bgcolor;
256 // $params will be used for each <img > tag
257 $params = array('src' => $value["name"],
258 '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;
303 $newheight = $this->newSize($size[1], $height);
304 if ($height != 'auto' && $newheight > 0) {
305 $params = array_merge($params, array("height" => $newheight));
310 $cell = array('align' => "center",
312 'class' => 'photoalbum cell',
313 'bgcolor' => "$color");
314 if ($cellwidth != 'auto') {
315 if ($cellwidth == 'equal') {
316 $newcellwidth = round(100/$numcols)."%";
317 } else if ($cellwidth == 'image') {
318 $newcellwidth = $newwidth;
320 $newcellwidth = $cellwidth;
322 $cell = array_merge($cell, array("width" => $newcellwidth));
324 if (in_array("nowrap", $attributes)) {
325 $cell = array_merge($cell, array("nowrap" => "nowrap"));
327 //create url to display single larger version of image on page
328 $url = WikiURL($request->getPage(),
329 array("p" => basename($value["name"])))
331 . basename($value["name"]);
333 $b_url = WikiURL($request->getPage(),
334 array("h" => basename($value["name"])))
336 . basename($value["name"]);
338 ? HTML::a(array("href" => "$url"), basename($value["desc"]))
339 : basename($value["name"]);
341 if ($mode == 'normal' || $mode == 'slide') {
342 if(!@empty($params['location'])) $params['src'] = $params['location'];
343 unset ($params['location'],$params['src_tile']);
344 $url_image = $link ? HTML::a(array("id" => basename($value["name"]),
345 "href" => "$url"), HTML::img($params))
346 : HTML::img($params);
349 if (!@empty ($params['src_tile']))
350 $params['src'] = $params['src_tile'] ;
351 unset ($params['location'],$params['src_tile']);
352 $url_image = $link ? HTML::a(array("id" => basename($value["name"]),
354 ImageTile::image_tile($params))
355 : HTML::img($params);
360 if(!@empty($params['location'])) $params['src'] = $params['location'];
361 unset ($params['location'],$params['src_tile']);
362 $url_image = $link ? HTML::a(array("id" => basename($value["name"]),
363 "href" => "$b_url"), HTML::img($params))
364 : HTML::img($params);
367 $url_text = HTML::a(array("id" => basename($value["name"])),
369 // here we use different modes
370 if ($mode == 'tiles') {
373 HTML::div(array('valign' => 'top'), $url_image),
374 HTML::div(array('valign' => 'bottom'),
375 HTML::div(array('class'=>'boldsmall'),
378 HTML::div(array('class'=>'gensmall'),
384 } elseif ($mode == 'list') {
385 $desc = ($showdesc != 'none') ? $value["desc"] : '';
387 HTML::td(array("valign" => "top",
389 "bgcolor" => $color),
390 HTML::div(array('class'=>'boldsmall'),($url_text))));
392 HTML::td(array("valign" => "top",
394 "bgcolor" => $color),
395 HTML::div(array('class'=>'gensmall'),
403 HTML::td(array("valign" => "top",
405 "bgcolor" => $color),
406 HTML::div(array('class'=>'gensmall'),$desc)));
408 } elseif ($mode == 'thumbs') {
409 $desc = ($showdesc != 'none') ?
410 HTML::p(HTML::a(array("href" => "$url"),
415 // FIXME: no HtmlElement for fontsizes?
416 // rurban: use ->setAttr("style","font-size:small;")
417 // but better use a css class
418 HTML::div(array('class'=>'gensmall'),$desc)
420 } elseif ($mode == 'normal') {
421 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
425 // FIXME: no HtmlElement for fontsizes?
426 HTML::div(array('class'=>'gensmall'),$desc)
428 } elseif ($mode == 'slide') {
429 if ($newwidth == 'auto' || !$newwidth)
430 $newwidth = $this->newSize($size[0],$width);
431 if ($newwidth == 'auto' || !$newwidth)
432 $newwidth = $size[0];
433 if ($newheight != 'auto') $newwidth = round($size[0] * $newheight / $size[1]);
434 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
436 $cell=array('style' => 'display: block; '
437 . 'position: absolute; '
439 . 'margin-left: -'.round($newwidth / 2).'px;'
440 . 'text-align: center; '
441 . 'vertical-align: top',
442 'name' => "wikislide".$count);
444 $cell=array('style' => 'display: none; '
445 . 'position: absolute ;'
447 . 'margin-left: -'.round($newwidth / 2).'px;'
448 . 'text-align: center; '
449 . 'vertical-align: top',
450 'name' => "wikislide".$count);
451 if ($align == 'left' || $align == 'right') {
453 $cell=array('style' => 'display: block; '
454 .'position: absolute; '
456 .'vertical-align: top',
457 'name' => "wikislide".$count);
459 $cell=array('style' => 'display: none; '
460 .'position: absolute; '
462 .'vertical-align: top',
463 'name' => "wikislide".$count);
468 HTML::div(array('class'=>'gensmall'), $desc)
471 } elseif ($mode == 'row') {
472 $desc = ($showdesc != 'none') ? HTML::p($value["desc"]) : '';
474 HTML::table(array("style" => "display: inline",
475 'class' > "photoalbum row"),
476 HTML::tr(HTML::td($url_image)),
477 HTML::tr(HTML::td(array("class" => "gensmall",
478 "style" => "text-align: center; "
479 ."background-color: $color"),
483 return $this->error(fmt("Invalid argument: %s=%s", 'mode', $mode));
486 // no more images in one row as defined by $numcols
487 if ( ($key + 1) % $numcols == 0 ||
488 ($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) {
523 if (trim(substr($value,strlen($value)-1)) != "%") {
526 $value = str_replace("%", "", $value);
527 return round(($oldSize*$value)/100);
531 * fromLocation - read only one picture from fixed album_location
532 * and return it in array $photos
534 * @param string $src Name of page
535 * @param array $photos
536 * @return string Error if fixed location is not allowed
538 function fromLocation($src, &$photos) {
539 /*if (!allow_album_location) {
540 return $this->error(_("Fixed album location is not allowed. Please specify parameter src."));
543 if (! IsSafeURL($src)) {
544 return $this->error(_("Bad url in src: remove all of <, >, \""));
546 $photos[] = array ("name" => $src, //album_location."/$src".album_default_extension,
551 * fromFile - read pictures & descriptions (separated by ;)
552 * from $src and return it in array $photos
554 * @param string $src path to dir or textfile (local or remote)
555 * @param array $photos
556 * @return string Error when bad url or file couldn't be opened
558 function fromFile($src, &$photos, $webpath='') {
560 if (preg_match("/^Upload:(.*)$/", $src, $m)) {
561 $src = getUploadFilePath() . $m[1];
562 $webpath = getUploadDataPath() . $m[1];
564 //there has a big security hole... as loading config/config.ini !
565 if (!preg_match('/(\.csv|\.jpg|\.jpeg|\.png|\.gif|\/)$/',$src)) {
566 return $this->error(_("File extension for csv file has to be '.csv'"));
568 if (! IsSafeURL($src)) {
569 return $this->error(_("Bad url in src: remove all of <, >, \""));
571 if (preg_match('/^(http|ftp|https):\/\//i', $src)) {
572 $contents = url_get_contents($src);
576 if (string_ends_with($src,"/"))
577 $src = substr($src,0,-1);
579 if (!file_exists($src) and @file_exists(PHPWIKI_DIR . "/$src")) {
580 $src = PHPWIKI_DIR . "/$src";
582 // check if src is a directory
583 if (file_exists($src) and filetype($src) == 'dir') {
586 foreach (array('jpeg','jpg','png','gif') as $ext) {
587 $fileset = new fileSet($src, "*.$ext");
588 $list = array_merge($list, $fileset->getFiles());
590 // convert dirname($src) (local fs path) to web path
593 // assume relative src. default: "themes/Hawaiian/images/pictures"
594 $webpath = DATA_PATH . '/' . $src_bak;
596 foreach ($list as $file) {
597 // convert local path to webpath
598 $photos[] = array ("src" => $file,
599 "name" => $webpath . "/$file",
600 "name_tile" => $src . "/$file",
601 "src" => $src . "/$file",
606 // check if $src is an image
607 foreach (array('jpeg','jpg','png','gif') as $ext) {
608 if (preg_match("/\.$ext$/", $src)) {
609 if (!file_exists($src) and @file_exists(PHPWIKI_DIR . "/$src"))
610 $src = PHPWIKI_DIR . "/$src";
611 if ($web_location == 1 and !empty($contents)) {
612 $photos[] = array ("src" => $src,
619 if (!file_exists($src))
620 return $this->error(fmt("Unable to find src='%s'", $src));
621 $photos[] = array ("src" => $src,
622 "name" => "../".$src,
629 if ($web_location == 0) {
630 $fp = @fopen($src, "r");
632 return $this->error(fmt("Unable to read src='%s'", $src));
634 while ($data = fgetcsv($fp, 1024, ';')) {
635 if (count($data) == 0 || empty($data[0])
636 || preg_match('/^#/',$data[0])
637 || 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]))
656 if (empty($data[1])) $data[1] = '';
657 $photos[] = array ("name" => dirname($src)."/".trim($data[0]),
658 "src" => dirname($src)."/".trim($data[0]),
659 "desc" => trim($data[1]),
660 "name_tile" => dirname($src)."/".trim($data[0]));
671 // c-hanging-comment-ender-p: nil
672 // indent-tabs-mode: nil