Cameri
03-19-2007, 12:53 PM
In this tutorial I will show you how to make your avatar show random pictures with every page refresh.
You can either create an array with the path to the images where you have your collection of avatars, or use a php function that does something like that for you. The glob() function returns an array of string with the files that match a given pattern (This is not a regular expression, it's just a string that accepts wildcards like * for example).
$images = array(
'01.jpg',
'02.jpg',
'03.jpg'
);
//OR...
$images = glob("images/*.jpg");
Next we have to select a random entry in our array, we do that by using the array_rand() function which returns the index of the element in the array that we randomly selected.
$selected = array_rand($images);
After that, we have to send some headers to the browser so he thinks he's dealing with an image (in fact he is) and not with a plain text file or html file.
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Pragma: no-cache");
header("Content-Type: image/jpeg");
The first header tells the browser not to store or cache the file, among other things.
The second header tells the browser that this image already expired (noticed it's an old date?), this makes the browser try to look for a new one next time.
The third header makes the browser ask for a new version of the file EVEN if it has one in cache, shouldn't happen in the first place, but we can't risk it can we? :P
The third header tells the browser what contents the file has, and you may change it accordingly to the images that you've got. If it's png, it's image/png, for gif it's image/gif, etc.
After that we load the contents of the image and output them directly using the function readfile() like so:
readfile($images[$selected]);
Everything put together will look like this:
<?php
$images = array(
'01.jpg',
'02.jpg',
'03.jpg',
);
//$images = glob("*.jpg");
$selected = array_rand($images);
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Pragma: no-cache");
header("Content-Type: image/jpeg");
readfile($images[$selected]);
?>
To use it on forums, you have to put the filename one of this ways to fool the forum:
http://www.url.com/myimage.php/lala.jpg
or
http://www.url.com/myimage.php?lala=lala.jpg
And... Good luck!
You can either create an array with the path to the images where you have your collection of avatars, or use a php function that does something like that for you. The glob() function returns an array of string with the files that match a given pattern (This is not a regular expression, it's just a string that accepts wildcards like * for example).
$images = array(
'01.jpg',
'02.jpg',
'03.jpg'
);
//OR...
$images = glob("images/*.jpg");
Next we have to select a random entry in our array, we do that by using the array_rand() function which returns the index of the element in the array that we randomly selected.
$selected = array_rand($images);
After that, we have to send some headers to the browser so he thinks he's dealing with an image (in fact he is) and not with a plain text file or html file.
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Pragma: no-cache");
header("Content-Type: image/jpeg");
The first header tells the browser not to store or cache the file, among other things.
The second header tells the browser that this image already expired (noticed it's an old date?), this makes the browser try to look for a new one next time.
The third header makes the browser ask for a new version of the file EVEN if it has one in cache, shouldn't happen in the first place, but we can't risk it can we? :P
The third header tells the browser what contents the file has, and you may change it accordingly to the images that you've got. If it's png, it's image/png, for gif it's image/gif, etc.
After that we load the contents of the image and output them directly using the function readfile() like so:
readfile($images[$selected]);
Everything put together will look like this:
<?php
$images = array(
'01.jpg',
'02.jpg',
'03.jpg',
);
//$images = glob("*.jpg");
$selected = array_rand($images);
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Pragma: no-cache");
header("Content-Type: image/jpeg");
readfile($images[$selected]);
?>
To use it on forums, you have to put the filename one of this ways to fool the forum:
http://www.url.com/myimage.php/lala.jpg
or
http://www.url.com/myimage.php?lala=lala.jpg
And... Good luck!