Rotating/refreshing banner in php

Soldato
Joined
6 Jan 2006
Posts
3,423
Location
Newcastle upon Tyne
Not the best with php so I need your help please

What ever is going to be the easiest, rotating or changing on refresh.

Thanks for any help

Mark
 
I wrote this because it’s always getting bleedin’ asked in HG&P

:p

However its not for a sig in a forum but a banner on a web page.

Im nearly there but I think the problem is that its not an image but a php file.

Code:
<a href="', $scripturl, '"><img align="top" src="', $settings['images_url'], '/Banner/rotate.php" alt="Logo" /></a>';

Any ideas?
 
Mark M said:
However its not for a sig in a forum but a banner on a web page.

an image is an image. i don't think php cares what it is.... :p

i guess this is the problem?

Code:
src="', $settings['images_url'], '/Banner/rotate.php"

you can't have commas like that. this might work?

Code:
<a href="<?php echo $scripturl;?>"><img align="top" src="<?php echo $settings['images_url'].'/Banner/rotate.php';?>" alt="Logo" /></a>
 
Last edited:
well there's nothing i can do about that. they're your strings and arrays in that code. it's upto you as to how they resolve.... :p

the actual rotate.php code i linked to works when you embed directly in <img> tags. not much else i can say. :)

quite simply i tested by creating the rotate.php in a folder full of images. i then created an index.htm in the same folder containing this code...

Code:
<img src="rotate.php" alt="image" />

refresh the page and the images rotate. simple.:D
 
Last edited:
I can get the whole thing working on a test page just not in the page I want it!! Think I'll have to recode it a bit but as I said Im not too good with php :p
 
There's nothing to recode within the rotation script, it'll be your markup that is wrong.

Try posting some code, then we can help...
 
Yeah thats what I meant, will have to have a look at my pages code. Will post up the code if I dont get it to work thanks :)
 
Reviving the thread...

I have this HTML code at my homepage:

Code:
<img src="images/ads/rotate.php" alt="image" />

with images/ads/rotate.php as this:

Code:
<?php
	$files = glob('{*.PNG,*.png,*.JPG,*.jpg,*.GIF,*.gif}', GLOB_BRACE);
	readfile($files[array_rand($files)]);
?>

I have several .jpg files in the images/ads folder. I only get a red-x on the homepage.
 
The image properties (for the red-x) is http://www.[mydomain].com/images/ads/rotate.php which seems to be correct.

Per the pointers on Rob's site, I also have a .htaccess file with the following:

Code:
RewriteEngine On
RewriteRule ^.*$ rotate.php

If it helps, mydomain is mtariders.


Edit: running the rotate.php script directly returns a page of encrypted JPG code, not the JPG itself. This is the same result that occurred when I attempted to include the php script. Strange.
 
Last edited:
You need to specify the MIME type:

Code:
$files = glob('{*.JPG,*.jpg}', GLOB_BRACE);
header("Content-type: image/jpeg\r\n");
readfile($files[array_rand($files)]);

This'll only work for JPEGs of course. A more robust solution would be to dynamically determine the MIME type:

Code:
$files = glob('{*.PNG,*.png,*.JPG,*.jpg,*.GIF,*.gif}', GLOB_BRACE);
$file = $files[array_rand($files)];
if (is_array($imageInfo = getimagesize($file)))
{
	header("Content-type: $imageInfo[mime]\r\n");
}
readfile($file);
 
Last edited:
I tried both your options, both still return the same result - it doesn't seem to be able to decode the images.

I found that trying to access the images directly had the same results: just code. So I removed the .htaccess file and they now work properly.

Thank you very much for your help!
 
an image is an image. i don't think php cares what it is.... :p

i guess this is the problem?

Code:
src="', $settings['images_url'], '/Banner/rotate.php"

you can't have commas like that. this might work?

Code:
<a href="<?php echo $scripturl;?>"><img align="top" src="<?php echo $settings['images_url'].'/Banner/rotate.php';?>" alt="Logo" /></a>

You can use multiple parameters with echo, so if he's doing that, then the commas are ok.

echo 'sometstring', $var, 'somestring';
 
Well now. The image is showing up properly, but the rotate is not rotating. Every visit to the site reveals the same image.

I'm using the code:
Code:
$files = glob('{*.PNG,*.png,*.JPG,*.jpg,*.GIF,*.gif}', GLOB_BRACE);
$file = $files[array_rand($files)];
if (is_array($imageInfo = getimagesize($file)))
{
	header("Content-type: $imageInfo[mime]\r\n");
}
readfile($file);
I need to study more.

Edit: reverted back to the original code. Same issue.

Code:
<?php
$files = glob('{*.PNG,*.png,*.JPG,*.jpg,*.GIF,*.gif}', GLOB_BRACE);
readfile($files[array_rand($files)]);
?>
 
Last edited:
Back
Top Bottom