PHP implements automatic scrolling of images

Source: Internet
Author: User
This article mainly introduces PHP's method of automatically displaying images in scrolling mode. It involves php's techniques for operating image effects and has some reference value. For more information, see

This article mainly introduces PHP's method of automatically displaying images in scrolling mode. It involves php's techniques for operating image effects and has some reference value. For more information, see

This example describes how PHP automatically displays images in scrolling mode. 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, paste it in a text file, and name it "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 |"> 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.