Jump to content

[SOLVED] 'php-include' - call this file every 30 seconds?


Recommended Posts

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?

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.

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)

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? 

<?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.

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.