Hello,
I want to display images dynamically after a certain time, say 5 seconds. I have to do the coding in PHP. The images are coming form a folder.
Please help. Thanks a lot in advance.
Printable View
Hello,
I want to display images dynamically after a certain time, say 5 seconds. I have to do the coding in PHP. The images are coming form a folder.
Please help. Thanks a lot in advance.
You can create the page's markup via PHP, but changing content after the page is loaded will require Javascript. Here's a basic outline:
- with PHP, find all image files in the particular directory of interest
- write the paths to these images into an array in a block of inline Javascript on your page
- use Javascript to rotate through the array of images on a set interval
Do some Googling on these topics, try writing out something (even just the first step), then post what you've got.
Something else would be to have a script load the directory, and show a random image from the directory using the GDLib. You can use <img src='random.php' />
Then either refresh the page using javascript, or reload the image using js
as samba says, this can't be done in php but it can be done in javascript...
i have some code i used in a site i've made...
(i did not make the code, i found on google)
this won't be random but this will change the image every 5 secs.Code:<script type="text/javascript">
var delay = 5000;
var noOfImgs = 2; // number of images
pic0 = new Image(900,175); //width, height
pic0.src = "images/Banner1.jpg";
pic1 = new Image(900,175);
pic1.src = "images/Banner3.jpg";
var imgArry = new Array()
imgArry[0] = "images/Banner1.jpg";
imgArry[1] = "images/Banner3.jpg";
function rotateImage(imgNo)
{
clearTimeout(timerId);
if(imgNo == noOfImgs)
{
imgNo = 0;
}
else
{
document.getElementById('image').src = imgArry[imgNo]; /* this is the image <img id="image" src="whatever.png" /> */
imgNo++;
}
var recur_call = "rotateImage('" + imgNo + "')";
setTimeout(recur_call, delay);
}
var timerId = setTimeout('rotateImage(0)', 5000);
</script>
Well in PHP, You can create your images in php. Well the images will php jpg, gif, etc..., but i php code can dynamically change them;
Code:<?php
//Send a generated image to the browser
create_image();
exit();
function create_image()
{
//Let's generate a totally random string using md5
$md5 = md5(rand(0,999));
//We don't need a 32 character long string so we trim it down to 5
$pass = substr($md5, 10, 5);
//Set the image width and height
$width = 100;
$height = 20;
//Create the image resource
$image = ImageCreate($width, $height);
//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 204, 204, 204);
//Make the background black
ImageFill($image, 0, 0, $black);
//Add randomly generated string in white to the image
ImageString($image, 3, 30, 3, $pass, $white);
//Throw in some lines to make it a little bit harder for any bots to break
ImageRectangle($image,0,0,$width-1,$height-1,$grey);
imageline($image, 0, $height/2, $width, $height/2, $grey);
imageline($image, $width/2, 0, $width/2, $height, $grey);
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);
//Free up resources
ImageDestroy($image);
}
?>
oh thats called captcha....
i believe you need this for a form(login, register guestbook or whatever) and you need to make it possible to change the image without the page having to reload...
Can't he just refresh the page with a new image?
The next code displays an image selected at random using PHP. The list of images for this example comes from a directory listing. According to php tutorials you could also select an image from an array of images from other sources, such as a database query, or a static list of images you provide.
The code for this example:
Place the following where you wish the random image to appear:PHP Code:<?php
function getRandomFromArray($ar) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($ar);
return $ar[$num];
}
function getImagesFromDir($path) {
$images = array();
if ( $img_dir = @opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// checks for gif, jpg, png
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[] = $img_file;
}
}
closedir($img_dir);
}
return $images;
}
$root = '';
// If images not in sub directory of current directory specify root
//$root = $_SERVER['DOCUMENT_ROOT'];
$path = 'images/';
// Obtain list of images from directory
$imgList = getImagesFromDir($root . $path);
$img = getRandomFromArray($imgList);
?>
PHP Code:<img src="<?php echo $path . $img ?>" alt="" />
This thread's like open mic night and the OP hasn't even returned.
while none of these "examples" really even address the issue that PHP cannot do what was originally asked alone, the use of any *srand (srand, mt_srand) function calls are unneeded as of PHP 4.2. PHP will automatically seed the random number generator for you.
furthermore, I'd suggest not posting random pieces of code if you're not entirely sure what that code is actually doing. in Justa's case, you have some JavaScript that creates image objects -- these objects are never actually used, and instead you're simply changing the source value of the 'main' image (which would be the easiest way to do it anyway).