Index: common/attachment.php
===================================================================
--- common/attachment.php	(revision 1152)
+++ common/attachment.php	(working copy)
@@ -145,4 +145,83 @@
     return false;
 }
 
+/**
+ * Resizes an image whose filepath is given by the parameters into a new
+ * location specified by the other parameters
+ *
+ * Returns whether or not the image has been successfully resized
+ * Note that it returns false if the image doesn't have a known file-type
+ *
+ * The $new_filepath parameter if ommited or given as null will result in the
+ * image being outputted directly to the client
+ *
+ * @param  array   $image_info
+ * @param  string  $filepath
+ * @param  array   $new_image_info
+ * @param  string  $new_filepath
+ * @return bool
+ */
+function image_resize($image_info, $filepath, $new_image_info,
+        $new_filepath = null) {
+    list($image_width, $image_height, $image_type, $image_attribute) = $image_info;
+    list($new_image_width, $new_image_height) = $new_image_info;
+
+    switch ($image_type) {
+        case IMAGETYPE_GIF:
+            // NOTE: animated GIFs become static. Only the first frame is saved
+            // Seems like a good thing anyway
+            $image = imagecreatefromgif($filepath);
+            $image_resized = imagecreate($new_image_width, $new_image_height);
+            // reset palette and transparent color to that of the original file
+            $trans_col = imagecolortransparent($image);
+            imagepalettecopy($image_resized, $image);
+            imagefill($image_resized, 0, 0, $trans_col);
+            imagecolortransparent($image_resized, $trans_col);
+            imagecopyresampled($image_resized, $image, 0, 0, 0, 0,
+                    $new_image_width, $new_image_height, $image_width,
+                    $image_height);
+
+            if ($new_filepath != null) {
+                return imagegif($image_resized, $new_filepath);
+            }
+            return imagegif($image_resized);
+
+        case IMAGETYPE_JPEG:
+            $image = imagecreatefromjpeg($filepath);
+            $image_resized = imagecreatetruecolor($new_image_width,
+                    $new_image_height);
+            imagecopyresampled($image_resized, $image, 0, 0, 0, 0,
+                    $new_image_width, $new_image_height, $image_width,
+                    $image_height);
+
+            if ($new_filepath != null) {
+                return imagejpeg($image_resized, $new_filepath);
+            }
+            return imagejpeg($image_resized);
+
+        case IMAGETYPE_PNG:
+            $image = imagecreatefrompng($filepath);
+            $image_resized = imagecreatetruecolor($new_image_width,
+                    $new_image_height);
+            // turn off the alpha blending to keep the alpha channel
+            imagealphablending($image_resized, false);
+            // allocate transparent color
+            $col = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
+            // fill the image with the new color
+            imagefilledrectangle($image_resized, 0, 0, $new_image_width,
+                    $new_image_height, $col);
+            imagecopyresampled($image_resized, $image, 0, 0, 0, 0,
+                    $new_image_width, $new_image_height, $image_width,
+                    $image_height);
+            imagesavealpha($image_resized, true);
+            if ($new_filepath != null) {
+                return imagepng($image_resized, $new_filepath);
+            }
+            return imagepng($image_resized);
+
+        default:
+            // unsupported image type
+            return false;
+    }
+}
 ?>
Index: common/avatar.php
===================================================================
--- common/avatar.php	(revision 0)
+++ common/avatar.php	(revision 0)
@@ -0,0 +1,39 @@
+<?php
+
+require_once(IA_ROOT_DIR.'common/db/db.php');
+require_once(IA_ROOT_DIR.'www/controllers/attachment.php');
+require_once(IA_ROOT_DIR.'www/controllers/image_attachment.php');
+require_once(IA_ROOT_DIR.'common/db/attachment.php');
+require_once(IA_ROOT_DIR.'www/controllers/account_validator.php');
+require_once(IA_ROOT_DIR.'common/common.php');
+require_once(IA_ROOT_DIR.'common/attachment.php');
+
+/**
+ * It takes an avatar image file given by it's filepath and resizes it in the
+ * sizes necessary on the site.
+ *
+ * @param  string  $filepath
+ * @param  array   $image_info
+ * @param  string  $new_filename
+ */
+function avatar_cache_resized($filepath, $image_info, $new_filename) {
+    $resize_sizes = array('L16x16' => 'tiny/', 'L32x32' => 'small/',
+            'L50x50' => 'normal/' , '75x75'=> 'forum/', '150x150' => 'big/');
+
+    // Copying the original image
+    copy($filepath, IA_AVATAR_FOLDER.'full/'.$new_filename);
+
+    list($image_width, $image_height, $image_type, $image_attribute) =
+            $image_info;
+
+    foreach ($resize_sizes as $resize_size => $resize_folder) {
+        $new_image_info = resize_coordinates($image_width,
+                $image_height, $resize_size);
+
+        // resizing
+        image_resize($image_info, $filepath, $new_image_info,
+                IA_AVATAR_FOLDER.$resize_folder.$new_filename);
+    }
+}
+
+?>
Index: common/common.php
===================================================================
--- common/common.php	(revision 1152)
+++ common/common.php	(working copy)
@@ -261,6 +261,17 @@
     return $obj == 'user' || $obj == 'task' || $obj == 'round' || $obj == 'textblock';
 }
 
+/**
+ * Returns whether or not the size type given is an existent one on the site
+ *
+ * @param  string  $size_type
+ * @return bool
+ */
+function is_valid_size_type($size_type) {
+    $size_types = array("full", "tiny", "small", "normal", "forum", "big");
+    return in_array($size_type, $size_types);
+}
+
 // Cached version of create_function_cached.
 // Never use create_function_cached directly because it's a memory leak.
 // It's better to try avoid create_function_cached anyway, but if you
Index: common/db/user.php
===================================================================
--- common/db/user.php	(revision 1152)
+++ common/db/user.php	(working copy)
@@ -167,7 +167,7 @@
 function user_update($user)
 {
     log_assert_valid(user_validate($user));
-    
+
     // Update DB
     db_update("ia_user", $user, "id = ".db_quote($user['id']));
 
Index: config.php.sample
===================================================================
--- config.php.sample	(revision 1152)
+++ config.php.sample	(working copy)
@@ -32,7 +32,10 @@
 // Slashes at both ends.
 define("IA_URL_PREFIX", "--write-me-IA_URL_PREFIX--");
 
+// Define the place the avatars are being stored
+define("IA_AVATAR_FOLDER", "--write-me-IA_AVATAR_FOLDER--");
 
+
 // Congratulations! You're done with the essential configuration.
 // --------------------------------------------------------------
 // There are some more settings to adjust but they are optional.
Index: scripts/make-avatar-folder
===================================================================
--- scripts/make-avatar-folder	(revision 0)
+++ scripts/make-avatar-folder	(revision 0)
@@ -0,0 +1,78 @@
+#! /usr/bin/env php
+<?php
+
+require_once(dirname($argv[0])."/utilities.php");
+require_once(IA_ROOT_DIR.'common/db/db.php');
+require_once(IA_ROOT_DIR.'www/controllers/attachment.php');
+require_once(IA_ROOT_DIR.'www/controllers/image_attachment.php');
+require_once(IA_ROOT_DIR.'common/db/attachment.php');
+require_once(IA_ROOT_DIR.'common/cache.php');
+require_once(IA_ROOT_DIR.'common/db/user.php');
+require_once(IA_ROOT_DIR.'common/avatar.php');
+require_once(IA_ROOT_DIR.'common/db/textblock.php');
+
+// Database connect
+db_connect();
+
+// Managing the userinfo template from the textblock table
+if (read_bool("Do you want to change the template/userinfo textblock?", true)) {
+    $textfortextblock = "table(compact). |=/5. ==UserImage".
+    "(user=\"%user%\" size=\"big\")==\r\n{'Vezi solutiile trimise':monitor?use".
+    "r=%user%}\r\n|_. Nume | == userinfo(user=\"%user%\" info=\"fullname\")== ".
+    "|\r\n| |_. Cont | ==userinfo(user=\"%user%\" info=\"username\")== |\r\n| ".
+    "|_. Rating | ==userinfo(user=\"%user%\" info=\"rating\")== |\r\n| |_. Sta".
+    "tut | ==userinfo(user=\"%user%\" info=\"security\")== |\r\n|_. Forum | ==".
+    "SmfLink(user=\"%user%\" link=\"pm\" caption=\"trimite mesaj privat\")==, ".
+    "==SmfLink(user=\"%user%\" link=\"stats\" caption=\"vezi activitate\")== |".
+    "\r\n\r\n\r\n";
+    textblock_add_revision('template/userinfo', 'template/userinfo',
+            $textfortextblock, 1, "protected");
+}
+
+// getting the user list
+$userlist = user_get_list(false);
+
+// taking care of the noimage file
+$noimage_filepath = attachment_get_filepath(attachment_get('noimage',
+                    'template/infoarena'));
+// creating the avatar folder if it does not exists, and grant it permissions
+if (file_exists(IA_AVATAR_FOLDER) == false) {
+    mkdir(IA_AVATAR_FOLDER, 0777);
+    $size_types = array("tiny", "small", "normal", "forum", "big", "full");
+    foreach ($size_types as $size) {
+        if (file_exists(IA_AVATAR_FOLDER."/".$size) == false) {
+            mkdir(IA_AVATAR_FOLDER."/".$size, 0777);
+        }
+    }
+}
+
+// resizing the noimage file
+$img_info = getimagesize($noimage_filepath);
+avatar_cache_resized($noimage_filepath, $img_info, 'noimage');
+
+// Going through all the users
+foreach ($userlist as $user) {
+    $attach = attachment_get('avatar', 'utilizator/'.$user);
+    $found = !is_null($attach);
+
+    if ($found) {
+        $real_name = attachment_get_filepath($attach);
+        $found = file_exists($real_name);
+        if (!$found)
+            log_warn("File does not exists but it is in the Database.");
+    }
+
+    if (!$found) {
+        continue;
+    }
+
+    $img_info = getimagesize($real_name);
+    if ($img_info == false) {
+        continue;
+    }
+
+    // The user has a valid image, resizing it, but keeping the attachment
+    avatar_cache_resized($real_name, $img_info, "a".$user);
+}
+
+?>

Property changes on: scripts/make-avatar-folder
___________________________________________________________________
Added: svn:executable
   + *

Index: scripts/setup
===================================================================
--- scripts/setup	(revision 1152)
+++ scripts/setup	(working copy)
@@ -134,9 +134,16 @@
 $config_vars['IA_DB_NAME'] = 'infoarena2';
 
 // Ask user.
-$config_vars['IA_ROOT_DIR'] = read_line("SVN checkout dir?", $config_vars['IA_ROOT_DIR']);
+$config_vars['IA_ROOT_DIR'] = read_line("SVN checkout dir?",
+        $config_vars['IA_ROOT_DIR']);
 $config_vars['IA_ROOT_DIR'] = slash_string(
         realpath($config_vars['IA_ROOT_DIR']), true, true);
+$config_vars['IA_AVATAR_FOLDER'] =
+        $config_vars['IA_ROOT_DIR'].'www/static/images/avatar/';
+$config_vars['IA_AVATAR_FOLDER'] = read_line("Avatar Folder?",
+        $config_vars['IA_AVATAR_FOLDER']);
+$config_vars['IA_AVATAR_FOLDER'] = slash_string(
+        realpath($config_vars['IA_AVATAR_FOLDER']), true, true);
 $config_vars['IA_URL_HOST'] = read_line("Host part of url (with http)?",
         $config_vars['IA_URL_HOST']);
 $config_vars['IA_URL_HOST'] = slash_string(
@@ -222,6 +229,7 @@
     $sitename = read_line("Site name?", $sitename);
     system("chmod g+ws {$ia_root}attach");
     system("chmod g+ws {$ia_root}cache");
+    system("chmod g+ws {$ia_root}www/static/images/avatar");
     system("chmod g+ws {$ia_root}www/static/images/tmp");
     system("chmod g+ws {$ia_root}www/static/images/latex");
 
Index: smf/Sources/Load.php
===================================================================
--- smf/Sources/Load.php	(revision 1152)
+++ smf/Sources/Load.php	(working copy)
@@ -1004,7 +1004,7 @@
 	}
 
 	// use infoarena avatars
-	$avatar_img_src = url_user_avatar($profile['memberName'], '75x75', true);
+	$avatar_img_src = url_user_avatar($profile['memberName'], 'forum', true);
 
 	// What a monstrous array...
 	$memberContext[$user] = array(
Index: www/controllers/account.php
===================================================================
--- www/controllers/account.php	(revision 1152)
+++ www/controllers/account.php	(working copy)
@@ -3,7 +3,9 @@
 require_once(IA_ROOT_DIR."common/db/user.php");
 require_once(IA_ROOT_DIR."common/db/attachment.php");
 require_once(IA_ROOT_DIR."common/db/smf.php");
+require_once(IA_ROOT_DIR."common/avatar.php");
 require_once(IA_ROOT_DIR."www/controllers/account_validator.php");
+require_once(IA_ROOT_DIR."www/config.php");
 
 // identify target user and check permission to edit profile
 // Yields flash_error & redirect when username invalid or security error
@@ -130,6 +132,15 @@
                                             $disk_name)) {
                         $errors['avatar'] = 'Fisierul nu a putut fi incarcat '
                                             .'pe server.';
+                    } else {
+                        // resize the avatar if it is a correct mime-type
+                        global $IA_SAFE_MIME_TYPES;
+                        $img_info = getimagesize($disk_name);
+                        // check if mime-type is from accepted ones
+                        if (in_array($img_info['mime'], $IA_SAFE_MIME_TYPES)) {
+                            avatar_cache_resized($disk_name, $img_info,
+                                    "a".$user['username']);
+                        }
                     }
                 }
             }
Index: www/controllers/image_attachment.php
===================================================================
--- www/controllers/image_attachment.php	(revision 1152)
+++ www/controllers/image_attachment.php	(working copy)
@@ -69,46 +69,11 @@
     // Actual image resizing
     // Image is placed in output buffer, cached, then flushed.
     ob_start();
-    switch ($img_type) {
-        case IMAGETYPE_GIF:
-            // NOTE: animated GIFs become static. Only the first frame is saved
-            // Seems like a good thing anyway
-            $im = imagecreatefromgif($real_name);
-            $im_resized = imagecreate($new_width, $new_height);
-            // reset palette and transparent color to that of the original file
-            $trans_col = imagecolortransparent($im);
-            imagepalettecopy($im_resized, $im);
-            imagefill($im_resized, 0, 0, $trans_col);
-            imagecolortransparent($im_resized, $trans_col);
-            imagecopyresampled($im_resized, $im, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
-            imagegif($im_resized);
-            break;
+    $image_supported = image_resize($ret, $real_name, array($new_width, $new_height));
+    ob_end_clean();
 
-        case IMAGETYPE_JPEG:
-            $im = imagecreatefromjpeg($real_name);
-            $im_resized = imagecreatetruecolor($new_width, $new_height);
-            imagecopyresampled($im_resized, $im, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
-            imagejpeg($im_resized);
-            break;
-
-        case IMAGETYPE_PNG:
-            $im = imagecreatefrompng($real_name);
-            $im_resized = imagecreatetruecolor($new_width, $new_height);
-            // turn off the alpha blending to keep the alpha channel
-            imagealphablending($im_resized, false);
-            // allocate transparent color
-            $col = imagecolorallocatealpha($im_resized, 0, 0, 0, 127);
-            // fill the image with the new color
-            imagefilledrectangle($im_resized, 0, 0, $new_width, $new_height, $col);
-            imagecopyresampled($im_resized, $im, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
-            imagesavealpha($im_resized, true);
-            imagepng($im_resized);
-            break;
-
-        default:
-            ob_end_clean();
-            // unsupported image type
-            die_http_error(500, "Unsupported image type");
+    if ($image_supported == false) {
+        die_http_error(500, "Unsupported image");
     }
 
     // Store in cache, if enabled
@@ -119,7 +84,7 @@
     // HTTP headers
     header("Content-Type: " . image_type_to_mime_type($img_type));
     header("Content-Disposition: inline; filename=" . urlencode($file_name) . ";");
-    // WARNING: strlen() is supposed to be binary safe but some say it may 
+    // WARNING: strlen() is supposed to be binary safe but some say it may
     // be shadowed by mb_strlen() and treat strings as unicode by default,
     // thus reporting invalid lengths for random binary buffers.
     // What is the alternative?
Index: www/format/format.php
===================================================================
--- www/format/format.php	(revision 1152)
+++ www/format/format.php	(working copy)
@@ -129,12 +129,11 @@
 }
 
 // Format avatar img.
-function format_user_avatar($user_name, $width = 50, $height = 50,
+function format_user_avatar($user_name, $size_type = "full",
                             $absolute = false)
 {
-    log_assert(is_whole_number($width), "Invalid width");
-    log_assert(is_whole_number($height), "Invalid height");
-    $url = url_user_avatar($user_name, "L{$width}x{$height}");
+    log_assert(is_valid_size_type($size_type), "Invalid size type");
+    $url = url_user_avatar($user_name, $size_type);
     if ($absolute) {
         $url = url_absolute($url);
     }
@@ -165,7 +164,7 @@
     $result = "";
     $result .= "<span class=\"tiny-user\">";
     $result .= format_link($user_url,
-                           format_user_avatar($user_name, 16, 16, false).$user_fullname,
+                           format_user_avatar($user_name, "tiny", false).$user_fullname,
                            false);
     $result .= ' '.$rbadge;
     $result .= "<span class=\"username\">"
@@ -187,7 +186,7 @@
     $result = "";
     $result .= "<div class=\"normal-user\">";
     $result .= format_link($user_url,
-                           format_user_avatar($user_name, 32, 32, false),
+                           format_user_avatar($user_name, "small", false),
                            false);
     $result .= "<span class=\"fullname\">$user_fullname</span><br />";
     $result .= $rbadge;
Index: www/htaccess.sample
===================================================================
--- www/htaccess.sample	(revision 1152)
+++ www/htaccess.sample	(working copy)
@@ -11,7 +11,14 @@
 RewriteCond %{REQUEST_URI}  !^.*/favicon\.ico$
 RewriteCond %{REQUEST_URI}  !^.*/stiri/.*$
 RewriteCond %{REQUEST_URI}  !^.*/news_feed$
+RewriteCond %{REQUEST_URI}  !^.*/avatar/.*$
 RewriteRule ^(.*) index.php?page=$1 [QSA]
+
+RewriteRule ^avatar/(tiny|small|normal|forum|big|full)/(.+)$ static/images/avatar/$1/a$2
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteCond %{REQUEST_FILENAME} !-d
+RewriteRule ^static/images/avatar/(tiny|small|normal|forum|big|full)/(.+)$ static/images/avatar/$1/noimage
+
 RewriteRule ^stiri/(.*)$ blog/$1 [R=301,QSA]
 RewriteRule ^news_feed$ blog?action=rss [R=301,QSA]
 
Index: www/macros/macro_userimage.php
===================================================================
--- www/macros/macro_userimage.php	(revision 0)
+++ www/macros/macro_userimage.php	(revision 0)
@@ -0,0 +1,16 @@
+<?php
+
+require_once(IA_ROOT_DIR.'www/format/format.php');
+require_once(IA_ROOT_DIR.'common/common.php');
+function macro_userimage($args) {
+    $user = $args['user'];
+
+    $size_type = $args['size'];
+
+    if (is_valid_size_type($size_type) == false) {
+        return macro_error("Unkown size type \"".$size_type."\".");
+    }
+
+    return format_link(url_complex("avatar/full/".$user),
+        format_user_avatar($user, $size_type, false), false);
+}
Index: www/url.php
===================================================================
--- www/url.php	(revision 1152)
+++ www/url.php	(working copy)
@@ -238,8 +238,8 @@
     ));
 }
 
-function url_user_avatar($username, $resize = "50x50") {
-    return url_image_resize(IA_USER_TEXTBLOCK_PREFIX . $username, 'avatar', $resize);
+function url_user_avatar($username, $size_type = "full") {
+    return url_complex("avatar/".$size_type."/".$username);
 }
 
 function url_unsubscribe($username, $key) {
Index: www/views/account.php
===================================================================
--- www/views/account.php	(revision 1152)
+++ www/views/account.php	(working copy)
@@ -91,7 +91,7 @@
         <li>
             <?php
                 // display avatar
-                $avatar_url = url_user_avatar($user['username'], "150x150");
+                $avatar_url = url_user_avatar($user['username'], "big");
                 echo '<img class="avatar" src="'.html_escape($avatar_url).'" alt="avatar"/>';
             ?>
         </li>
Index: www/views/sitewide.php
===================================================================
--- www/views/sitewide.php	(revision 1152)
+++ www/views/sitewide.php	(working copy)
@@ -15,7 +15,7 @@
 <div id="header" class="clear">
     <?php if (!identity_is_anonymous()) { $username = $identity_user['username']; ?>
         <div id="userbox">
-        <?= format_link(url_user_profile($username, true), format_user_avatar($username, 50, 50, true), false) ?>
+        <?= format_link(url_user_profile($username, true), format_user_avatar($username, "normal", true), false) ?>
             <div class="user">
                 <strong><?= html_escape($identity_user['full_name']) ?></strong><br/>
                 <?= format_user_ratingbadge($username, $identity_user['rating_cache']) ?>
