mellojoe Posted October 26, 2007 Share Posted October 26, 2007 I have a .php file that I call through an include in the main page. It is a server-side file that pulls in a random image from the photo galleries. What I'd like to do is simply set it up so that it calls that include file once every so often, sort of as an auto-refresh. I've spent the last week searching the internet, but I can't seem to find a solution to this problem. But, then again, my knowledge and understanding of PHP is limited. I have tried different solutions, but now I realize the best one needs to be simply setting up a timer that as it counts-down to zero it runs the 'include()' function. This seems to be simple, but I just can't figure it out. Thanks in advance. What do you suggest? Quote Link to comment https://forums.phpfreaks.com/topic/74926-solved-php-include-call-this-file-every-30-seconds/ Share on other sites More sharing options...
premiso Posted October 26, 2007 Share Posted October 26, 2007 Using AJAX. You set a div with innerhtml tag, when the timer hits 30 seconds ajax makes a call to a script and grabs the next random image url, set that as the new innerhtml and you are good. Quote Link to comment https://forums.phpfreaks.com/topic/74926-solved-php-include-call-this-file-every-30-seconds/#findComment-378873 Share on other sites More sharing options...
mellojoe Posted October 26, 2007 Author Share Posted October 26, 2007 I don't have access to the .php file that is called. I just have the <include(file.php)>, and the server's file handles the image code. I can't get into that. So, I won't know the name of the image file or anything that is called. All I can do is to call the include. Quote Link to comment https://forums.phpfreaks.com/topic/74926-solved-php-include-call-this-file-every-30-seconds/#findComment-378883 Share on other sites More sharing options...
kratsg Posted October 26, 2007 Share Posted October 26, 2007 You'll love this. Make the php file output the random image url. At the top of the php file, add a header refresh at the top for every 30 seconds to re-run the script, like so. header('Content-type: image/png');//we want all images outputted to be png header( 'refresh: 30; url=YOURURLHERE' );//ADD SCRIPT URL!!!! //some code to determine random image $size = getimagesize($filename); $width = $size[0];//we need this $height = $size[1];//and this to copy the image $im = imageCreate($width,$height); imagecopy($im,$filename,0,0,0,0,$width,$height);//copy from the top left corner of $filename to $im imagePNG ($im);//send the image data imageDestroy ($im);//destroy the image data so we don't worry about memory Then, on the page that you want to display the random image: <img src="toyourphpfile.php"> Now, the img tag is a dynamically changing tag, javascript can change the src of it, and a new image auto-display. If you want to see a new random image, just link to the php file as mentioned above which outputs the image data for you. the imagecopy() function is a smart way of copying the current image onto one that you're creating. Test it out (this requires that you have the GD Image Library of PHP) Quote Link to comment https://forums.phpfreaks.com/topic/74926-solved-php-include-call-this-file-every-30-seconds/#findComment-378895 Share on other sites More sharing options...
LiamProductions Posted October 26, 2007 Share Posted October 26, 2007 You could do a for loop and include the images then put a 30 second sleep then in 30 seconds it will do the next loop Quote Link to comment https://forums.phpfreaks.com/topic/74926-solved-php-include-call-this-file-every-30-seconds/#findComment-378912 Share on other sites More sharing options...
GingerRobot Posted October 26, 2007 Share Posted October 26, 2007 You could do a for loop and include the images then put a 30 second sleep then in 30 seconds it will do the next loop Erm, no you couldn't. Quote Link to comment https://forums.phpfreaks.com/topic/74926-solved-php-include-call-this-file-every-30-seconds/#findComment-378919 Share on other sites More sharing options...
mellojoe Posted October 26, 2007 Author Share Posted October 26, 2007 Here's the deal. I'm currently using Gallery1 for my photo galleries. They have included a .php file which generates a random image from all of the galleries. All I have to do to get the random image on any page is to use the <php include()>. I do not have access to edit that file, so I am stuck with only manipulating how I call that file. You'll love this. Make the php file output the random image url. At the top of the php file, add a header refresh at the top for every 30 seconds to re-run the script, like so. header('Content-type: image/png');//we want all images outputted to be png header( 'refresh: 30; url=YOURURLHERE' );//ADD SCRIPT URL!!!! //some code to determine random image $size = getimagesize($filename); $width = $size[0];//we need this $height = $size[1];//and this to copy the image $im = imageCreate($width,$height); imagecopy($im,$filename,0,0,0,0,$width,$height);//copy from the top left corner of $filename to $im imagePNG ($im);//send the image data imageDestroy ($im);//destroy the image data so we don't worry about memory Then, on the page that you want to display the random image: <img src="toyourphpfile.php"> Now, the img tag is a dynamically changing tag, javascript can change the src of it, and a new image auto-display. If you want to see a new random image, just link to the php file as mentioned above which outputs the image data for you. the imagecopy() function is a smart way of copying the current image onto one that you're creating. Test it out (this requires that you have the GD Image Library of PHP) This looks promising. But, I have no idea where to begin. Since I can't edit the .php file that generates the random image, I don't think this will work. Also, the imagesize and imagename... I won't know those since they are handled within the random.php that is created for my use from Gallery1. So, I'm stuck again, I think. I am guessing, now, that I need a basic function. refresh_image(); timer_countdown(30seconds); when timer_countdown== 0 call run_file(random.php) Something like this seems like it must be possible, huh? Quote Link to comment https://forums.phpfreaks.com/topic/74926-solved-php-include-call-this-file-every-30-seconds/#findComment-378924 Share on other sites More sharing options...
premiso Posted October 26, 2007 Share Posted October 26, 2007 <?php header('Content-type: image/png');//we want all images outputted to be png header( 'refresh: 30;' ); include('URLTORANDOMIMAGESCRIPTHERE'); ?> A modification to the above. That should work. Quote Link to comment https://forums.phpfreaks.com/topic/74926-solved-php-include-call-this-file-every-30-seconds/#findComment-378925 Share on other sites More sharing options...
kratsg Posted October 27, 2007 Share Posted October 27, 2007 <?php header('Content-type: image/png');//we want all images outputted to be png header( 'refresh: 30;' ); include('URLTORANDOMIMAGESCRIPTHERE'); ?> A modification to the above. That should work. Errr, that won't work. Two reasons: one, you don't have the imagecreate function, which means that the headers you're sending aren't gonna work. Two, you can't just include the URL because we do not know the exact output. Ahh, speaking of outputs, maybe it's time to bring the iFrame into play. <iframe id="image_holder" src="some_php_file.php" width="200" height="200"> some_php_file.php header("refresh: 30; url=some_php_file.php;"); include("The Random Image Script that you can't Edit"); Now, what happens is an iFrame is considered a dynamic browser window, similar to AJAX url loading inside a div. If the information in the iFrame changes, such as a php file refreshing, the iFrame will show this change. Quote Link to comment https://forums.phpfreaks.com/topic/74926-solved-php-include-call-this-file-every-30-seconds/#findComment-379108 Share on other sites More sharing options...
mellojoe Posted October 29, 2007 Author Share Posted October 29, 2007 As mush disdain as I've heard about iFrames, I've been hesitant to try them. But, this looks promising. I'll give it a shot. Quote Link to comment https://forums.phpfreaks.com/topic/74926-solved-php-include-call-this-file-every-30-seconds/#findComment-380328 Share on other sites More sharing options...
mellojoe Posted October 29, 2007 Author Share Posted October 29, 2007 Ok. This worked. Here is the exact code I'm using: I created a file called "image_refresher_file.php". The only code in that file is this: <?php header( 'refresh: 17;' ); ?> <style> img { border: solid 1px #000; } </style> <?php include('http://jpdimaggio.com/gallery/block-random_no_caption.php'); ?> And then in the main "index.php", I have included this little gem. <iframe id="image_holder" onLoad="calcHeight();" src="image_refresher_file.php" scrolling="no" frameborder="0"> </iframe> I found a quick way to set the height of the iframe to be the same as the picture height, since in my case I won't know what the height of the picture is, since I won't know what picture is being called. This little JS figures that out for me. That is what the "onLoad" is calling. <script language="JavaScript"> <!-- function calcHeight() { var the_height = document.getElementById('image_holder').contentWindow.document.body.scrollHeight; document.getElementById('image_holder').height = the_height; } //--> </script> It works like a CHAMP! Thank you all for your help with this. And thank you, specifically, to kratsg for suggesting the dreaded iFrame. I hate to admit it, but it does exactly what I need it to. THANKS! Quote Link to comment https://forums.phpfreaks.com/topic/74926-solved-php-include-call-this-file-every-30-seconds/#findComment-380407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.