Solved

change image on timer php

Posted on 2010-09-02
15
629 Views
Last Modified: 2012-05-10
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
Comment
Question by:adrian7474
  • 5
  • 5
  • 5
15 Comments
 
LVL 3

Expert Comment

by:Peter_Werner
Comment Utility
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
 

Author Comment

by:adrian7474
Comment Utility
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
 
LVL 5

Expert Comment

by:gyoreg
Comment Utility
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
 

Author Comment

by:adrian7474
Comment Utility
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
 
LVL 5

Expert Comment

by:gyoreg
Comment Utility
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
 
LVL 5

Assisted Solution

by:gyoreg
gyoreg earned 250 total points
Comment Utility
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
 
LVL 5

Expert Comment

by:gyoreg
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 5

Expert Comment

by:gyoreg
Comment Utility
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
 
LVL 3

Expert Comment

by:Peter_Werner
Comment Utility
You may want to explain your real target, like "display the number of days left until a given date using custom images".
0
 

Author Comment

by:adrian7474
Comment Utility
Ok, how do i make it count down as opposed to count up?
0
 
LVL 3

Expert Comment

by:Peter_Werner
Comment Utility
$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
 

Author Comment

by:adrian7474
Comment Utility
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
 
LVL 3

Accepted Solution

by:
Peter_Werner earned 250 total points
Comment Utility
<?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
 
LVL 3

Expert Comment

by:Peter_Werner
Comment Utility
BTW, what are on those pictures, not numbers from 60 to 01?
0
 

Author Comment

by:adrian7474
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now