PHP Tutorials - Herong's Tutorial Examples - v5.18, by Herong Yang
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 2009-2015 (c) 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> " ."<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> " ."<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 slideshow.
Try it out. It's very easy to use. You can easily customize it to meet your requirements.
Table of Contents
Introduction and Installation of PHP
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
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
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
►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
►ShowPhoto.php - Simple Slid Show Script
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
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS