ShowPhoto.php - Simple Slid Show Script

This section provides a tutorial example on how to display image files in a directory in a slide show format. The getimagesize() function is used to retrieve image sizes from image files.

Another nice function for image file management is the "get image size" function, getimagesize($path), which returns the size and other information about the image stored in a file located at $path.

To show you how to use this getimagesize() function, I wrote the following sample script:

<?php # PhotoShow.php
#- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
# 
#- Get the photo file name
   $i = intval($_GET['i']);
   $c = 0;
   $p = "";
   if ($d = opendir(".")) {
      while (($f = readdir($d)) != false) {
      	 if (preg_match('/.jpg$/',$f)) {
            if ($i==$c) {
               $p = $f;
            }
            $c++;
         }
      }
      closedir($d);
    }

#- Build the page
   $h = fopen("header.html", "r");
   $s = fread($h, filesize("header.html"));
   fclose($h);
   print "$s\n";

   if (strlen($p)>0) {
      $i1 = ($i+$c-1) % $c;
      $i2 = ($i+$c+1) % $c;
      list($width, $height, $type, $attr) = getimagesize($p);
      if ($width>700) {
         $width = $width/2;
         $height = $height/2;
      }
      print "<center><a href=\"ShowPhoto.php?i=$i1\"><=</a>&nbsp;"
         ."<a href=\"ShowPhoto.php?i=$i2\">=></a></center>\n"
         ."<center><img src=\"$p\" border=\"1\" width=\"$width\" "
         ."height=\"$height\"></center><br\>\n"
         ."<center><a href=\"ShowPhoto.php?i=$i1\"><=</a>&nbsp;"
         ."<a href=\"ShowPhoto.php?i=$i2\">=></a></center>\n";
   } else {
      print "<p>Wrong URL. Please go back to the home page.</p>\n";
   }
   
   $h = fopen("footer.html", "r");
   $s = fread($h, filesize("footer.html"));
   fclose($h);
   print "$s\n";
?>

This script will display image files stored in a directory one image at a time like a slid show.

Try it out. It's very easy to use. You can easily customize it to meet your requirements.

Last update: 2015.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.0

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Configuring and Sending out Emails

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 MySQL Server Connection and Access Functions

Functions to Manage Directories, Files and Images

 opendir() and Directory Management Functions

 file_exists() and File Testing Functions

 FileExistsTest.php - File Testing Examples

 fopen() and File Input/Output Functions

 File_Input_Output_Test.php - File Input/Output Examples

 readfile() and Special File Handling Functions

 imagecreatetruecolor() and GD Imaging Library Functions

ShowPhoto.php - Simple Slid Show Script

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Outdated Tutorials

 References

 PDF Printing Version