PHP implements automatic scrolling of images

Source: Internet
Author: User
This article mainly introduces PHP's method of automatically displaying images by scrolling. it involves php's techniques for operating Image effects and has some reference value, for more information about how to use PHP to automatically display images, see the following example. Share it with you for your reference. The details are as follows:

Specify an image directory. The program automatically displays each image on the page. usage:

1. create an image folder for the slide.
2. delete the slides in the image folder.
3. encode the following code and paste it into a text file named "index. php"
4. Upload files to a directory
5. change the settings for 6th and 8 actions.
6. run (use the URL set in step 1)

The code is as follows:

The code is as follows:

<?
/*
PHP image slideshow-auto version-PHP5
*/
// Set the absolute path to the directory containing the images
Define ('imgdir', '/home/devel/public_html/domain.com/public/images/slideshow /');
// Same but for www
Define ('webimgdir', '/images/slideshow /');
// Set session name for slideshow "cookie"
Define ('SS _ sessname', 'slideshow _ sess ');
// Global error variable
$ Err = '';
// Start img session
Session_name (SS_SESSNAME );
Session_start ();
// Init slideshow class
$ Ss = new slideshow ($ err );
If ($ err = $ ss-> init ())! = '')
{
Header ('http/1.1 500 Internal Server Error ');
Echo $ err;
Exit ();
}
// Get image files from directory
$ Ss-> get_images ();
// Set variables, done.
List ($ curr, $ caption, $ first, $ prev, $ next, $ last) = $ ss-> run ();
/*
Slideshow class, can be used stand-alone
*/
Class slideshow
{
Private $ files_arr = NULL;
Private $ err = NULL;
Public function _ construct (& $ err)
{
$ This-> files_arr = array ();
$ This-> err = $ err;
}
Public function init ()
{
// Run actions only if img array session var is empty
// Check if image directory exists
If (! $ This-> dir_exists ())
{
Return 'error retrieving images, missing directory ';
}
Return '';
}
Public function get_images ()
{
// Run actions only if img array session var is empty
If (isset ($ _ SESSION ['imgarr'])
{
$ This-> files_arr =$ _ SESSION ['imgarr'];
}
Else
{
If ($ dh = opendir (IMGDIR ))
{
While (false! ==( $ File = readdir ($ dh )))
{
If (preg_match ('/^. * \. (jpg | jpeg | gif | png) $/I', $ file ))
{
$ This-> files_arr [] = $ file;
}
}
Closedir ($ dh );
}
$ _ SESSION ['imgarr'] = $ this-> files_arr;
}
}
Public function run ()
{
$ Curr = 1;
$ Last = count ($ this-> files_arr );
If (isset ($ _ GET ['IMG '])
{
If (preg_match ('/^ [0-9] + $/', $ _ GET ['IMG ']) $ curr = (int) $ _ GET ['IMG '];
If ($ curr <= 0 | $ curr> $ last) $ curr = 1;
}
If ($ curr <= 1)
{
$ Prev = $ curr;
$ Next = $ curr + 1;
}
Else if ($ curr >=$ last)
{
$ Prev = $ last-1;
$ Next = $ last;
}
Else
{
$ Prev = $ curr-1;
$ Next = $ curr + 1;
}
// Line below sets the caption name...
$ Caption = str_replace ('-', '', $ this-> files_arr [$ curr-1]);
$ Caption = str_replace ('_', '', $ caption );
$ Caption = preg_replace ('/\. (jpe? G | gif | png) $/I ', '', $ caption );
$ Caption = ucfirst ($ caption );
Return array ($ this-> files_arr [$ curr-1], $ caption, 1, $ prev, $ next, $ last );
}
Private function dir_exists ()
{
Return file_exists (IMGDIR );
}
}
?>




Slideshow




<? = $ Curr;?> "Alt =" "/>

<? = $ Caption;?>



"> First |" class = "sp"> Previous "> Next |"> Last




I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.