...

View Full Version : Image Rotator - Need Help with php or java code



matafy
05-01-2009, 01:50 AM
I have been looking for an image rotator that swaps images by date and time from my severs time set. Similar to what internet radio stations use for their DJs. I have yet to find the right one that works. Please forgive me for the example will be long.

Example:

Sun - 10 March 2009
0000 - 0900 = "image/DJ-00.png"
0900 - 1000 = "image/DJ-1.png"
1000 - 1100 = "image/DJ-2.png"
1100 - 1200 = "image/DJ-3.png"
1200 - 1300 = "image/DJ-4.png"
1300 - 1400 = "image/DJ-5.png"
1400 - 1500 = "image/DJ-6.png"
1500 - 1600 = "image/DJ-7.png"
1600 - 1700 = "image/DJ-8.png"
1700 - 1800 = "image/DJ-9.png"
1800 - 1900 = "image/DJ-10.png"
1900 - 2000 = "image/DJ-11.png"
2000 - 2100 = "image/DJ-12.png"
2100 - 0000 = "image/DJ-00.png"

Mon - 11 March 2009
0000 - 0900 = "image/DJ-00.png"
0900 - 1000 = "image/DJ-13.png"
1000 - 1100 = "image/DJ-14.png"
1100 - 1200 = "image/DJ-15.png"
1200 - 1300 = "image/DJ-16.png"
1300 - 1400 = "image/DJ-17.png"
1400 - 1500 = "image/DJ-18.png"
1500 - 1600 = "image/DJ-19.png"
1600 - 1700 = "image/DJ-20.png"
1700 - 1800 = "image/DJ-21.png"
1800 - 1900 = "image/DJ-22.png"
1900 - 2000 = "image/DJ-23.png"
2000 - 2100 = "image/DJ-24.png"
2100 - 0000 = "image/DJ-25.png"

Tue - 12 March 2009
0000 - 0900 = "image/DJ-00.png"
0900 - 1000 = "image/DJ-26.png"
1000 - 1100 = "image/DJ-27.png"
1100 - 1200 = "image/DJ-28.png"
1200 - 1300 = "image/DJ-29.png"
1300 - 1400 = "image/DJ-30.png"
1400 - 1500 = "image/DJ-31.png"
1500 - 1600 = "image/DJ-32.png"
1600 - 1700 = "image/DJ-33.png"
1700 - 1800 = "image/DJ-34.png"
1800 - 1900 = "image/DJ-35.png"
1900 - 2000 = "image/DJ-36.png"
2000 - 2100 = "image/DJ-37.png"
2100 - 0000 = "image/DJ-00.png"

Wed - 13 March 2009
0000 - 0900 = "image/DJ-00.png"
0900 - 1000 = "image/DJ-38.png"
1000 - 1100 = "image/DJ-39.png"
1100 - 1200 = "image/DJ-40.png"
1200 - 1300 = "image/DJ-41.png"
1300 - 1400 = "image/DJ-42.png"
1400 - 1500 = "image/DJ-43.png"
1500 - 1600 = "image/DJ-44.png"
1600 - 1700 = "image/DJ-45.png"
1700 - 1800 = "image/DJ-46.png"
1800 - 1900 = "image/DJ-47.png"
1900 - 2000 = "image/DJ-48.png"
2000 - 2100 = "image/DJ-49.png"
2100 - 0000 = "image/DJ-00.png"

Thur - 14 March 2009
0000 - 0900 = "image/DJ-00.png"
0900 - 1000 = "image/DJ-50.png"
1000 - 1100 = "image/DJ-51.png"
1100 - 1200 = "image/DJ-52.png"
1200 - 1300 = "image/DJ-53.png"
1300 - 1400 = "image/DJ-54.png"
1400 - 1500 = "image/DJ-55.png"
1500 - 1600 = "image/DJ-56.png"
1600 - 1700 = "image/DJ-57.png"
1700 - 1800 = "image/DJ-58png"
1800 - 1900 = "image/DJ-59.png"
1900 - 2000 = "image/DJ-60.png"
2000 - 2100 = "image/DJ-61.png"
2100 - 0000 = "image/DJ-00.png"

Fri - 15 March 2009
0000 - 0900 = "image/DJ-00.png"
0900 - 1000 = "image/DJ-62.png"
1000 - 1100 = "image/DJ-63.png"
1100 - 1200 = "image/DJ-64.png"
1200 - 1300 = "image/DJ-65.png"
1300 - 1400 = "image/DJ-66.png"
1400 - 1500 = "image/DJ-67.png"
1500 - 1600 = "image/DJ-68.png"
1600 - 1700 = "image/DJ-69.png"
1700 - 1800 = "image/DJ-70.png"
1800 - 1900 = "image/DJ-71.png"
1900 - 2000 = "image/DJ-72.png"
2000 - 2100 = "image/DJ-73.png"
2100 - 0000 = "image/DJ-00.png"

Sat - 16 March 2009
0000 - 0900 = "image/DJ-00.png"
0900 - 1000 = "image/DJ-74.png"
1000 - 1100 = "image/DJ-75.png"
1100 - 1200 = "image/DJ-76png"
1200 - 1300 = "image/DJ-77.png"
1300 - 1400 = "image/DJ-78.png"
1400 - 1500 = "image/DJ-79.png"
1500 - 1600 = "image/DJ-80.png"
1600 - 1700 = "image/DJ-81.png"
1700 - 1800 = "image/DJ-82.png"
1800 - 1900 = "image/DJ-83.png"
1900 - 2000 = "image/DJ-84.png"
2000 - 2100 = "image/DJ-85.png"
2100 - 0000 = "image/DJ-00.png"

and all over the following week... Keep in mind that every day will have a different image. And yes, I do have 365 images to swap.

Can anyone help?

Thanks in advance,
Frank

venegal
05-01-2009, 04:00 AM
Naming your image files according to the date and time they should be shown makes things much simpler. For example, if you name the image that should be shown from 11 to 12 on 13 March 2009 like DJ-11130309.png, you can use


$image = (date(H) < 9 || date(H) > 21) ? "DJ-00.png" : "DJ-".date(Hdmy).".png";

and $image will always hold the right filename.

matafy
05-01-2009, 06:18 AM
This makes sense. Thank you very much. What would the full code look like using your example if?

Would it look something like this for Sun, 10 March 2009:

<?PHP
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-09100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-10100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-11100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-12100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-13100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-14100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-15100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-16100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-17100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-18100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-19100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-20100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-21100309.png" : "DJ-".date(Hdmy).".png";
$image = (date(H) < 21 || date(H) > 9) ? "img/DJ-00.png" : "DJ-00.png";
?>

venegal
05-01-2009, 02:04 PM
No, it's much simpler than that. It's really just the one line I gave you, for all the days and all the hours.

I've written it with ternary conditional notation to make it shorter; maybe it's easier to understand if I wrote it more verbosely:



// Is it between 21 and 9 o'clock right now?
if (date(H) < 9 || date(H) > 21) {
// then make $image the standard image name "DJ-00.png"
$image = "DJ-00.png";
} else {
// else make $image the current image name in the format "DJ-[hhddmmyy].png"
// (the same format according to which you named your image files)
$image = "DJ-".date(Hdmy).".png";
}

matafy
05-01-2009, 05:28 PM
Oh, I understand it now. Now I guess the only question I have is how would I call the image from my server? This is my directory...

my server/my webpage/img

Wouldn't I have to put my directory in the script.

$image = "img/DJ-00.png";
$image = "img/DJ-".date(Hdmy).".png";

venegal
05-02-2009, 03:36 AM
Yeah, how you handle your image path is completely at your discretion. If it's a fixed path that won't change, you can put it right into that snippet like


$image = (date(H) < 9 || date(H) > 21) ? "img/DJ-00.png" : "img/DJ-".date(Hdmy).".png";

or when it might be changed later on you may want leave it variable like


$imagePath = "img/";
$image = (date(H) < 9 || date(H) > 21) ? $imagePath."DJ-00.png" : $imagePath."DJ-".date(Hdmy).".png";


There are 50 ways to do this, depending on how much abstraction you want put into that for later changa- and portability.

matafy
05-02-2009, 04:48 AM
Would this script be good enough if I just wanted to repeat the same images around the clock for that day?

<?PHP

$imagePath = "img/";

if (date(H)-8 < 9 || date(H)-8 > 21) {

$imagePath."DJ-00.png";

} else {

$imagePath."DJ-".date(DH).".png";
}

?>



The file names would now be "DJ-Sun09.png"
"DJ-Sun10.png"
"DJ-Sun11.png"
"DJ-Sun12.png"
"DJ-Sun13.png"
"DJ-Sun14.png"
"DJ-Sun15.png"
"DJ-Sun16.png"
"DJ-Sun17.png"
"DJ-Sun18.png"
"DJ-Sun19.png"
"DJ-Sun20.png"
"DJ-Sun21.png"

I added the GMT to reflect California's time. I changed the date format so I can repeat the images. The calculation of images I would have to upload was bigger than I originally thought. So, I decided to repeat the same images week after week. Does this look right?

venegal
05-02-2009, 05:13 AM
It looks like it should work, except you are evaluating those strings without doing anything with them. You will want to either store the filename with path in some variable like



$imagePath = "img/";
if (date(H)-8 < 9 || date(H)-8 > 21) {
$imagePathAndFilename = $imagePath."DJ-00.png";
} else {
$imagePathAndFilename = $imagePath."DJ-".date(DH).".png";
}
echo $imagePathAndFilename;


or make it a function returning the filename with path like


function currentImage(){
$imagePath = "img/";
if (date(H)-8 < 9 || date(H)-8 > 21) {
return $imagePath."DJ-00.png";
} else {
return $imagePath."DJ-".date(DH).".png";
}
}
echo currentImage();

matafy
05-02-2009, 07:05 AM
I used both of the scripts and I got the text:

img/DJ-Fri22.png

and not the actual image.

venegal
05-02-2009, 01:44 PM
Well it's really hard to tell someones proficiency and how much you have to spell things out. I echoed the string out so you can see the usage of the two examples. If you want to output the actual picture, you'll have to put the filename in some html, like


<img src="<?php echo currentImage();?>" alt="DJ image" />

matafy
05-03-2009, 01:24 AM
:thumbsup: Thanks once again and Thank you for your time. I'm sure you get annoyed when you have to think about something as simple as this. I appreciate all your help. I ended using folders to help me sort out the images.

Here is what I ended up with.

function currentImage(){
$imagePath = "img/";
if (date(H) < 09 || date(H) > 21) {
return $imagePath."".date(D)."/".date(H).".png";
} else {

return $imagePath."".date(D)."/".date(H).".png";
}
}

<img src="<?php echo currentImage();?>">

This scripted worked out perfect for me.

Would I be able to swap html or php files with the same method?

venegal
05-03-2009, 01:39 AM
That's quite alright; if I were getting annoyed, I would simply stop replying.

In your code the if statement is superfluous -- both the if and the else return the same thing. Originally the if statement was there in order to return a standard image between 21 and 9 o'clock. If you now really do have an image for every hour (even at night), you can kick the if/else.

Also concatenating an empty string literal into your filename doesn't make much sense. Use $imagePath.date(D) instead of $imagePath."".date(D).

And I wish you hadn't changed the html I gave you. In order for your code to be valid, <img>s need to have an 'alt' property, and they must be properly closed (/> instead of just >)

Regarding your questions: Like you did with the image filenames, you can certainly put the names of html- or php files into a php variable with the same method. The real question is, how you want to use that variable. With the image filenames, you put them into <img> tags in order to show them in the browser. What do you want to do with the html and php files? If they should be links, that should change with every hour, you could echo the variable in the href property of an <a> tag.

matafy
05-03-2009, 03:51 AM
Your right! One thing though. The $imagePath.date(D) instead of $imagePath."".date(D). Didn't seem to work for me. I first put the .date(D) and nothing. Then I put "".date and it targeted the folder I was looking for.

Well, to be honest. This is what I'm trying to accomplish. If you go to latalkradio.com and click right on through till you get to the who is DJing. They have a rotating image along with the rotating ip stream. I like that and would like to use that same method for my website.

I figured since you helped me tremendously with this script you can show me how I can do the very same thing they have... If it isn't to much to ask, I would greatly appreciate that.

venegal
05-03-2009, 02:14 PM
Yeah, if you just put .date(D) in your code, it won't work. "." is the concatenation operator, it has to be between two strings you want to concatenate. Thus putting "".date(D) seemed to solve your problem, but you really should have just put date(D) there.

You already have the rotating image, so all that's left is the rotating stream. Well I guess you won't use the same method for that, but always use the same link and let the .php responsible for the stream decide, which one to serve. But I can't tell you any specifics unless you elaborate on how you are planning on serving that stream.

matafy
05-03-2009, 10:58 PM
You know what? I found my problem. I didn't put the "." in the script. I accidently left it out. This is what it looked like when I used it $imagePathdate(D) It now works just like you said.

Ooops!

As far as the ip stream. I'm still working on whether to set my own cpu as a server to stream or look for some type of shoutcast hosting provider, but till then here is what I'm looking at.

Monday Schedule 1:

0900 - 1000 = Dj 1 (on ip 127.0.0.2:8000) ip provided by the DJ's cpu at home
1000 - 1100 = Dj 2 (on ip 127.0.0.3:8000)
1100 - 1200 = Dj 3 (on ip 127.0.0.4:8000)
1200 - 1300 = Dj 4 (on ip 127.0.0.5:8000)
1300 - 1400 = Dj 5 (on ip 127.0.0.6:8000)
1400 - 1500 = Dj 6 (on ip 127.0.0.6:8000)
1500 - 1600 = Dj 7 (on ip 127.0.0.7:8000)
1600 - 1700 = Dj 8 (on ip 127.0.0.8:8000)
1700 - 1800 = Dj 9 (on ip 127.0.0.9:8000)
1800 - 1900 = Dj 10 (on ip 127.0.1.0:8000)
1900 - 2000 = Dj 11 (on ip 127.0.1.1:8000)
2000 - 2100 = Dj 12 (on ip 127.0.1.2:8000)

Monday Schedule: Repeated

2100 - 0900 = Djs (on ip 127.1.2.3:8000)

------------------------------------------

Tuesday Schedule 2:

0900 - 1000 = Dj 13 (on ip 127.0.1.3:8000)
1000 - 1100 = Dj 14 (on ip 127.0.1.4:8000)
1100 - 1200 = Dj 15 (on ip 127.0.1.5:8000)
1200 - 1300 = Dj 16 (on ip 127.0.1.6:8000)
1300 - 1400 = Dj 17 (on ip 127.0.1.7:8000)
1400 - 1500 = Dj 18 (on ip 127.0.1.8:8000)
1500 - 1600 = Dj 19 (on ip 127.0.1.9:8000)
1600 - 1700 = Dj 20 (on ip 127.0.2.0:8000)
1700 - 1800 = Dj 21 (on ip 127.0.2.1:8000)
1800 - 1900 = Dj 22 (on ip 127.0.2.2:8000)
1900 - 2000 = Dj 23 (on ip 127.0.2.3:8000)
2000 - 2100 = Dj 24 (on ip 127.0.2.4:8000)

Tuesday Schedule: Repeated

2100 - 0900 = Djs (on ip 127.1.2.3:8000)

------------------------------------------

And so on for the rest of the week. Sundays would be prerecorded randomly selected files starting at 0900 to Monday 0900. I should have a total of 72 projected Djs by 2100 Saturday.

I just may consider using a webcam.

venegal
05-03-2009, 11:16 PM
Well that doesn't seem feasible. You can't use local IPs if you want people to reach you, and how will (or why should) you provide a different public IP for every hour of the week?

If you have trouble implementing your stream, please seek help in another thread, as this doesn't have much to do with your orignial problem, which seems solved to me.

matafy
05-03-2009, 11:56 PM
It's not so much about my stream. The way I have seen it work is like this. The DJ is at home streaming his music. I will be providing my webpage for him to show his/her stuff. All the DJ has to do is give me his ip stream, then put it into my hourly rotation of the DJs choice, if it's not already taken. This is why I kind of wanted to know how I can rotate .php files. The streamers ip would be enbeded to that .php page. The ips I used where examples of the djs stream I would rotate. Would this be possible?

My website visitor would have a choice on what Dj he/she would like to hear.

venegal
05-04-2009, 01:16 AM
Alright then. If the DJ is providing the stream from is own server, all you have to do is provide the right link to that server.

First you have to set up some an array that associates an hour with the proper ip, which in your example would look like:


$ips = array(
"default" => "127.1.2.3",
"Mon09" => "127.0.0.2",
"Mon10" => "127.0.0.3",
//and so on...
"Tue09" => "127.0.1.3",
//and so on...
);

After


$current_ip = (date(H) < 9 || date(H) > 21) ? $ips['default'] : $ips[date(DH)];

the variable $current_ip will hold the right ip for the current hour.

You can use that to link to the stream, like


<a href="http://<?php echo $current_ip ?>:8000">Stream</a>

matafy
05-04-2009, 01:54 AM
Nice! Thanks Venegal. So, if the Djs port happens to be different then the 8000. Would it be safe to change the array settings to:

"default" => "127.1.2.3:80",
"Mon09" => "127.0.0.2:8080",
"Mon10" => "127.0.0.3:2121",

and the link to:

<a href="http://<?php echo $current_ip ?>">Stream</a>

venegal
05-04-2009, 02:04 AM
Sure, that's ok. I don't know though how exactly those streams will work, and whether it will suffice to just link to them in the browser.

matafy
05-04-2009, 03:15 AM
Cool, now comes the questions.

Would I be able to use both the image rotator and the ip rotator in the same page?

venegal
05-04-2009, 03:45 AM
Why not? You can do whatever you want, as long as you don't have a variable collision (which you currently don't have).

matafy
05-04-2009, 05:26 AM
Please don't beat me up if I didn't write this correctly... I'm learning php for the first time. And I'm sure you already know this. Be gentle!

<?PHP

function currentImage(){
$imagePath = "img/";
return $imagePath.date(D)."/".date(H).".png";
}

$ips = array(
"default" => "127.1.2.3:8080",
"Mon09" => "127.0.0.2:8000",
"Tue09" => "127.0.1.3:2121",

);

function currentIP(){
if (date(H)-8 < 9 || date(H)-8 > 21) {
$ips['default'];
} else {
$ips[date(DH)];
}

?>

<img src="<?php echo currentImage();?>" alt="Dj-Image">
<a href="http://<?php echo $current_ip ?>">Stream</a>



As for how I was going to make this work is like this:

<object id="Player" width="100%" height="64" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" align="middle" >
<param name="URL" value="<?php echo $current_ip ?>">
<param name="DefaultFrame" value="mainFrame">
<param name="AutoStart" value="<?php print $autostart; ?>">
<param name="playCount" value="1">
<param name="loop" value="1">
<param name="windowlessVideo" value="0">
<param name="Stretchtofit" value="1">
<param name="enableContextMenu" value="false">
<param name="showstatusbar" value="true">
<embed TYPE="video/x-ms-asf-plugin" PLUGINSPAGE="http://www.microsoft.com/Windows/
Downloads/Contents/Products/MediaPlayer/" SRC="<?php print $play_file; ?>"
PLAYCOUNT="1" LOOP="1" NAME="Player" WIDTH="100%" HEIGHT="64" showstatusbar="1">
</object>

venegal
05-04-2009, 01:18 PM
Well, almost.

First of all, please put your snippets into php tags in forum posts, so they look all pretty like mine.

Secondly, the function currentIP does not return any value. We already hat that same problem with currentImage, so I am going to suppose you don't know what "return" does: It immediately ends the execution of the current function and, well, returns a value.

The way you wrote it, echoing currentIP(); will not do anything, because the function currentIP does not return anything to the echo command, so there will not be any output. (There are languages, which automatically return the value of the last statement. In such a language, the way you wrote it would work, but PHP is not one of them.)

Furthermore you forgot the function's closing brace. Always indent your code for better readability, and so that those mistakes won't happen.

All in all, currentIP should look like this:


function currentIP(){
if (date(H)-8 < 9 || date(H)-8 > 21) {
return $ips['default'];
} else {
return $ips[date(DH)];
}
}


If you got that, please read up on the ternary conditional operator. I have been using it a few times in my answers to you; it can greatly simplify some kinds of conditionals.

Your function currentIP could look as simple as this:


function currentIP(){
return (date(H) < 9 || date(H) > 21) ? $ips['default'] : $ips[date(DH)];
}


Alright. Now if what you wrote at the end is how you are going to make it work, you can of course kick the <a>. That was just a placeholder for the <object> bit you got there.

Inside the <object> you echo, or rather print, the two variables $autostart and $play_file. They should be set somewhere in your code before doing that.

And, before asking if your code will work, just try if it does. If it does not, and you are not able to fix it, you can always ask, but please at least try to do your debugging on your own.

matafy
05-04-2009, 05:25 PM
This worked out. Thanks for your time venegal. You have been very helpful. I greatly appreciate all your help. I will be back soon to learn more php scripting. As for now, I have some chours I have neglected. Chat with you soon...

matafy
06-14-2009, 11:13 AM
Ok, I'm back! and I have one more questions. Now that I established this.


<?PHP
$ips = array(
"default" => "127.1.2.4:8080",
"Mon09" => "127.1.2.5:8080",
"Tue09" => "127.1.2.6:8080",
);

function currentIP(){
if (date(H)-8 < 9 || date(H)-8 > 21) {
return $ips['default'];
} else {
return $ips[date(DH)];
}
}
?>

and I would call it with:


<?php echo $current_ip ?>

How would I do the same with a quote using the same time format? :)
Would be able to just replace the $ips with $quotes and if so, what function would I use?

Thank,
Frank



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum