• 0

Random image as Avatar


Question

I have 6 images that i would like to have one randomly selected each time someone loads a page for my avatar.

I am not sure if this is possible. however, i was hoping that perhaps i could set a php file as my avatar image on Neowin and then have it randomly select one of the 6 images to load.

If this is possible could someone help me out.

Link to comment
https://www.neowin.net/forum/topic/874286-random-image-as-avatar/
Share on other sites

10 answers to this question

Recommended Posts

  • 0

<?php
/*******************************************************************************
*	PHP AVATAR ROTATOR
*******************************************************************************/

$path = "/images/avatars/";	// Path to your avatar folder relative to the root directory of your site

/******************************************************************************
*       DO NOT EDIT BELOW THIS LINE!
******************************************************************************/

$dir = $_SERVER['DOCUMENT_ROOT'].$path;
$avatars = array();

// Open avatar directory and read its contents into an array
if (is_dir($dir)) {
	if ($dh = opendir($dir)) {
		while (($file = readdir($dh)) !== false) {
			if (filetype($dir.$file) == "file" && getimagesize($dir.$file)) {
				array_push($avatars, $file);
			}
		}
		closedir($dh);
	}
}

// Create random avatar
$img = $dir.$avatars[rand(0, count($avatars)-1)];
$info = getimagesize($img);

if ($info[2] == 2) {
	header('Content-Type: image/jpeg');

	$img = imagecreatefromjpeg($img);
	imagejpeg($img);
}

elseif ($info[2] == 3) {
	header('Content-Type: image/png');

	$img = imagecreatefrompng($img);
	imagepng($img);
}

else {
	header('Content-Type: image/gif');

	$img = imagecreatefromgif($img);
	imagegif($img);
}

imagedestroy($img);
?>

Save as avatar.php. Most forums will have this file extension disabled so you'll need to add the following to .htaccess:

AddType application/x-httpd-php .png
RewriteEngine On
RewriteRule .png avatar.php

I used a PHP generated image for a sig I did awhile back and the forum wouldn't allow PHP extensions as an image (for obvious reasons.)

So that .htaccess AddType allowed you to use http://mlgvideos.com/mw2/sig/countdown.png to show

countdown.png

Even though it's really http://mlgvideos.com/mw2/sig/countdown.php

  • 0

I tried that however, i cannot get it to work being that the images are hosted elsewhere from the php file. i will attempt to get them all in the same location and try again.

Is there a way other than PHP that can do it. My host does not allow PHP that is why i put the php file elsewhere. A friend has a PHP host that will allow this however, i cannot host the images there due to bandwidth.

lol if not its no big deal i was just wondering if i could rotate automatically the images used with my avatar.

  • 0
  On 10/02/2010 at 23:53, sweetsam said:

That shouldn't be a problem. Have the image url's in an array and select a random url and do a url redirect and voila you got your rotating avatar.

how would i go about doing that.

  • 0

Try this...

<?php

$host = 'http://www.imghost.com/';

$img[] = 'images/av01.png';
$img[] = 'images/av02.png';
$img[] = 'images/av03.png';

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

header("location: $host.${img[$num]}");

?>

The above code will go in to the php file and host would be the address of the image host.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.