Sneblot Posted January 17, 2011 Share Posted January 17, 2011 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 More sharing options...
Garethp Posted January 17, 2011 Share Posted January 17, 2011 <?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 Link to comment https://forums.phpfreaks.com/topic/224654-php-image-randomiser-help/#findComment-1160467 Share on other sites More sharing options...
Sneblot Posted January 17, 2011 Author Share Posted January 17, 2011 Sorry to sound like a complete idiot but this is my first implementation of php in a site. Where should I place this code? I was advised against placing php directly in html files. Link to comment https://forums.phpfreaks.com/topic/224654-php-image-randomiser-help/#findComment-1160469 Share on other sites More sharing options...
Garethp Posted January 17, 2011 Share Posted January 17, 2011 That would be the code to rotate.php If it's your first code, I suggest you go through some tutorials on basic PHP before you even start to look at things like displaying images. Start here http://www.w3schools.com/php/default.asp Link to comment https://forums.phpfreaks.com/topic/224654-php-image-randomiser-help/#findComment-1160470 Share on other sites More sharing options...
Sneblot Posted January 17, 2011 Author Share Posted January 17, 2011 Thanks for the advice. I shall look up the W3schools. Link to comment https://forums.phpfreaks.com/topic/224654-php-image-randomiser-help/#findComment-1160471 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.