Solved

change image on timer php

Posted on 2010-09-02
15
659 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
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 

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 250 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 250 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

622 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