Jump to content

PHP image randomiser help


Sneblot

Recommended Posts

I am attempting to do is random image rotation on the index page, I have followed this sitepoint tutorial  on how to code a php image rotation and would like to make sure that the page calls on the php file properly. I have created a separate php file below is the code:

 

<?php 
// rotate images randomly but w/o dups on same page - format: 
// <img src='rotate.php?i=0'> - rotate image #0 - use 'i=1' 
// for second, etc 
// (c) 2004 David Pankhurst - use freely, but please leave in my credit 
$images=array( // list of files to rotate - add as needed 
"images/paulinewoolley/bjarg.jpg",
"images/paulinewoolley/ion.jpg",
"images/paulinewoolley/foss.jpg",
"images/paulinewoolley/altitude1.jpg",
"images/paulinewoolley/altitude5.jpg",
"images/paulinewoolley/altitude6.jpg" );
$total=count($images); 
$secondsFixed=10; // seconds to keep list the same 
$seedValue=(int)(time()/$secondsFixed); 
srand($seedValue); 
for ($i=0;$i<$total;++$i) // shuffle list 'randomly' 
{ 
$r=rand(0,$total-1); 
$temp =$images[$i]; 
$images[$i]=$images[$r]; 
$images[$r]=$temp; 
} 
$index=(int)($_GET['i']); // image index passed in 
$i=$index%$total; // make sure index always in bounds 
$file=$images[$i]; 
header("Location: $file"); // and pass file reference back 
?>

 

Here is the code that would be placed on the html file

 

<img src='canstudios.org/rotate.php?i=0'>image #1 
<img src='canstudios.org/rotate.php?i=1'>image #2 
<img src='canstudios.org/rotate.php?i=2'>image #3

Link to comment
https://forums.phpfreaks.com/topic/224654-php-image-randomiser-help/
Share on other sites

<?php

$images=array( // list of files to rotate - add as needed 
   "images/paulinewoolley/bjarg.jpg",
   "images/paulinewoolley/ion.jpg",
   "images/paulinewoolley/foss.jpg",
   "images/paulinewoolley/altitude1.jpg",
   "images/paulinewoolley/altitude5.jpg",
   "images/paulinewoolley/altitude6.jpg" );

$file = array_rand($images);

header("Content-Type: image/png");
echo file_get_contents($images[$file]);

?>

The header might not be right, but that should be the general idea of what you want

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.