Plugin: Avatar Upload
-
Although there are already two avatar plugins available, they require that users either have their own webspace to store an avatar or use one of the avatar services (e.g. Gravatar).
My users do not have either of those things, so I wrote an avatar upload plugin. Besides offering avatar hosting, it also allows you more control over the type and content of avatars than simply providing a link to an offsite image.
Features
- Bozos can not upload avatars.
- Admins can configure the maximum allowed file size (bytes) and dimensions (pixels) of images.
- Currently done from within the script (no Admin page interface at this time).
- Anybody with the ‘moderate’ capability can upload another user’s avatar.
- This ensures that inappropriate images can be removed.
- There is no “delete avatar” function at this time, but an inappropriate image can be removed by uploading a ’safe’ image (e.g. a blank 1×1 pixel image) to replace it (you could then manually set that user as a bozo to stop them re-uploading inappropriate images.
- File checking is carried out to ensure that:
- The file has a valid extension (.gif, .jpeg, .jpeg, .png)
- The file has a valid content-type (’image/gif’, ‘image/jpeg’, ‘image/png’)
- The file extension and content-type actually match.
- Other checks for filesize, image dimensions, etc.
More info and ZIP download: bbPress Plugin: Avatar Upload
I will be submitting this avatar to the bbPRess Plugin Repositry too.
-
Lovely! I thought about writing this plugin myself. Thank you very much indeed!
But I think the avatar.php file should be uploaded to the template folder within the bb-templates folder, rather than the my-templates folder…
If you are using the default template, ‘kakumei’, then yes, you can put ‘avatar.php’ in the default ‘bb-templates/kakumei’ folder.
If you are using a custom template, then you need to upload it to your custom template folder (which is how I’m using it) – it does not cause problems if the file does not exist in the default template.
A really useful plugin, it works very nicely. I’ve had my eye on Identicons for a while and wanted to integrate them, the result being that I hacked up Scott’s identicon plugin to only contain the class, wrote a function to make identicons for users and integrated it with your plugin (so, changes made to all three files). It assigns users identicons when they register, and they’re kept in the same way as you do things now. I personally love identicons. It’s running now at http://forums.loinhead.net/ – just look in any thread.
I was wondering, is there a reason for storing the metavalues in the database? An alternative is to go to the folder directly and look for all possible variants in turn (considering the standard naming, you’re just checking extensions), saving on the database read and writes – but I’m not sure which would be faster. You’d have to delete the old avatar of course in case you don’t overwrite it because of different filetypes.
There’s a final modification I want to make, purely aesthetic. Any idea how to get the avatar link into one of the profile tabs (like Edit or Favourites)?
Fel, want to shoot the code over to me, I would love to integrate it into my bbPress sites.
Sure, give me a moment to clean and zip it up.
What I meant to ask, Louisedade, was if you fancied making the identicons a branch of your plugin?
Should work; if it doesn’t please tell me! Instructions are there.
Arggh, I think my webhost has to upgrade to php5, as I am getting errors in the
<a href="avatar-upload.php?id=<?php echo $user->ID; ?>">Upload Avatar</a>
include.Strange – what is the error?
Besides, the code should be this instead I think!
href="<?php bb_get_option( 'uri' ); ?>/avatar-upload.php?id=<?php echo $user->ID; ?>"
Small error in the documentation. If you’re feeling lazy, feel free to hard-code your forum’s URL in instead.
Variations of this:
Parse error: parse error, unexpected ‘<‘ in /home/wetworx/public_html/forums/profile-edit.php on line 117
If you use wordpress aswell, I don’t suppose you know of a way to include the avatar next to the users comments? That would complete a perfect avatar mod.
@fel64: I’ve downloaded your mod and will take a look at how it works to see about branching my plugin. More thoughts later…
@smurfdude: That is an excellent idea. I’m not sure about how to integrate with WordPress, but I’m guessing it would require a WordPress plugin that pulls the data from the bbPress database.
I like the plugin! I am also looking at your ‘upload’ function as this could be useful for many other things including an ‘overall’ image uploader for members, attachment mod, etc. Very nice!
Trent
Very nice. I was hoping someone would eventually write a file uploader that I could integrate with my avatar plugin, but this works too. Good job
Ah i was hoping it would be something simple like inserting a bit of code in the comments page rather than needing a plugin to call stuff from the database. Being rubbish at php that kind of thing just confuses me, so will have to cross my fingers that someone else does it
Ok, I’ve now looked at the code and can see how it can be included without needing to branch the plugin too much (and all the maintenance issues involved with that!).
First of all I removed the identicon functions from the
bb-avatar-upload.php
file and placed them at the bottom of theidenticon.php
file, to keep them all together.Secondly, I moved the
if($_POST['identicon']){...}
code from theavatar-upload.php
file to the bottom ofidenticon.php
and made it a function called by the ‘bb_head’ hook (I might eventually do this for the main upload functions too). Note: Is there a pre-head hook that fires before any headers are sent?function useidenticon()
{
global $user;
//fel identicon
if( $_POST['identicon'] )
{
felapplyidenticon( $user->ID );
}
}
add_action( 'bb_head', 'useidenticon' );You then only need to supply and maintain two files for the “branch”.
- my-plugins/identicon.php
- my-templates/avatar.phpThe
avatar.php
template is the least likely file to change (it’s been modified slightly since version 0.1).I’ve made the modifications and zipped them up for you to examine:
Identar Modified (this is minus my files, except the template file)
You will also need the updated version of Avatar Upload: Avatar Upload (version 0.2)
By keeping the Identicon and Avatar parts of the code separate, it should be easier to use Subversion to merge in – or simply replace – changes I make to the trunk code.
Regarding filename storage in the database – there are two reasons for this. The first is that I prefer to store the image dimensions along with the current avatar. The maximum width and height of the avatar is just that, maximums; the actual dimensions could be any size and any aspect ratio within that limit. I then include the height and width attributes in the <img> tag – it’s good practise because the browser makes space for the image when loading the page, instead of shifting everything after the page has loaded.
The second reason is more exciting – future proofing. A future version of this plugin will allow multiple file uploads, allowing users to upload more than one avatar image and select which one to use.
Since releasing version 0.1 of the plugin, I have reduced the number of DB calls in the profile pages and in the
avatar-upload.php
file by making use of the$user
object and calling$user->avatar_file
when I need to access the avatar info.Although, when adding avatars to a user’s posts you still have to do a DB query. If I used your suggested method, it is still going to involve at least one call to the DB, either to get the
user_login
from theget_post_author_id()
(and then grab the avatar file), or to get the avatar itself from theget_post_author_id()
.@Trent and ardentfrost: Thank you!
Trent, feel free to use the upload code for whatever! I stole it from myself anyway… it is adapted from a photo gallery application I wrote.
My previous message hinted at expanding the number of images that can be uploaded, although I only mean it within the context of multiple avatars. But somebody else is welcome to expand the code to a general image/file uploader.
W00t! I’ve figured out how to display the avatar next to comments in wordpress. Though the way I have done it i’m worried it’s going to cause problems.
Now if only there was a way to display a default image if the user hasn’t uploaded one. Not that it’s a big deal. Thanks
A default avatar option will be in the next version of the plugin!
Also, Smurfdude, Would you also like to show how you included avatars in WordPress, for the benefit of other users?
Ok sure. Though bare in mind this is probably a really crappy way of doing it, but hey, I don’t even know php, so give me some credit for trying hehe.
I deleted bb-avatar-upload.php from /my-plugins/ and put it in my wordpress plugins folder. Then at the top of the file I put:
include(‘/path/to/blog/forum/avatar_function.php’);
I created avatar_function.php myself and pasted in the contents of the bbpress config file. The reason I didn’t use the original file is because the follow was causing an error when logging in to the admin panel.
require_once(‘/path/to/blog/wp-blog-header.php’);
define(‘wp_bb’,true);
That’s basically it. I’ve checked about every page and everything is working fine, so i’ve no idea if there’s any potential problems doing this, but all I know is it works as far as i can tell.
Now if only there was a way to display a default image if the user hasn’t uploaded one.
That’s the point of the identicon jobbie, it assigns everyone a unique and easily distinguishable image when they sign up.
Louisedade,
bb_init
is called beforebb_head
I think. In your avatar.php template file it should also beclass="bbcrumb"
rather thanid="breadcrumb"
, at least if you want the same styling as everything else.I have found that there are many more mime-types out there. Heres a list of extension to mime-type associations that you might want to add. I have built up this list over the last few years of real world use of applications.
'png' => 'image/png'
'png' => 'image/x-png'
'png' => 'application/png'
'png' => 'application/x-png'
'jpg' => 'image/jpeg'
'jpg' => 'image/jpg'
'jpg' => 'image/jp_'
'jpg' => 'image/pjpeg'
'jpg' => 'image/pjpg'
'jpg' => 'image/pipeg'
'jpg' => 'application/jpg'
'jpg' => 'application/x-jpg'
'jpeg' => 'image/jpeg'
'jpeg' => 'image/jpg'
'jpeg' => 'image/jp_'
'jpeg' => 'image/pjpeg'
'jpeg' => 'image/pjpg'
'jpeg' => 'image/pipeg'
'jpeg' => 'application/jpg'
'jpeg' => 'application/x-jpg'
'gif' => 'image/gif'
'gif' => 'image/gi_'.
Of course, you would have to adapt your code to handle multiple mime types for each extension like so (or similar)…
Line 31:
$allowed_types = array(
'gif' => array(
'gif' => 'image/gif',
'gif' => 'image/gi_'
),
'jpg' => array(
'image/jpeg',
'image/jpg',
'image/jp_',
'image/pjpeg',
'image/pjpg',
'image/pipeg',
'application/jpg',
'application/x-jpg'
),
'png' => array(
'image/png',
'image/x-png',
'application/png',
'application/x-png'
)
);
$allowed_types['jpeg'] = $allowed_types['jpg'];.
Line 124:
if ($error == 0 && (!in_array($img_type, $allowed_types[$img_ext]) || !in_array($img_ext, $allowed_extns)) ) {
.
Then remove the next conditional block from line 130 to line 134 because that check is now covered in the previous conditional block.
Now then if we could integrate this with the bloglog plugin I hacked and the other Avatar plugin to produce a plugin that will do all three options – Allowing a user to choose which option they would prefer but defaulting to identicon if they don’t – Now that would be a killer plugin!!!
Yikes, what a lot of mime-types! Thanks, they won’t be hard to include in the next version. Although I’ve not actually seen many of them before, particularly the JPEG ones – I’m guessing those are created by various digital cameras?
@amethystdragon: It shouldn’t be too hard to hack all four plugins together, while also keeping them as standalone plugins. I’ve downloaded all the avatar plugins to take a look. I have an idea for a “über-avatar” layer – a single script that ties all the others together. Almost an API layer.
I don’t want to start, though, until I’ve got access to the plugin SVN repositry.
> I’m guessing those are created by various digital cameras?
I’m not sure, something microsofty I think.
Version 0.3 is now available for download from the plugin browser.
Version 0.3 is not compatible with previous versions, so you will have to go through your templates and make some changes to the template functions (check the Readme file).
New Features
* added an ‘Avatar’ tab to the profile menu
* added a ‘default avatar image’ option (if you don’t want to use fel64’s identicons — see previous posts in this thread).
* can access just the avatar’s URI for use in other plugins.
fel – how would you like me to link to the identicon’s plugin? Currently, I’m just linking to your post in this thread.
Oh and I have hacked together an Über-Avatar plugin which pulls together the various avatar plugins into one ‘api’. It iterates through some logic based on a specified order of preference (set by the admin), checking to see if the user has specified an avatar using each plugin.
For example:
Order to check: avatar-upload, bb-avatar. bb-myavatars, identicon
Has user uploaded an avatar?
— Yes: Invoke the ‘avatar-upload’ plugin template function and break.
— No… continue
Has the user specified an external URL?
— Yes, invoke ‘bb-myavatar’ template function and break.
— No… continue
Has the user got a Gravatar?
— Yes, invoke ‘bb-myavatars’ template functions and break.
— No… continue
Has the user got an identicon?
— Yes, invoke the same code as ‘avatar-upload’ and break.
— No. create an identicon and then invoke the ‘avatar-upload’ code.
It’s not perfect yet though, so no release.
- The topic ‘Plugin: Avatar Upload’ is closed to new replies.