Jump to content

Archived

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

perezf

images change

Recommended Posts

How do i make an image change on refresh of the page
im stuck without a clue

Share this post


Link to post
Share on other sites
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">

Share this post


Link to post
Share on other sites
ok say i have <img src="image1.jpg" /> how can i make it where it changes with other images in a certain folder

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
okay so lets say i have
[code]
$image = array("image1.jpg", "image2.gif", "image3.jpg");
echo "<img src=\"$image\" />";
[/code]

how do i make it choose random

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
[quote author=Barand link=topic=107542.msg431745#msg431745 date=1157915196]
Andy,

$the_image = $image[array_rand($image)];
[/quote]

Aha! ... live and learn

Share this post


Link to post
Share on other sites
Yours was just getting a random index, not the image. (I put it down to cocoa-deficiency ;D)

Share this post


Link to post
Share on other sites

×

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.