• 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.
  • Posts

    • Ouch, man.. Hope you can get a replacement for that..
    • They just need to add a transparency slider. I’d prefer a middle ground where everything is transparent, but background items are blurred.
    • A guy messaged me yesterday wanting to talk on the phone, it turns out his friend messaged on "marktplaats" a day earlier asking what my lowest price was (€1650). After talking for a bit about it (he says he has no experience with computers and is relying on his friend) and then confirming with his friend, he messaged me he wanted to pick it up today. They both came today to check it out, and pick it up. SOLD. I prefer it this way rather than having to cannibalize it to sell for parts.
    • Was taking the side glass panel off because I want to install the AORUS X870E Master GPU holder, and while I had the glass side panel in my hands the front one decided to take a kamikaze dive. Now I hope they sell the glass fronts separately, or I am screwed. I'll probably be finding bits of glass for weeks now  😅
    • Yeh, I think you'll find there wasn't much legit savings from what he did. Any chump can fire a bunch of people indiscriminately from positions and services that don't impact that person. At the end of the day, it just means that services that are essential in any functional democracy, won't be able to operate because there are no people to deliver those services. Sure, it "saves" money on paper, but like most short sighted bean counters, there are very real, and expensive impacts later on down the track. Did you know that by buying 2x toothbrush's and a tube of toothpaste every year, and seeing the dentist every 6 months would cost you about $400 a year? So by the time you're in your 30's that would be $12000 of cost! Now imagine if you didn't spend any of that $12000 on preventative oral health care! Wow, so much more money! But you also needed to have several root canals at $1500 per tooth. Plus the time off work for being unable to function with the pain. So lets face it, that initial cost is worth it!
  • Recent Achievements

    • Week One Done
      Simmo3D earned a badge
      Week One Done
    • One Month Later
      Simmo3D earned a badge
      One Month Later
    • One Month Later
      greege earned a badge
      One Month Later
    • Week One Done
      greege earned a badge
      Week One Done
    • Week One Done
      LagFighterZ earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      547
    2. 2
      ATLien_0
      230
    3. 3
      +FloatingFatMan
      166
    4. 4
      Michael Scrip
      119
    5. 5
      +Edouard
      91
  • Tell a friend

    Love Neowin? Tell a friend!