Jump to content

images change


perezf

Recommended Posts

That would surely depend on whatever code you're using to generate the html code that displays the image.  If the issue is cached images, then you can add a random number at the end of the image filename to make your browser think it's a different image and therefore fetch it from the server .... <img src="whatever.jpg?asdf1234">
Link to comment
Share on other sites

Get the image names into an array and select a random image from the array, then display it. Is there a small fixed number of images, a large fixed number of images, or could the number of images change frequently? Are the images all always in the same folder?

You can use the glob() function to generate an array of specific filetypes from a folder. Examples in the online manual. rand() is also in the manual.
Link to comment
Share on other sites

[code=php:0]
$imgpath = 'path/to/my/crap/pic/';

$dirp = opendir($imgpath); //open the directory
$validimgtypes = array("gif", "jpg", "jpeg", "png", "bmp");
$image = array();

//read the directory
while($myfile = readdir($dirp))
{
$picfileA = explode(".", $tutorfile); //delimit the file by .
$picfileA = array_reverse($tutorfileA); //reverse array to get extension
$ext = strtolower($picfileA[0]); //change ext to lowercase for case-sensitivity

if(in_array($ext, $validimgtypes))
{
$image[] = $myfile;
}
}
closedir($dirp);


//if there is at least 1 image in the folder
if(count($image) > 1)
{
$randimage = $image[array_rand($image)];
echo "<img src=\"$randimage\" alt=\"$randimage\" />";
}


[/code]
Link to comment
Share on other sites

[quote author=perezf link=topic=107542.msg431690#msg431690 date=1157907199]
okay so lets say i have ... how do i make it choose random
[/quote]

[code]
$image = array("image1.jpg", "image2.gif", "image3.jpg");
$the_image = rand(0, count($image)-1);
echo "<img src='". $the_image. "'/>";
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.