Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 682
  • Last Modified:

change image on timer php

Hey Experts, I would like to change an image out on a timer(every 24 hrs) to different image(image1, image 2,image 3,etc.)
For example, I made a countdown clock for a facebook page and cannot use javascript or flash, so I need a php way to change the image automatically every 24hrs (so i don't have to manually change it everyday).
Is there a way to do this?
0
adrian7474
Asked:
adrian7474
  • 5
  • 5
  • 5
2 Solutions
 
Peter_WernerCommented:
Normally you would do that in a cron task that is scheduled to run periodically.  Then you can replace the file at a given location. You can implement your cron task in php as well as any other, usually scripting, language.

If you can't create a cron task at your server then you can still use php to verify every time when your page is served if the 24 hours is over and replace the image.

The actual solution also depends on the details.  You could store images names by the date when they should be served, e.g. 2010-12-31.jpg and generate the file name with php in the <img> tag.

You could use always the same filename, check the file modification date and if it's older than 24 hours you overwrite the file in your php script.

You can even use a php link in the <img> tag, and use header() to set the appropriate mime-type and content-length in your php script tat serves the image data and then use read() to read the actual image.
0
 
adrian7474Author Commented:
Wow, so many options. I can't use cron w/server. I would be interested in how to store an array of images by date, eg. 12.23.jpg,12.24.jpg, then change them every 24 hrs using the img tag. How would one do this? Coding skills limited here.
0
 
gyoregCommented:
Hey.

I can see two ways:
- you create a PHP which will dynamically pass through the proper image
- you run a scheduled task daily, which changes the images as you wish.

That's it...
<?php

// #1: passing through the image
// you'll need to embed the image like this:
// <img src="my_image.php">

# find out which image should be shown
$number_of_images = 4;  // change it as you wish...
$filename = 'image' . (((time()/3600/24)%$number_of_images)+1).'jpg';

# set up header: we will show image not html...
header("Content-type: image/jpeg");

# pass through the image
readfile($filename);

?>

<?php

// #2: second solution: should be started daily by scheduled task (e.g. crontab)
// it copies file from image1.jpg .. imageN.jpg to image_current.jpg
// you should embed image_current.jpg to your page

# find out which image should be shown
$number_of_images = 4;  // change it as you wish...
$filename = 'image' . (((time()/3600/24)%$number_of_images)+1).'jpg';

# do the copy
copy($filename, 'image_current.jpg');

?>

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
adrian7474Author Commented:
Cool. I have a few questions on how to make it work.
1. What do you mean by number of images. Do mean total for my countdown?
2. does the +1 mean it will add a number to the end of the image incrementally?
3. How do change the time to 30 secs so i can test and not wait a day?

Here is what I got so far, but feel it's not right.

thanks again for the help.
cheers

<img src="mysite.com/images/countdown_day_01.png" />
<?php

// #1: passing through the image
// you'll need to embed the image like this:
// <img src="my_image.php">

# find out which image should be shown
$number_of_images = 4;  // change it as you wish...
$filename = 'countdown_day_01_' . (((time()/3600/24)%$number_of_images)+1).'png';

# set up header: we will show image not html...
header("Content-type: image/png");

# pass through the image
readfile($filename);

?>

Open in new window

0
 
gyoregCommented:
Yepp, Peter also gave other alternatives. All of them are pretty clear. Overwriting the current when it is older than 1 day is less resource-consuming,
0
 
gyoregCommented:
1. I assumed that you have image1.jpg, image2.jpg, ... imageN.jpg. The $number_of_images is N here. In other words how many images do you have. (I also assumed that the numbering is continuous, so there is no empty place).

Anyhow you may just simply change this part of course, just the result should be the filename of the current image. You can put all your image names to an array as well, if it is easier, and then choose a random one with array_rand().

2. No. The number is generated by (time()/3600/24)%$number_of_images) where time() is the number of seconds from 1/1/1970, which is divided by 3600*24 (so converted to days), and then I create modulus with the number of images. This would result in a number between 0..(N-1), so I need to add one (+1) to get the proper range (1..N).


3. Just replace the 3600/24 in the expression to 30... :)
0
 
gyoregCommented:
Yeppp. You may want to define your filename as:

$filename = 'countdown_day_' . str_pad(((time()/3600/24)%$number_of_images)+1, 2, '0', STR_PAD_LEFT) . 'png';
0
 
gyoregCommented:
You'll need to remove the <img src="mysite.com/images/countdown_day_01.png" /> from the beginning of the file obviously, and you should not send any output just the header and the image itself (the result of the readfile() function), as this PHP will return with the image itself, not a page containing your image.

You may need a different page (html file) to show this image on web.
0
 
Peter_WernerCommented:
You may want to explain your real target, like "display the number of days left until a given date using custom images".
0
 
adrian7474Author Commented:
Ok, how do i make it count down as opposed to count up?
0
 
Peter_WernerCommented:
$ix1 = (time()/3600/24) % $number_of_images;  // [0..n-1]
$ix2 = $number_of_images - $ix1; // [n .. 1]

count up: use $ix1
count down: use $ix2

How many images do you have?
Do you want to cycle them? Then gyoreg wrote your code above.
Do you have a dedicated image for every day? Use the date for the filename.
0
 
adrian7474Author Commented:
Thanks, yes I have a dedicated number of images (60) and want to start countdown tomorrow, so my first image would be 60, then 59, 58, etc...
I am making some progress with this code, but am not there yet. here is what i got so far. I change the 3600/24 to 10 to test. It seems to echo out file name+number, but counts up as opposed to down and also it starts the numbers at random number. I think its because the & means take the remainer of x amount, but i am missing where the x amount is.
Any help completing this code would be great.


<?php

# find out which image should be shown
$number_of_images = 60;  // change it as you wish...
$filename = 'countdown_day_' . (((time()/10)%$number_of_images)+1).'.png';


echo ($filename);
?>

Open in new window

0
 
Peter_WernerCommented:
<?php

# find out which image should be shown
$daysec = 3600 * 24;
$daysec = 10; // test: make a day 10 seconds
$number_of_images = 60;
$ix = (time()/$daysec) % $number_of_images;  // [0..n-1]
//$ix_up = $ix + 1; // [1..n]
$ix_down = $number_of_images - $ix; // [n .. 1]
$ix_padded = str_pad($ix_down, 2, '0', STR_PAD_LEFT);
$filename = 'countdown_day_' . $ix_padded . '.png';

echo ($filename);
?>
0
 
Peter_WernerCommented:
BTW, what are on those pictures, not numbers from 60 to 01?
0
 
adrian7474Author Commented:
Awesome, thanks. I actually came up with this mod and got it working. Since you and gyoreg both helped i should split the solution evenly right? I want to be fair. I hope this is ok for both of you, you both were awesome in helping me find this solution.  thanks again. :)
<?php


 
 $day   = 02;     // Day of the countdown
 $month = 11;      // Month of the countdown
 $year  = 2010;   // Year of the countdown
 $hour  = 23;     // Hour of the day (east coast time)
 
 
 $calculation = ((mktime ($hour,0,0,$month,$day,$year) - time(void))/3600);
 $hours = (int)$calculation;
 $days  = (int)($hours/24);
 $countdown = $days;




# find out which image should be shown
$number_of_images = 60;  // change it as you wish...
$filename = 'countdown_day_' . $countdown .'.png';

# set up header: we will show image not html...
//header("Content-type: image/png");

# pass through the image
//readfile($filename);
?>
<img src="images/<?php echo ($filename);?>">

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now