PDA

View Full Version : Image rotator [PHP]



wsg14
07-08-2008, 03:07 PM
<?php

$img[0] = "images/box_red.png";
$img[1] = "images/box_blue.png";
$img[2] = "images/box_purple.png";
$img[3] = "images/box_green.png";

$total = count($img) - 1;
$rand = rand(0,$total);

echo '<img src="$img[ $rand ]" border="0"';

?>That doesn't display what I want it to (images/box_xxxx.png). It displays the actual variable ($img[ $rand ]). +rep

(using an edited version L?KE's code)

Excellent
07-08-2008, 03:09 PM
<?php

$img[0] = "images/box_red.png";
$img[1] = "images/box_blue.png";
$img[2] = "images/box_purple.png";
$img[3] = "images/box_green.png";

$total = count($img) - 1;
$rand = rand(0,$total);

echo '<img src="$img[ $rand ]" border="0"';

?>That doesn't display what I want it to (images/box_xxxx.png). It displays the actual variable ($img[ $rand ]). +repOops, mod delete.

wsg14
07-08-2008, 03:09 PM
nvm. ^_^

Excellent
07-08-2008, 03:20 PM
You also forgot the '> to end the src.

MrCraig
07-08-2008, 03:27 PM
<?php

$img[0] = "images/box_red.png";
$img[1] = "images/box_blue.png";
$img[2] = "images/box_purple.png";
$img[3] = "images/box_green.png";

$total = count($img) - 1;
$rand = rand(0,$total);

echo '<img src="'.$img[ $rand ].'" border="0" />';

?>

wsg14
07-08-2008, 03:29 PM
Thanks Craig and Excellent.

Source
07-08-2008, 03:32 PM
if you want the .php file to be detected as an actual image so your can include it via <img src="..... just set the file as header("location:image.jpg");

MrCraig
07-08-2008, 03:34 PM
if you want the .php file to be detected as an actual image so your can include it via <img src="..... just set the file as header("location:image.jpg");

No.. that would redirect the url to "image.jpg"

wsg14
07-08-2008, 03:40 PM
I was just going to ask that, how would I do that?

--ss--
07-08-2008, 03:48 PM
I was just going to ask that, how would I do that?
I believe it's


header("Content-type: image/png");

wsg14
07-08-2008, 03:53 PM
The image “http://xxx.com/box/” cannot be displayed, because it contains errors.

I get that when using your code (--ss--).

--ss--
07-08-2008, 04:00 PM
I get that when using your code (--ss--).
Put it right at the top before anything else ;).

MrCraig
07-08-2008, 04:01 PM
you need to use php-gd commands to generate the image.

more simple to just leave it as you had originally.

wsg14
07-08-2008, 04:09 PM
you need to use php-gd commands to generate the image.

more simple to just leave it as you had originally.

I want to use it in my sig, so that's why I want to turn it into an image.

wsg14
07-08-2008, 04:28 PM
Nevermind, I figured it out.

Protege
07-08-2008, 05:07 PM
or look @ this

http://www.habboxforum.com/showthread.php?p=4881002#post4881002

Editing the other stuff out, you should get yourself an image rotation. :)

Invent
07-08-2008, 05:26 PM
<?php

$images = array( "lol", "wut", "dongs" );

$random = rand( 0, ( sizeof( $images ) - 1 ) );
$file = $images[ $random ] . '.gif';

$size = filesize( $file );

header( "Content-Type: image/gif" );

$f = fopen( $file, "r" );

while( $buf = fread( $f, $size ) )
{

print $buf;

}

fclose( $f );

?>


You could use something like that if you don't want to waste server resources generating an image using php gd everytime someone loads the image.

(You could easily just use file_get_contents but there was a reason I used fopen, lol).

Want to hide these adverts? Register an account for free!