Skip to:
Content
Pages
Categories
Search
Top
Bottom

Re: bbpress / wordpress avatar?


Vili
Participant

@vilimaunula

Ok, here’s what I have, commented. The comments that start with “VILI” are mine.

Please keep in mind two things:

a) This is strictly speaking a WordPress plugin, not a bbPress one. The functionality is simply extended to also cover an integrated bbPress installation. (And yes, this means that you need to install this in your WordPress, not your bbPress.)

b) I hacked this for my own purposes, so it may not be pretty, and it certainly won’t be ready for use without you reading through the code. If someone is interested in working to make it functional as an “out of the box” solution, then by all means go ahead.

Finally, the original Dan’s Avatar Thingy can be found here.

<?php
/*
Plugin Name: Vili's Avatar Thingy
Plugin URI: http://www.vertebratesilence.com/
Description: Displays an avatar next to posts or comments based on username. Based on Dan's Avatar Thingy (http://www.cheesemasterdan.com/).
Version: 0.1
Author: Vili Maunula
Author URI: http://www.vertebratesilence.com/
*/

// Variable declarations
// VILI: These were changed from Dan's Avatar Thingy, in which you only had default
// size settings into which all avatars were resized.

$avatar_max_width = 100; // Max avatar width
$avatar_max_height = 100; // Max avatar height
$avatar_max_upload_size = 15360; // Max file size in bytes
$avatars_path = ABSPATH."wp-content/avatars";

function cmd_show_avatar()
{
global $wpdb;
global $avatars_path;

// VILI: The following is a boolean setting I use to detect whether the page
// being served is a forum page. In practice, I have
// <?php global $forumpage;
// $forumpage = TRUE; ?>
// at the beginning of each forum page (I use WordPress headers in my integrated
// setup, but if your bbPress uses its own headers, just stick it there). If
// anyone knows a more cost-efficient way of detecting whether a page shown is
// a forum page, let me know.
global $forumpage;

if ($forumpage == FALSE) {
$the_author_name = get_comment_author();
if($the_author_name == "" or $the_author_name == __('Anonymous'))
{ // Avatar for posts
$the_author = get_the_author_id();
$the_author_name = get_the_author();
} else { // Avatar for comments - only for registered users
$the_comment_ID = get_comment_ID();
$the_author = $wpdb->get_var("SELECT user_ID FROM $wpdb->comments WHERE comment_ID='$the_comment_ID'");
}
} else {
$the_author_name = get_post_author();
$the_author = get_post_author_id();
}

$image_path = get_bloginfo('wpurl')."/wp-content/avatars/";
$the_avatar = $image_path.$the_author.".jpg";
if(file_exists("$avatars_path/$the_author.jpg"))
{
echo '<img src="' . $the_avatar. '" alt="' . $the_author_name . '" class="cmd-avatar" />';
} elseif(file_exists("$avatars_path/default.jpg")) {
echo '<img src="' . $image_path . 'default.jpg" alt="Unregistered" class="cmd-avatar" />';
}
// If the file doesn't exist then return nothing...
}

add_action ('admin_menu', 'cmd_avatar_menu');

function cmd_avatar_menu()
{
add_submenu_page('profile.php', '', 'Your Avatar', 0, __FILE__, 'cmd_avatar_profile');
}

function cmd_avatar_profile()
{
global $userdata;
global $avatar_max_width;
global $avatar_max_height;
global $avatar_max_upload_size;
global $avatars_path;
get_currentuserinfo();

if(!file_exists($avatars_path))
{
$mkdir_result = @mkdir($avatars_path, "0755");
if(!$mkdir_result)
{
echo '<div id="message" class="error fade">The folder' . $avatars_path . ' does not exist and could not be created automatically. Please create it and assign 0755 permissions then try using this plugin again.
</div>';
die();
}
}

if ($_POST['cmd_action'] == 'upload_avatar')
{
if ($_FILES['cmd_avatar_file']['size'] > 0 && $_FILES['cmd_avatar_file']['size'] < $_POST['MAX_FILE_SIZE'])
{
$uploaddir = ABSPATH."wp-content/avatars/";
$uploadfile = $uploaddir . $userdata->ID . '.jpg';
list($width, $height, $type, $attr) = getimagesize($_FILES['cmd_avatar_file']['tmp_name']);

// VILI: The following calculates the size into which the
// image needs to be put. If it's under the MAX limits, nothing
// is done, otherwise resizing takes place. This probably
// could be written more elegantly.

$widthratio = $width / $avatar_max_width;
$heightratio = $height / $avatar_max_height;
if ($widthratio > 1 && $heightratio > 1 && $widthratio > $heightratio) {
$usewidth = $width / $widthratio;
$useheight = $height / $widthratio;
} elseif ($widthratio > 1 && $heightratio > 1 && $widthratio < $heightratio) {
$useheight = $height / $heightratio;
$usewidth = $width / $heightratio;
} elseif ($widthratio > 1) {
$usewidth = $width / $widthratio;
$useheight = $height / $widthratio;
} elseif ($heightratio > 1) {
$useheight = $height / $heightratio;
$usewidth = $width / $heightratio;
} else {
$usewidth = $width;
$useheight = $height;
}

$cmd_avatar_image = imagecreatetruecolor ($usewidth, $useheight);
switch ($type)
{
case 1: // GIF
$image = imagecreatefromgif($_FILES['cmd_avatar_file']['tmp_name']);
imagecopyresampled($cmd_avatar_image, $image, 0, 0, 0, 0, $usewidth, $useheight, $width, $height);
$avatar_created = (imagejpeg($cmd_avatar_image, $uploadfile, 100) ? TRUE : FALSE);
imagedestroy($image);
imagedestroy($cmd_avatar_image);
break;
case 2: // JPEG
$image = imagecreatefromjpeg($_FILES['cmd_avatar_file']['tmp_name']);
imagecopyresampled($cmd_avatar_image, $image, 0, 0, 0, 0, $usewidth, $useheight, $width, $height);
$avatar_created = (imagejpeg($cmd_avatar_image, $uploadfile, 100) ? TRUE : FALSE);
imagedestroy($image);
imagedestroy($cmd_avatar_image);
break;
case 3: // PNG
$image = imagecreatefrompng($_FILES['cmd_avatar_file']['tmp_name']);
imagecopyresampled($cmd_avatar_image, $image, 0, 0, 0, 0, $usewidth, $useheight, $width, $height);
$avatar_created = (imagejpeg($cmd_avatar_image, $uploadfile, 100) ? TRUE : FALSE);
imagedestroy($image);
imagedestroy($cmd_avatar_image);
break;
default:
$avatar_created = FALSE;
break;
}
if ($avatar_created) {
echo '<div id="message" class="updated fade">File uploaded successfully.
</div>';
} else {
echo '<div id="message" class="error fade">File upload failed.
</div>';
}
}
}
echo '
<div class="wrap">
<h2>Your Avatar</h2>
<form name="cmd_avatar" id="your-profile" action="' . $PHP_SELF . '" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Avatar</legend>';

$the_user = $userdata->ID;

$avatars_path = ABSPATH."wp-content/avatars";
$image_path = get_bloginfo('wpurl')."/wp-content/avatars/";
$the_avatar = $image_path.$the_user.".jpg";

if(file_exists("$avatars_path/$the_user.jpg")){
echo '
<img src="' . $the_avatar . '" alt="' . $userdata->user_login . '" class="cmd-avatar">';
} else {
echo 'No avatar found...';
}
echo '

<label>Filename: </label>
<input type="file" accept="Pictures" name="cmd_avatar_file" />

<input type="hidden" name="MAX_FILE_SIZE" value="' . $avatar_max_upload_size . '" />
<input type="hidden" name="cmd_action" value="upload_avatar" />
Click browse to find your avatar image. It can be JPG, GIF or PNG and should be ' . $avatar_max_width . ' x ' . $avatar_max_height . ' pixels or less (if bigger, it will be resized).Images should also be no larger than ' . $avatar_max_upload_size / 1024 . 'KBCurrently animated GIFs are not supported.
</fieldset>
<br clear="all" />
<p class="submit">
<input type="submit" name="cmd_avatar_update" value="Update Profile &raquo;" />

</form>
</div>';
}

?>

Skip to toolbar