15 points for rotate image script

Status
Not open for further replies.

The_Magistrate

New Member
Messages
1,118
Reaction score
0
Points
0
Spartan Erik, your script doesn't rotate images... it randomizes images. I'm trying to write one in PHP using a cookie... gimme a few minutes.

Here ya go... crude, but effective. Requires you to add the image filenames into the array and specify the path.

P.S. If you could throw a few more than 15 point my way, it would be appreciated.
PHP:
<?
// Image Rotator Script
// By Alex Cline (http://alex.elementfx.com)
// Displays the next image in the array based on a cookie which stores the previous image
// If the cookie doesn't exist, the first image is shown.

// Create the array of images.  This must contain the names of all the images.
$imgArray = array("image1.jpg", "image2.gif", "image.png");
// Path to where your images are stored
$hostDir = "http://www.somedomain.com/path/to/images/"; // Don't forget that ending '/'

$imgArraySize = count($imgArray); // Store the size of the array (starts at 1)
$imgToShow = 0; // We'll default to always show the first image.

// Check to see if the cookie has already been created.
if ($_COOKIE['imagerotate'] != "") {
  // The cookie existed
  $imgToShow = $_COOKIE['imagerotate'];
  if($imgToShow + 1 >= $imgArraySize) {
    // The image we were supposed to show next would have been out of bounds in the array.
    $imgToShow = 0;  // We'll show the first image
  }
  else  // The image to be shown is not out of bounds.
    $imgToShow++;  // Increment the image counter
}

// Write the new cookie with the new image we are showing
setcookie ("imagerotate", $imgToShow, time()+1000);

//Print the image from the directory.
echo "<img src=\"".$hostDir.$imgArray[$imgToShow]."\" alt=\"An Image\" />";
?>
 
Last edited:

Josh

Uber No0b
Messages
394
Reaction score
0
Points
0
The_Magistrate said:
Spartan Erik, your script doesn't rotate images... it randomizes images. I'm trying to write one in PHP using a cookie... gimme a few minutes.

Here ya go... crude, but effective. Requires you to add the image filenames into the array and specify the path.

P.S. If you could throw a few more than 15 point my way, it would be appreciated.
PHP:
<?
// Image Rotator Script
// By Alex Cline (http://alex.elementfx.com)
// Displays the next image in the array based on a cookie which stores the previous image
// If the cookie doesn't exist, the first image is shown.

// Create the array of images.  This must contain the names of all the images.
$imgArray = array("image1.jpg", "image2.gif", "image.png");
// Path to where your images are stored
$hostDir = "http://www.somedomain.com/path/to/images/"; // Don't forget that ending '/'

$imgArraySize = count($imgArray); // Store the size of the array (starts at 1)
$imgToShow = 0; // We'll default to always show the first image.

// Check to see if the cookie has already been created.
if ($_COOKIE['imagerotate'] != "") {
  // The cookie existed
  $imgToShow = $_COOKIE['imagerotate'];
  if($imgToShow + 1 >= $imgArraySize) {
    // The image we were supposed to show next would have been out of bounds in the array.
    $imgToShow = 0;  // We'll show the first image
  }
  else  // The image to be shown is not out of bounds.
    $imgToShow++;  // Increment the image counter
}

// Write the new cookie with the new image we are showing
setcookie ("imagerotate", $imgToShow, time()+1000);

//Print the image from the directory.
echo "<img src=\"".$hostDir.$imgArray[$imgToShow]."\" alt=\"An Image\" />";
?>

points comming your way!
 
Status
Not open for further replies.
Top