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
https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89355
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
https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89391
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
https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89443
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
https://forums.phpfreaks.com/topic/20295-images-change/#findComment-89465
Share on other sites

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.