Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

change image on timer php

Posted on 2010-09-02
15
Medium Priority
?
673 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 5
15 Comments
 
LVL 3

Expert Comment

by:Peter_Werner
ID: 33591124
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
ID: 33591203
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
ID: 33591472
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:adrian7474
ID: 33591642
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
ID: 33591648
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 1000 total points
ID: 33591694
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
ID: 33591715
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
 
LVL 5

Expert Comment

by:gyoreg
ID: 33591730
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
ID: 33591950
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
ID: 33592008
Ok, how do i make it count down as opposed to count up?
0
 
LVL 3

Expert Comment

by:Peter_Werner
ID: 33592068
$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
ID: 33592136
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 1000 total points
ID: 33592336
<?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
ID: 33592347
BTW, what are on those pictures, not numbers from 60 to 01?
0
 

Author Comment

by:adrian7474
ID: 33592378
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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
This video teaches users how to migrate an existing Wordpress website to a new domain.

664 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