Help - Search - Members - Calendar
Full Version: Random Avatar Script Not Working?
Invision Power Services > Community Forums > Community Web Design and Coding
Shadow Wolf
It used to work but now it isn't working. I'm not sure what would of changed. There doesn't appear to have been any server updates but it just completely stopped working. Here is the code I was using:
CODE
<?

//--------------------------------
// Random Image Script v2.0 by InvisionAdds(tm)
// Copyright 2003-2004 Chris Griego [cgriego]
// http://www.invisionadds.com/

$imgs = array(
//==============================
// Setup the information about our random images
//==============================

// Image Name and path
"/home/protocul/www/images/avatars/angel.gif",
"/home/protocul/www/images/avatars/chobits.gif",
"/home/protocul/www/images/avatars/get_backers.gif",
"/home/protocul/www/images/avatars/naruto00.gif",
"/home/protocul/www/images/avatars/naruto01.gif",
"/home/protocul/www/images/avatars/s_champloo.gif",
"/home/protocul/www/images/avatars/ah_my_goddess01.gif",
"/home/protocul/www/images/avatars/ah_my_goddess34.gif",
"/home/protocul/www/images/avatars/angel_sanctuary02.gif",
"/home/protocul/www/images/avatars/divers13.gif",
"/home/protocul/www/images/avatars/fma04.gif",
"/home/protocul/www/images/avatars/hellsing16.gif",
"/home/protocul/www/images/avatars/nurse_komugi04.gif",
"/home/protocul/www/images/avatars/nurse_komugi07.gif",
"/home/protocul/www/images/avatars/tenjou02.gif",
"/home/protocul/www/images/avatars/tenjou03.gif",
"/home/protocul/www/images/avatars/tenjou07.gif",
"/home/protocul/www/images/avatars/tenjou15.gif"

//==============================
// There is no need to edit anything below this line.
//==============================
);

// Seed the random number generator
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
srand($mtime);

// Find the total images
$imgs_total = count($imgs);

// Choose a random number from 0 to end of array
$random = rand(0, ($imgs_total - 1));

$content_type = "";

if (strtolower(substr($imgs[$random], -3)) == "gif")
{
    $content_type = "image/gif";
} elseif (strtolower(substr($imgs[$random], -3)) == "png") {
    $content_type = "image/png";
} elseif (strtolower(substr($imgs[$random], -3)) == "jpg" || strtolower(substr($imgs[$random], -4)) == "jpeg") {
    $content_type = "image/jpeg";
} else {
    // Error...
    exit();
}

// Make sure the user is going to actually keep retreiving the image (an image in cache is not random)
@header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
@header("Cache-Control: no-cache, must-revalidate");
@header("Pragma: no-cache");

// Required, tells the browser what time of file we're sending it's way.
@header("Content-Type: ".$content_type);

// open the picture file
@readfile($imgs[$random]);

// End Random Image script
//--------------------------------

?>
JeremyM
For the path to the image, wouldn't you want to use the url and not the path? I know they say path but wouldn't the url be the more logical way of doing it?

EDIT - That or say your php file is in:

/my/directory

And your images are in:

/my/directory/images/avatars

Then the path would be:

/images/avatars
Shadow Wolf
It was originally written with the direct path to the images. The random_image.gif file which is the .php file saved as random_image.gif is actually saved in that same directory. I have tried using just a direct URL but that doesn't work.

When it stopped working, all it does is return the URL to the random_image.gif as:
http://www.pcxdesigns.com/images/avatars/random_image.gif

It just displays it, instead of actually going through the script and showing the image.
Scott H.
If you remove some of the @ before commands, particularly the readfile one, it might give more messages which can help point to the problem.

Another small thing which I would fix is changing <? to <?php - just to stop any problems there.
Shadow Wolf
QUOTE(Scott H @ Aug 2 2006, 06:46 AM) *
If you remove some of the @ before commands, particularly the readfile one, it might give more messages which can help point to the problem.

Another small thing which I would fix is changing <? to <?php - just to stop any problems there.
I have some of them as <? and some as <?php, neither appear to change what is happening. I have updated it though and still no change. Keep in mind that I have had 6-8 random avatar's being used and they have worked for a year with no changes made to the file. Then it just stopped working. Although it is possible the server could of done something to change it, it isn't working on two different servers hosted by two different companies.

CODE
// Make sure the user is going to actually keep retreiving the image (an image in cache is not random)
@header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
@header("Cache-Control: no-cache, must-revalidate");
@header("Pragma: no-cache");
Removing the @ still displays the same thing, it just displays the following link:
http://www.pcxdesigns.com/images/avatars/random_image.gif

CODE
// Required, tells the browser what time of file we're sending it's way.
@header("Content-Type: ".$content_type);
Removing the @ still displays the same thing, it just displays the following link:
http://www.pcxdesigns.com/images/avatars/random_image.gif

CODE
// open the picture file
@readfile($imgs[$random]);
When I remove the @ from here I get a different message:
"The image “http://www.pcxdesigns.com/images/avatars/random_image.gif” cannot be displayed, because it contains errors."

Which unfortunately still leaves me at square one. The script looks correct to me. It has been working, there haven't been any changes to it... so I'm not sure what it is detecting as errors.
.William
This one works. rolleyes.gif
Mark.
If you want my one, PM me, my one automatically grabs all the images in one folder. The only problem with my one is all the images have to be the same size (in pixels, not data size) and have to have the same extension.
Shadow Wolf
QUOTE(.William @ Aug 2 2006, 08:47 AM) *
This one works. rolleyes.gif
The main reason I used the one above was because after you set everything, you renamed it to random_image.gif. Not all message boards will let you link to a .php file as a link so it has to end in a .gif/.jpg extension to be recognized as an image... even though it is just a script. I can use a rid.php random image generator, but that will just random every image in the same directory... but with it I have the issue that for it to work it has to be link to rid.php insetad of a rid.gif (doesn't work).

Everything looks like it would work, but it doesn't. I uploaded it and I get the same issue that I have been having. So I guess there has to be some change that the hosts did to prevent it from working. Regular .php items don't seem to have any issues. So I'm not sure what changes they could make on the server which makes the script only output the URL.

I uploaded the directory to:
http://wolfbrigade.guildregister.com/temp/randomav/

Instead of an image though, I just get:
http://wolfbrigade.guildregister.com/temp/randomav/ or
http://wolfbrigade.guildregister.com/temp/randomav/index.php
Scott H.
Are you using an apache server? Put this code in the .htaccess in the folder where your script is:
CODE
AddType application/x-httpd-php gif
if it isn't already there. Ideally, this should be in a folder of its own, since all .gifs will be treated as .php once this code has been used.
Shadow Wolf
Yes I have tried that before, it didn't change anything.
Shadow Wolf
I've checked permissions on the directory, they have readable permissions of 755. I have even tried 777 but that hasn't changed anything.

Renaming the script to php, I get the same result it will just output the URL. If I remove some of the @ so I can output tests I get the message, "The image "http://www.pcxdesigns.com/images/avatars/random_image.php" cannot be displayed because it contains errors."

But I don't see any errors in the script. I tried .William's script and it does the same thing. All that it displays is the URL link, no image. His looks correct and it should work... but it isn't either.
Shadow Wolf
Everything is working now. It appears it was server side that was causing the problem, nothing with the script at all. After they made modifications to mod_security everything worked fine again as it was before. Thank you for the help.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.