Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: {PHP} Display a different image at midnight every day

  1. #1
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default {PHP} Display a different image at midnight every day

    Hello,

    I have been searching every where for a php + mysql script that will display one "Pic of the day" image in my header from a folder. (NOT USING A WORDPRESS SITE)

    Notes:

    1. I am NOT using sequential images
    2. I would like to have the script pick the images at random, never display the same image twice (until all images have been displayed)
    3. Image must display all day until 12:00am (New York City timezone) before displaying the next image.
    4. Images will be stored in a folder on my computer.

    Can someone do this?

  2. #2
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Anyone?

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Not sure how mysql would fit into this. When you say "4. Images will be stored in a folder on my computer." Is this live? Is 'my computer' the host? You can't expect anyone except you to see images on a local computer.

    Also, when you say random AND not repeat. That's actually not random. What you perhaps want is to randomly pick an image each time, temporarily eliminate that image from the pool. Keep doing that until there are none left in the pool, and then start over again. Even doing it that way, it's possible that the last and the first image would be the same. The chance of that decreases the more images you have, but would still always be a possibility.
    Last edited by jscheuer1; 04-14-2018 at 03:45 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    Not sure how mysql would fit into this. When you say "4. Images will be stored in a folder on my computer." Is this live? Is 'my computer' the host? You can't expect anyone except you to see images on a local computer.

    Also, when you say random AND not repeat. That's actually not random. What you perhaps want is to randomly pick an image each time, temporarily eliminate that image from the pool. Keep doing that until there are none left in the pool, and then start over again. Even doing it that way, it's possible that the last and the first image would be the same. The chance of that decreases the more images you have, but would still always be a possibility.
    It's been quite some time since I have worked with php+mysql to do custom coding. But in any case, I require a php script that I can use to display a different image in my website header every day at 12 midnight. Can you help me accomplish that kind of php script? I have looked everywhere and can seem to find anything close to it at all.

    Thank you
    Brian

  5. #5
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok,

    So can you make this php script: http://www.dyn-web.com/code/random-image-php/ change so it can display a different image every day at 12:00am (midnight) ? That is as close to what I am looking to do. Sorry but my php skills are very rusty, haven't been in this area for years.

  6. #6
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I think this will better explain what I want to do, sorry but the example below is copied from another website, I believe that code will display an image only at midnight...Which is what I want to do...

    i
    Code:
    f( $date->format( 'H') == 0 && $date->format( 'i') == 0) {
        echo "<img src="">";
    }
    For the <img src part, I want to use a url that will be stored in mysql db like: http://localhost/images/new_york.png

    so the images folder is located at: http://localhost/images folder (hence, I am running a server on my computer)

    Can you provide a php script that will take the php codes I provided and combine them so a random image script will change a different image a 12 midnight every night?

    Sorry for my lack of knowledge in explaining.

    Thank you
    Brian

  7. #7
    Join Date
    Jan 2015
    Posts
    78
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default

    While this wouldn't take much code, I doubt you will find anyone willing to write and test something for you for free.

    Here's how I would do this -

    Add a date column to the table holding the urls. This column should allow a NULL value (indicates not used.)

    1) On each page request query to find if there is a row in the table with a date that matches the current date.

    1.a) If there is a matching row, retrieve the row and use the url to display the image. You should only store the filename.ext in the database, so that you can easily use the same script on any server and with any folder location for the images.

    1.b) If there is no matching row, either the day changed or this is the first ever request.

    1.b.1) Query to get the ids of the rows that have a NULL value in the date column.

    1.b.1.a) If there are no matching rows, all the images have been used, UPDATE all the rows to change the date column to NULL, and go back to step 1.

    1.b.1.b) If there are matching row(s), retrieve the ids, get one random id. UPDATE the matching row with the current date in the date column, and go back to step 1.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Great thinking DyDr! Love the way the date becomes a sort of delimiter. Still doesn't eliminate the possibility of a repeat image at the end/beginning of a new cycle. Using part of your logic and a few (or less) flat files. I think I can do that and eliminate the possible repetition. Still need to write it up though, as you say, a fair amount of work. We can put your multidimensional array into a flat file and work off of it, or save the date separately in its own file, and in any case use the file containing the image names to gradually whittle down the choices. A separate file could contain the current image name, it would be banned from the new array of images for the next cycle when that needs to occur. This could likely all be done in a single flat file though, just gets a little trickier.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,497
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    OK, this seems to work very well in limited testing. Should be easily adapted to whatever (I have it write the source of an image tag, but it could write the background image of an element):

    PHP Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Random Image Each Midnight</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <img src="
    <?php
    $path 
    "../site5/cssimages/scaled/"// set path to images, include trailing slash
    $imgfile "images.txt"// set path and filename to file to hold image names and current date, should not initially exist
    $eol PHP_EOL// end of line reference
    $date date("zY"time()); // the current date as day number in the year, followed by the 4 digit year number
    if(file_exists($imgfile)){$datedims file($imgfileFILE_IGNORE_NEW_LINES);}
    else{
    $datedims = array();}
    if (
    count($datedims) < or (count($datedims) < and $date !== $datedims[count($datedims) - 1])){ // it's the first time or we're beyond time and only one image is left
        
    $imar glob("$path{*.jpg,*.gif,*.png}"GLOB_BRACE); // grab images as array (include/exclude image patterns if/as desired)
        
    if(count($datedims)){ // if this isn't the first time and the $imgfile hasn't been deleted
            
    if(($key array_search($datedims[0], $imar)) !== false){ // and the current image is still in the folder
                
    unset($imar[$key]); // do not consider it in the next iteration
            
    }
        }
        
    shuffle($imar); // random order for images (once shuffled will continue to be random until used up)
        
    $imar[] = $date// add the current date to the array for checking later
        
    file_put_contents($imgfileimplode($eol$imar), LOCK_EX); // save for future use
        
    $datedims $imar// use current $imar
    }
    else if (
    $date !== $datedims[count($datedims) - 1]){ // there're images left but we're past time
        
    array_shift($datedims); // get rid of the first image listing
        
    array_pop($datedims); // remove old outdated date
        
    $datedims[] = $date// add the current date to the array for checking later
        
    file_put_contents($imgfileimplode($eol$datedims), LOCK_EX); // save for future use
    }
    echo 
    $datedims[0]; // write result (first item now in the $datedims array)
    ?>
    ">
    </body>
    </html>
    Let me know if there are any problems or questions.

    Notes: Unless you have no possibility of the variables used here ever conflicting with other PHP code you're using or may use, the above PHP code should probably be encapsulated as a function, at which point $c could be used to economize on the usage of count($datedims) by setting it to that once $datedims is established, and utilizing it instead of the expression. array_splice() might be better than unset(), but is more cumbersome and the subsequent shuffle() restores sequential numeric keys (at least here), and doesn't (in testing) seem to be an issue, even if it didn't, though further changes might make that an issue. Not sure if LOCK_EX is required or even advisable, but seems to me as it should work out well in this scenario.
    Last edited by jscheuer1; 04-19-2018 at 04:38 AM. Reason: code improvement, later add "Notes", later improve glob syntax
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    OK, this seems to work very well in limited testing. Should be easily adapted to whatever (I have it write the source of an image tag, but it could write the background image of an element):

    PHP Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Random Image Each Midnight</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
    <img src="
    <?php
    $path 
    "../site5/cssimages/scaled/"// set path to images, include trailing slash
    $imgfile "images.txt"// set path and filename to file to hold image names and current date, should not initially exist
    $eol PHP_EOL// end of line reference
    $date date("zY"time()); // the current date as day number in the year, followed by the 4 digit year number
    if(file_exists($imgfile)){$datedims file($imgfileFILE_IGNORE_NEW_LINES);}
    else{
    $datedims = array();}
    if (
    count($datedims) < or (count($datedims) < and $date !== $datedims[count($datedims) - 1])){ // it's the first time or we're beyond time and only one image is left
        
    $imar glob("$path{*.jpg, *.gif}"GLOB_BRACE); // grab images as array (include/exclude image patterns if/as desired)
        
    if(count($datedims)){ // if this isn't the first time and the $imgfile hasn't been deleted
            
    if(($key array_search($datedims[0], $imar)) !== false){ // and the current image is still in the folder
                
    unset($imar[$key]); // do not consider it in the next iteration
            
    }
        }
        
    shuffle($imar); // random order for images (once shuffled will continue to be random until used up)
        
    $imar[] = $date// add the current date to the array for checking later
        
    file_put_contents($imgfileimplode($eol$imar), LOCK_EX); // save for future use
        
    $datedims $imar// use current $imar
    }
    else if (
    $date !== $datedims[count($datedims) - 1]){ // there're images left but we're past time
        
    array_shift($datedims); // get rid of the first image listing
        
    array_pop($datedims); // remove old outdated date
        
    $datedims[] = $date// add the current date to the array for checking later
        
    file_put_contents($imgfileimplode($eol$datedims), LOCK_EX); // save for future use
    }
    echo 
    $datedims[0]; // write result (first item now in the $datedims array)
    ?>
    ">
    </body>
    </html>
    Let me know if there are any problems or questions.

    Notes: Unless you have no possibility of the variables used here ever conflicting with other PHP code you're using or may use, the above PHP code should probably be encapsulated as a function, at which point $c could be used to economize on the usage of count($datedims) by setting it to that once $datedims is established, and utilizing it instead of the expression. array_splice() might be better than unset(), but is more cumbersome and the subsequent shuffle() restores sequential numeric keys (at least here), and doesn't (in testing) seem to be an issue, even if it didn't, though further changes might make that an issue. Not sure if LOCK_EX is required or even advisable, but seems to me as it should work out well in this scenario.
    John,

    First let me thank you so much for taking the time to code this up! I am very greatful for your experience in programming, I am one who truly does respect the work put out by you guys for all the countless hours you must have put into knowing what you do about computer programming. Thank you for that, really, Thank you!

    Second, I do have an issue, that maybe you can help me resolve...I have done the following:

    1. Copied / Pasted the code into a file called random.php
    2. I created a images.txt file in which the image ulrs and dates will be written to. Gave it 755 permissions (Running Ubuntu 16.04 Linux, btw)
    3. Update the path to my images folder (in which case I am using flags, so the folder is called flags)

    I put the random.php in my /var/www/html (root folder on localhost)...I have the folder inside the root (/var/www/html/flags/) with the trailing back-slash, but when I run the random.php script, I get a (broken) icon of the image, image not showing . When I right mouse click on the broken image and select view image, the url is: http://localhost/1052018

    Can you provide a solution?

    Thanks again!
    Brian

Similar Threads

  1. Replies: 0
    Last Post: 06-18-2013, 03:57 AM
  2. Display a default enlarged image using CSS Image Gallery
    By KCgame in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 08-14-2009, 06:54 AM
  3. Replies: 2
    Last Post: 02-08-2009, 08:42 PM
  4. display a text under each image in OO_CMotion Image Gallery
    By nuwaninfo in forum Dynamic Drive scripts help
    Replies: 12
    Last Post: 12-17-2008, 10:41 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •