Forum Avatar PHP project


(Andrew) #1

For a few reasons, I really dislike the letter based avatars automatically generated by the forums.

I would like to propose a PHP script that builds random avatars from a URL. For example: entering /blablabla.com/avatar.png in the forum’s avatar URL setting would generate a new random avatar on each new user registration. The avatar URL setting is built-in, so we just need to write the script.

http://php.net/manual/en/function.imagecreatefrompng.php

(PHP 4, PHP 5, PHP 7)

imagecreatefrompng — Create a new image from file or URL

Example:

<?php

function LoadPNG($imgname)
{
/* Attempt to open */
$im = @imagecreatefrompng($imgname);

/* See if it failed /
if(!$im)
{
/
Create a blank image */
$im = imagecreatetruecolor(150, 30);
$bgc = imagecolorallocate($im, 255, 255, 255);
$tc = imagecolorallocate($im, 0, 0, 0);

  imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

   /* Output an error message */
    imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}

return $im;
}

header(‘Content-Type: image/png’);

$img = LoadPNG(‘bogus.image’);

imagepng($img);
imagedestroy($img);
?>

Rather than a text overlay as in the example above, the script would overlay an image (with transparency) over another background image.

The other main difference would be that we would hard-code maybe 10 or 20 PNG images for the foreground and 5-10 for the background. Perhaps these would be in an array, and a random combination would be chosen each time the URL is called.

The output size would be consistent, and the sizing of the input images could be made to match in advance.

I suspect this script could be written by someone who knows PHP in less time than it took me to write this, but it’s also possible I’m overlooking a limitation in this function.

Anybody game? @Liras @ajprog (or others)


(Jarosław Wątroba) #2

Here you go, no need to hard-code anything :slight_smile: :

<?php
$foregroundsDir = "Foregrounds/";
$backgroundsDir = "Backgrounds/";
$filters = "*.{jpg,png,gif}";
$avatarSize = 128;

function GetRandomAvatar($seedString)
{
	global $foregroundsDir, $backgroundsDir, $filters, $avatarSize;
	
	$generatorSeed = crc32($seedString);
	srand($generatorSeed);

	$foregrounds = glob($foregroundsDir . $filters, GLOB_BRACE);
	$backgrounds = glob($backgroundsDir . $filters, GLOB_BRACE);

	if ((!$foregrounds) || (!$backgrounds))
		return false;

	$foregroundFilename = $foregrounds[rand(0, count($foregrounds) - 1)];
	$backgroundFilename = $backgrounds[rand(0, count($backgrounds) - 1)];

	$foregroundImage = imagecreatefrompng($foregroundFilename);
	$backgroundImage = imagecreatefrompng($backgroundFilename);
	
	if ((!$foregroundImage) || (!$backgroundImage))
		return false;
	
	$avatar = imagecreatetruecolor($avatarSize, $avatarSize);

	$copied = imagecopy($avatar, $backgroundImage, 0, 0, 0, 0, $avatarSize, $avatarSize);
	$copied = imagecopy($avatar, $foregroundImage, 0, 0, 0, 0, $avatarSize, $avatarSize);
	
	if (!$copied)
		return false;
	
	return $avatar;
}

$avatar = GetRandomAvatar($_GET["seed"]);

if ($avatar)
{
	header('Content-Type: image/png');
	imagepng($avatar);
	imagedestroy($avatar);
}
else
	echo "ERROR";  
?>

It works like this: you have one folder called foregrounds and one called backgrounds. In these folders you put the images. They have to be squares of width specified by $avatarSize in pixels. It is also the size of your output image. $filters define the file types supported and $…dirs are paths to the image directories. Feel free to change those parameters. Then you make a server request [or simply type that into the browser’s address bar): someURL/avatar.php?seed=newUserNameOrWhateverUnique (I assume the file is called avatar.php, of course) and you get either a PNG image download or an “ERROR” message.

The script first selects a bg and a fg based on your input seed, then combines them together and outputs the file. No problem with transparency. I attach my testing setup which I put inside my htdocs folder on my local Apache server. Make sure GD library is enabled (can check in phpinfo()). As I don’t have access to the FC website, it’s up to you though to integrate it into the forums user registration.

Good to be back to programming, even for such little things. I now realise how much I missed it. It took some 20 Google searches and tabs opened at once to write this. :slight_smile:

Liras

Ah, can’t upload ZIPs here, so Dropbox URL: Avatar.zip


(Andrew) #3

This is great. Thank you! I’ll test it later tonight.


(Andrew) #4

Works perfectly! The system even retroactively regenerated default avatars for everyone who hadn’t set them to default. That’s cool, because if we come up with nicer/custom avatars in the future, it will continue to roll out the new versions.

Thanks for putting this together, @liras.


(Jarosław Wątroba) #5

I was a bit skeptical at first, to be honest… I thought it is waaay too much work for me and for you with only such a subtle effect as more interesting default avatars… but man! They look sooo much better now! And it worked for you on day one! I was expecting like a week more of debugging and it’s working after just a coupla hours of writing… :slight_smile: I am very surprised how well it turned out. Great work. Just make sure you put enough icons so that there is enough variety.