Deprecated: The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence in /home/iano/public_html/tpforums-vb5/forum/includes/class_core.php on line 5842

PHP Warning: Use of undefined constant MYSQL_NUM - assumed 'MYSQL_NUM' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: Use of undefined constant MYSQL_BOTH - assumed 'MYSQL_BOTH' (this will throw an Error in a future version of PHP) in ..../includes/init.php on line 165

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 588

PHP Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in ..../includes/functions_navigation.php on line 612

PHP Warning: Use of undefined constant archive_postsperpage - assumed 'archive_postsperpage' (this will throw an Error in a future version of PHP) in ..../archive/index.php on line 456
[PHP] Random Avatar/Image [Archive] - Forums

PDA

View Full Version : [PHP] Random Avatar/Image



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!

Theftz
03-19-2007, 03:25 PM
Thanks alot! Really appriciated!
Will help me in my image verification script =D

Nevski
03-22-2007, 04:54 PM
Thanks using it on my forum :D Post more sripts like these :P