One thing I have always found insanely annoying when designing or even using a social network, is the constant battle with inconsistently sized avatars. As a user, I understand I can only do my part, but as a designer, I have been continually vexed by my inability to find a good Avatar Generator around the internet.
For those of you who have no idea what I am talking about, an Avatar is a computer user's representation of himself or herself. Open up Facebook, or God forbid Myspace, and look at your friends lists. Bingo...Avatars. And no matter what you will always have that one jackass who cuts himself out of a wedding photo, making their profile picture 60px X 400px, and now your profile looks like a working piece of shite!
As I said before...as a user, I have done my part. As a designer I have failed to find a good Avatar Generator...so as always I just built one, and have decided that if I post the code...maybe I can get some of these other social networks on the same page. (Doubtful!)
Now, I am fairly familiar with the Scriptaculous library, and I realized that it had everything I needed. If your familiar with the library, just stop and think about it. The only things that are complex in an avatar generator are the drag and drop, and image re-sizing. Scripaculous has both. A drag and drop function, and slider controls.
Below is my simple avatar generator utilizing Scriptaculous. (The final files can be downloaded at the end.)
Naturally, I built this on a PHP platform. However, the only time PHP is utilized is when saving it to the server. Other than that you have a little javascript, one photo to manipulate, a few DIV tags and the Scriptaculous library. The whole thing is well under 100 lines of code (excluding the PHP which is 31).
Javascript Includes:
<script src="ajax/prototype.js" type="text/javascript"></script><script src="ajax/scriptaculous.js" type="text/javascript"></script><script src="ajax/effects.js" type="text/javascript"></script><script src="ajax/slider.js" type="text/javascript"></script>
Javascript:
var origW = 0;var origH = 0;var curX = 0;var curY = 0;var curS = 0;var curImage = 'photo.jpg';function SubmitIt() {window.location.href="renderImage.php?image=" escape(curImage) '&&xpos=' escape(curX) '&&ypos=' escape(curY) '&&scale=' escape(curS);
}function Debug() {document.getElementById('textarea').value = 'Top: ' curY ', Btm: ' curX ', Scale: ' curS;}window.onload = function init() {origW = document.getElementById('handle').width;origH = document.getElementById('handle').height;new Draggable('handle');Droppables.add('testAv', {onDrop: function(element) {curX = element.style.left;curY = element.style.top;Debug();}});new Control.Slider('handle2', 'track2', {axis: 'vertical',onSlide: function(v){document.getElementById('handle').width = Number(origW * (1-v));document.getElementById('handle').height = Number(origH * (1-v));curS = v;Debug();}});}
HTML:
<h1>Avatar Generator</h1><table width="150" cellpadding="3" cellspacing="0" border="0"><tr><td width="120"><div id="testAv" style="width: 120px; height: 120px; overflow: hidden; border: 2px solid #FF3300;"><img src="photo.jpg" width="850" height="565" border="0" id="handle" /></div></td><td><div id="track2" style="height:120px; background-color:#ccc; width:10px;"><div id="handle2" style="width:15px; height:10px; background-color:#f00; cursor:move;"></div></div></td></tr></table><div style="margin-top: 20px;"><textarea name="textarea" id="textarea" rows="5" style="width: 300px"></textarea></div><div><input type="submit" name="Submit" value="Submit" onClick="SubmitIt()" /></div>
PHP:
<?php$uid = $_GET['uid'];$xP = substr($_GET['xpos'], 0, -2);$yP = substr($_GET['ypos'], 0, -2);$sC = $_GET['scale'];$file = $_GET['image'];$source = imagecreatefromjpeg($file);list($w, $h) = getimagesize($file);$width = ceil($w * (1 - $sC));$height = ceil($h * (1 - $sC));$tempFile = explode('.', $file);$tempFile = $tempFile[0] .'_TEMP.' .$tempFile[1];// Create 190x127 ###################################################$dest = imagecreatetruecolor(120, 120);$source = imagecreatefromjpeg($file);if(imagecopyresampled($dest, $source, $xP, $yP, 0, 0, $width, $height, $w, $h)) {imagejpeg($dest, $tempFile, 100);header('Content-Type: image/jpg');print file_get_contents($tempFile);} else {echo 'Error: There was a problem creating the file.';}?>













