Jump to content


Photo

Periodically changing random images?


  • Please log in to reply
23 replies to this topic

#1 fishbowl

fishbowl
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 11 August 2006 - 11:18 PM

I need a script that will pull 20 random images from a pool of 2000, and will keep those same random 20 images for a certain time period. For example, every day a new set of 20 random images would show up.

I've found a lot of random image scripts, but with all of them, the image changes every time the page is refreshed. I don't want the images to change that often, but they do need to change periodically, without my having to go into the code and change the images manually.

I'm thinking about trying to modify an existing php random image script to do the above. However, I don't really know anything about PHP, and I have no idea where to begin.

#2 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 11 August 2006 - 11:37 PM

Dang thats a tough one.... Lemme thing for a few minutes lol
Why doesn't anyone ever say hi, hey, or whad up world?

#3 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 11 August 2006 - 11:55 PM

this is fairly easy to do.  if you have a database, use that - otherwise, use a simple flat file (although a db would be easier):

1.  check the db/flat file storage for the set of images to use.
a)  if the stored info's date/time is *insert interval here* older than the current, proceed to 2.
b)  if the stored info is NOT *interval* older than current, use the stored image info and you're done.
2.  use PHP to get the random images (perhaps use a pre-existing script).
3.  capture the images you are using (perhaps plug them into an array when you display them).
3.  store the contents of this array either in a db, or in a flat file, along with the current date/time.

hope this makes sense.

#4 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 12 August 2006 - 01:34 AM

Hmmm I attached some files i made for displaying 20 random images from a folder... It stores the names of the 20 images to use and recreates that file every 24 hours... Just look through the files and you should be able to figure out whats going on...

lol its not very good... I wrote it half asleep :D

Also if you have a DB i suggest using it instead of flat files...

[attachment deleted by admin]
Why doesn't anyone ever say hi, hey, or whad up world?

#5 fishbowl

fishbowl
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 12 August 2006 - 05:03 AM

Thanks for the help so far..

corbin, I tried the attached files, but they don't really work right. The images change with every refresh, and the number of images changes as well. Also, there's always at least one broken image, where no image name is pulled (shows up in images.txt as a string of numbers).

I really don't know anything about PHP, so I have no idea what's going on in those files. I've been trying to read through online PHP manuals to figure it out, but.. more help would be appreciated. :-\

#6 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 12 August 2006 - 05:39 AM

Damn i just wrote the whole script here and accidently pressed F5, ill just make the mscript and tell you
I am a PHP Guru, ask me questions if you want to

#7 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 12 August 2006 - 05:58 AM

Hehe I was kinda tired when I wrote that earlier... I just went back through it and its kinda... ummmm not good lol... I guess RockingGroudon said hes gonna make a script so ill just let them do that :D
Why doesn't anyone ever say hi, hey, or whad up world?

#8 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 12 August 2006 - 06:35 AM

I made it, the only problem is that this dosent check if the image is already in the 20
and also your images shud have names like image2000.gif

EDIT: Let me just go through it if i can set it to check if the images are repeating

[attachment deleted by admin]
I am a PHP Guru, ask me questions if you want to

#9 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 12 August 2006 - 06:36 AM

not that i want to beat rockinggroudon to it, i just wanted to give you something that was hopefully easy to follow and understand.  i should note that storing it in a file will require PHP 4.2.0 or greater, since it uses var_export().

i should also note that you should REALLY look into using a database to do this.  let us know if that's possible, as it's both more efficient and easier to write/understand.  that being said, here's the function itself:

<?php

function get_images_list($list_filename, $path_to_images, $number_of_images)
{
  // define an array of image extensions
  $image_extensions = array('gif', 'jpg', 'jpeg', 'png', 'bmp');

  // initialise the return variable
  $image_list = array();

  // check if the list file exists
  if (file_exists($list_filename))
  {
    // include it so that it defines an array for us (called $image_list by default)
    include($list_filename);

    // check if the list is less than 24 hours old
    if ($image_list['timestamp'] >= (time() - 24*3600))
    {
      // if it is less than 24 hours old, just drop the timestamp and return the list
      unset($image_list['timestamp']);
      return $image_list;
    }
    else
    {
      // if it's stale, then undo the definition the include has made
      unset($image_list);
    }
  }

  // by now it will have booted out of the function if the file's image list was useable
  // therefore if we're still here, we need to create a new file list (either for the first time, or to replace the stale one)

  // open the images directory
  $handle = @opendir($path_to_images);

  // boot out with a NULL return if the directory couldn't be opened
  if ($handle === FALSE)
    return NULL;

  // otherwise, go through the directory
  while ($filename = readdir($handle))
  {
    // grab a lowercase version of its extension
    $extension = strtolower(substr(strrchr($filename, '.'), 1));

    // if its extension is an image type, then log it
    if (in_array($extension, $image_extensions))
      $image_list[] = $filename;

    // boot out if we've reached the limit of images
    if (count($image_list) == $number_of_images)
      break;
  }

  // close the directory
  closedir($handle);

  // add a timestamp to the array
  $image_list = array_merge($image_list, array('timestamp' => time()));

  // turn the list content into something useable
  $content = '<?php $image_list = '.var_export($image_list, TRUE).'; ?>';

  // write the list to a file
  $newfile = fopen($list_filename, 'w');
  fwrite($newfile, $content);
  fclose($newfile);

  // kill the timestamp again
  unset($image_list['timestamp']);

  // return NULL if it found no images, or the image list if it did
  return (empty($image_list)) ? NULL : $image_list;
}

?>

in order to grab the current file list (the function will either grab the currently stored version, or create a new one and attempt to store it anew), use the following code:

$image_list = get_images_list('image_list.php', 'images/', 20);

replacing the variables with the actual values you want to use, of course.  keep in mind the list filename needs to be a .php file.  i assume you know how to go through an array using a foreach().  if not, it's quite simple:

<?php
foreach ($image_list AS $filename)
{
  echo '<img src="images/'.$filename.'" />';
}
?>

however, if the function didn't work for some reason, the $image_list variable will be NULL (not an array).  in order to accommodate this, you'll want to do something along the lines of:

<?php
if ($image_list === NULL || empty($image_list))
{
  echo 'No images could be grabbed.';
}
else
{
  // foreach stuff
}
?>

furthermore, if you want to randomize the order only (but not the overall list of images), you can use:

shuffle($image_list);

somewhere between when you grab it, and when you use the foreach() to echo the images.

my aim with this is to get you to learn something new.  hopefully that's what's happened.  let me know if anything is foggy or if it doesn't work (i haven't tested it).

EDIT:  my method does not require that the images be any particular name, since renaming 2000+ images would be a pain.

#10 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 12 August 2006 - 06:39 AM

It dosent matter me if you beat me, but the reason I dont use COOKIES, SESSIONS or SQL is because I make scripts so everyone can use it, and with the lowest resources.
I know you came out aa li'l better than me but whatever, atleast I made it. ^_^
I am a PHP Guru, ask me questions if you want to

#11 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 12 August 2006 - 06:45 AM

hehe both of yall versions was better than mine... Oh well.. Didnt put much thought or effort into mine :D
Why doesn't anyone ever say hi, hey, or whad up world?

#12 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 12 August 2006 - 07:22 AM

rockinggroudon, i wasn't trying to "beat" you.  i was simply pointing out that the image names didnt have to be imagenumber.whatever, in case he thought it applied to my solution as well.  as for resources, cookies sessions and databases generally use fewer resources than a filesystem approach; that's irrelevant anyhow, since i'm using files.

i'm not trying to show you up, i just figured i'd see if i could teach the user something new.

#13 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 12 August 2006 - 07:34 AM

I know that. You see what I meant by resources was that you need MySQL to have Databases, or neother SQL. You need to have COOKIES enabled to have cookies, and the webmaster needs to allow SESSIONS to use sessions.
btw the reason i got hyper was that mostly I dont create scripts for ppl cause i have HW to, u see i am too younger to you ppl.
I am in VIIth grade
The first time I created a script for sum1 and there was sum1 to give him a better 1.
You can urself know how angry i cud hav got
I am a PHP Guru, ask me questions if you want to

#14 fishbowl

fishbowl
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 12 August 2006 - 08:23 AM

Thanks to everyone for their responses.. this is by far the most helpful forum I've ever been on.

akitchin, I tried your script, and it seems to be working. I didn't want to wait 24 hours to test it, so I took this line:

    // check if the list is less than 24 hours old
    if ($image_list['timestamp'] >= (time() - 24*3600))

and tried editing it so that it would change the images, say, every minute. I wasn't really sure how to format that (did several PHP.net/Google searches, came up empty handed), so I tried changing the "24*3600" to things like, "1*60", "60", "0", "1*1", "1". Waited about 2 minutes after each change to refresh, and the same random images came up each time. So, I'm not sure if that's a problem with the script, or if I formatted the time wrong.

I do have a mysql database, as well as PHP 4.4. However, I literally know nothing about mysql coding.. I can install pre-written scripts, but I don't know how to write my own or edit any existing ones.

#15 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 12 August 2006 - 08:31 AM

Use mine then
I am a PHP Guru, ask me questions if you want to

#16 fishbowl

fishbowl
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 12 August 2006 - 06:38 PM

RockingGourdon, your code has a similar timestamp line in it, and again I can't figure out how to modify it so that I don't have to wait 24 hours to test it. I tried what I did with akitchin's code, but with yours, and got the same results. So I assume that I'm doing something wrong. My only problem with your code is that I don't want to go through and rename all 2000 images to image1.gif, image2.gif, etc. Thanks for the help, though.

#17 Chetan

Chetan
  • Members
  • PipPipPip
  • Advanced Member
  • 162 posts
  • LocationIndia

Posted 12 August 2006 - 06:44 PM

You see the timestamp is in seconds and i minus it to check if it works there so minus in seconds and for the renaming thing, you have to go through some pain sometimes, ya i know you are not suppose to go through the pain but yet just telling you if you cant get akitchins script to work.

Ill see wat you do though to check if you do something wrong.

Well do this to check akitchins code

    // check if the list is less than 24 hours old
    if ($image_list['timestamp'] >= (time() - 24*3600))

changed to

    // check if the list is less than 24 hours old
    if ($image_list['timestamp'] >= (time() - 10))

Now it should change every 10 secs
I am a PHP Guru, ask me questions if you want to

#18 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 12 August 2006 - 07:17 PM

as rocking said, it's in seconds.  there are 3600 seconds in an hour, and 24 hours in a day, so i subtract 24 * 3600 seconds from the current time to get the time 24 hours ago.

as for using MySQL, perhaps we'll leave that to another topic if you want to learn how to use it some day.  if this works for now, might as well use it.

#19 fishbowl

fishbowl
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 12 August 2006 - 11:39 PM

I see. Well, I edited the number of seconds in both codes, and neither changed the set of images when I refreshed after 10 seconds.

I also tried manually removing the image lists, to see if it would create a new set of random images, but the same random images are pulled each time. With both codes. :-[

#20 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 13 August 2006 - 12:38 AM

haha whoops.  i totally forgot about the random part.  change the while() loop to this:

<?php
  // otherwise, go through the directory
  while ($filename = readdir($handle))
  {
    // grab a lowercase version of its extension
    $extension = strtolower(substr(strrchr($filename, '.'), 1));

    // if its extension is an image type, then log it
    if (in_array($extension, $image_extensions))
      $image_list[] = $filename;
  }
?>

and add this between the closedir() command and the array_merge():

<?php
$chunks = array_chunk($image_list, $number_of_images);
$image_list = $chunks[0];
?>

(minus the php tags, since those are already in your file, they're here for syntax highlighting).




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users