なんか役に立つかと思って現実逃避に書いた、画像スライドショー用のPHPスクリプト。

画像の表示順は、ファイル名の昇順。日付なんかでファイル名を管理することが多いし。

デフォルトの表示イメージは、一番最近のもの、つまり右端に現れるやつ。

ローカル上の画像格納ディレクトリ(IMAGEDIR)と、URL上の画像表示URLの基礎部(IMAGEURL)を設定しておけば最低限動くんじゃないかな。

IFRAMEとかを使って、親ページに埋め込んで動かすようにするほうがいいんじゃないかと思う。

あと、サムネイルは単に元画像をサイズ表示してるだけなので、大きすぎる画像だと重くなってしまう。サムネイル画像だけ別に用意できるバージョンがあってもいいけど、その分サムネイルは各自作らなくちゃいけなくなるんだから、まあそんながんばらなくても。

DEMO

slideshow.php
<?php
/*
  simple slideshow script ver. 0.3
                   Jan. 2010
                   Yamamoto. T
 
  require:
    php5 or higher
  usage:
    (1) configure values in [configuration] section.
    (2) put this script anywhere accessible from the web.
 
    in default configuration, you may put this script and images like that:
 
      [any directory] -+- slideshow.php
                       +- image/
                            +- 20100101.jpg
                            +- 20100102.jpg
                            +- 20100102b.jpg
                               ...
 
    (3) edit HTML template section if you feel like.
 
    * order of image is determined according to its filename.
 
    displaying this script on the top of a web browser is OK, but
    embedding this script in other pages using <IFRAME> tag is also fine.
    In latter case you can separately design the main pages.
*/
 
#
# configuration
#
define(IMAGEDIR, './image/');   # local directory that contains image files.
                                # make sure it ends with '/'.
                                # e.g.   /home/someone/www/slideshow/image/
                                # relative path might be OK
define(IMAGEURL, './image/');   # base url to access image files
                                # make sure it ends with '/'.
                                # e.g.  http://yourhost/slideshow/image/
                                # relative URL might be OK
define(THUMB_W, 64);            # thumbnail width
define(THUMB_H, 64);            # thumbnail height
define(SPAN_WITH_THUMB, 3);     # navigation size
define(SPAN_WITHOUT_THUMB, 5);  # navigation size(no thumbnail)
# misc messages
define(M_PREV, 'Prev');
define(M_NEXT, 'Next');
define(M_THUMB_ON, 'Show thumbnails');
define(M_THUMB_OFF, 'Hide thumbnails');
# no need to modify
define(BASEURL, $_SERVER['SCRIPT_NAME']);   # this script

/* get web parameters */
 
$current = $_GET['page']+0;
$thumb_switch = $_GET['thumb'] || "0";
 
/* get file list */
 
$files = Array();
foreach (scandir(IMAGEDIR, 0) as $i) {
  if (!is_dir(IMAGEDIR . $i)) {
    array_push($files, $i);
  }
}
$total = count($files);
if ($current == 0) {
  $current = $total;  # set latest file for derfault view
}
 
/* [subroutine] make a link URL */
 
function a_linkurl($target, $cap, $thumb) {
  return "<a href=\"" . BASEURL . "?page=" . ($target) . "&thumb=$thumb\">$cap</a>";
}
 
/* [subroutine] make navigation struct */
 
function &get_nav($current, $total, $width) {
  global $thumb_switch;
  $w_begin = max(1, min($current - $width, $total - $width*2));
  $w_end = min($w_begin + $width*2, $total);
  $nav = Array();
  for ($i=$w_begin; $i<=$w_end; $i++) { $nav[] = $i; }
  if ($thumb_switch == "0") {
    if     ($w_begin == 2) { array_unshift($nav, 1); }
    elseif ($w_begin > 1)  { array_unshift($nav, 1, "..."); }
    if     ($w_end == $total - 1) { array_push($nav, $total); }
    elseif ($w_end < $total)      { array_push($nav, "...", $total); }
  }
  return $nav;
}
 
/* show prev/next links */
 
function show_prev_next_links() {
  global $current, $thumb_switch, $total;
  if ($current > 1) { echo a_linkurl($current-1, M_PREV, $thumb_switch); }
  echo " | ";
  if ($current < $total) { echo a_linkurl($current+1, M_NEXT, $thumb_switch); }
}
 
/* show main content */
 
function show_main_content() {
  global $current, $files;
  #echo "[page $current]" . $files[$current-1];
  echo "<img src=\"" . IMAGEURL . $files[$current-1] . "\">";
}
 
 
/* show thumbnail switch */
 
function show_thumbnail_switch() {
  global $thumb_switch, $current;
  if ($thumb_switch == "0") {
    echo a_linkurl($current, M_THUMB_ON, 1);
  } else {
    echo a_linkurl($current, M_THUMB_OFF, 0);
  }
}
 
/* show navigation (supports thumbnailed or not) */
 
function show_navigation() {
  global $thumb_switch, $current, $total, $files;
  $nav_width = ($thumb_switch == "0" ? SPAN_WITHOUT_THUMB : SPAN_WITH_THUMB);
  $nav = get_nav($current, $total, $nav_width);
  foreach ($nav as $n) {
    if ($thumb_switch == "0") {
      $s = "$n";
    } else {
      $s = "<img src=\"" . IMAGEURL . $files[$n-1] . "\" alt=\"" . $n .
           "\" border=\"0\" width=\"" . THUMB_W . "\" height=\"" . THUMB_H . "\">";
    }
    if ($n == $current) { echo "$s "; }
    elseif ($n == "...") { echo " ... "; }
    else { echo a_linkurl($n, $s, $thumb_switch) . "\n "; }
  }
}
 
?>
<?php /* edit HTML template to customize appearance. keep php snippets unedited. */ ?>
<html>
<head>
<title>slideshow</title>
</head>
<body>
<center>
<p><?php show_prev_next_links() ?></p>
<p><?php show_main_content() ?></p>
<p><?php show_thumbnail_switch() ?></p>
<p><?php show_navigation() ?></p>
</center>
</body>
</html>
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki