Topic: Resize avatar if resolution is greater than requested amount.

I find it a pain to manually resize an image that is 10 pixels larger than maximum size. Some people don't even know how to resize an image, actually.

Re: Resize avatar if resolution is greater than requested amount.

Doing it with PHP would horribly ruin the quality of the image.

Re: Resize avatar if resolution is greater than requested amount.

And I don't think we're going to add GD, etc to the list of requirements for people wink
Moved to Modifications

Re: Resize avatar if resolution is greater than requested amount.

Imagemagick is not so bad. However, that's more of a job for the future 1.3 extensions, no?

Re: Resize avatar if resolution is greater than requested amount.

elbekko wrote:

Doing it with PHP would horribly ruin the quality of the image.

What kind of quality would you need in a 60x60 picture, anyway?

Re: Resize avatar if resolution is greater than requested amount.

I've got a community where many members couldn't resize an image to save their life.  In the absence of this cool mod, I've pointed some of the more talented ones to download irfanview (for windows), or just go to one of the many sites that offer online resizing for free.

This would be an interesting plugin for sure.

Rob Ludlow 
www.Nifty-Stuff.com - Repository of all Stuff Nifty!
www.reviewum.com - Professor Ratings + Teacher Reviews

7 (edited by thame^ 2007-12-18 00:23)

Re: Resize avatar if resolution is greater than requested amount.

I was looking for the same thing, but found nothing.  So I decided to make it myself.  I'm not exactly an expert at PHP, and I haven't had the opportunity to test my modification extensively (other than on my own server setup), but hey it works for me.

If that doesn't scare you off, this is what I ended up with: http://ohok.nl/stuff/profile.txt (or do you like pretty colours?)

Basically what it does is resize any image that's wider and/or higher than allowed, to whatever's set to be the maximum, maintaining the original aspect ratio.  Since resizing the image also changes the file size, I had to move the file size check until after the resizing is done, and also comment out the MAX_FILE_SIZE hidden form field, as big files can be made smaller now.

It's also supposed to scale down gracefully if the GD library isn't installed, or if the image format isn't supported by it.  In either case the script should return to the default PunBB behaviour and simply reject the image if it's too large.  The only difference is the absence of the MAX_FILE_SIZE hidden form field (not that I personally mind, since the output PHP gives me when using it is a meaningless blob of code anyway).  But this doesn't really affect anything, since the file size is double checked by the script anyway.

I'd turn this into a nicely distributed 'mod' or 'plugin' but I have no idea how, sorry.  Hope this will be of help to someone anyway.

P.S. My modifications start at around line 350 and end with the now commented out MAX_FILE_SIZE hidden form field, which is now at line 463.  I tried to play nice and comment my code, but if you have any questions feel free to ask.

EDIT: This was turned into a proper mod some time ago.  Please refer to http://punbb.org/forums/viewtopic.php?id=16883 rather than following the instructions in this post.

8 (edited by Peter 2007-03-07 00:10)

Re: Resize avatar if resolution is greater than requested amount.

Wow! Just what I needed! And it actually seems to work!! smile

Bedankt!!

To get it to work I had to change the Upload directory from 'img/avatars' to 'forum/img/avatars', even though the base URL is http://mywebsite.com/forum.

After upload I get this message though:

The file you tried to upload is larger than the maximum allowed 10240 bytes

And then when I delete the avatar I get this:

Bad HTTP_REFERER. You were referred to this page from an unauthorized source. ...

But below that there's this:

Avatar deleted. Redirecting...

And the operation is succesfull.

The errors are probably caused by a mistake in these address lines:

$src_img = @imagecreatefromgif($pun_config['o_avatars_dir'].'/'.$id.'.tmp');

I have profile.php at the root and PunBB in a '/forum' folder, so I probably need a PUN_ROOT in there somewhere. Not sure what the syntax should be.

edit:
I've added PUN_ROOT like this:

@unlink(PUN_ROOT.$pun_config['o_avatars_dir'].'/'.$id.$extensions[0]);
if ($type == 1) $src_img = @imagecreatefromgif(PUN_ROOT.$pun_config['o_avatars_dir'].'/'.$id.'.tmp');

I don't get syntax errors, but I get the same errors as before (including bad referrer) and the script doesn't do anything anymore. sad

What else am I missing...?

Bottom line: As thame^ wrote it the code works great, but...

1. I have to add forum/ to the img/avatars in admin-options
2. I get the 'larger than the maximum allowed' error at uploading
3. I get the 'Bad HTTP_REFERER' error when I delete an avatar

edit:

Simply removing these lines solves problem #2:

            // Make sure the file isn't too big
            if ($uploaded_file['size'] > $pun_config['o_avatars_size'])
                message($lang_profile['Too large'].' '.$pun_config['o_avatars_size'].' '.$lang_profile['bytes'].'.');

Can I just do that?

Still need to fix #1 and 3...

edit:

I set the Base URL now to root, mywebsite.com instead of mywebsite.com/forum. That fixes the Bad HTTP_REFERER and viewing posts in /forum still works.

I know, my customization is a mess...

Re: Resize avatar if resolution is greater than requested amount.

Hi Peter, glad to hear my modifcation's working out for you.. sort of.

Your situation is a little unique, since you have profile.php outside of the PUN_ROOT.  The 'Bad HTTP_REFERER' errors you're getting when attempting to delete an avatar would've occured with or without my changes to profile.php -- it has to do with the 'confirm_referrer('profile.php');' line.  Though I can't help you with that specific error, I think I may know how to fix the other errors.

The other errors you're getting is because the $pun_config['o_avatars_dir'] config variable ('Avatar upload directory') is relative to the PUN_ROOT.  Since your profile.php is outside that, it's obviously not going to work.  You've already started off on the right foot by editing a few instances of $pun_config['o_avatars_dir'], but the real solution would be to replace ALL instances of it to 'PUN_ROOT.$pun_config['o_avatars_dir']'.  Or, if you're lazy like me, you could also just redeclare the variable somewhere at the top of profile.php, as in:

$pun_config['o_avatars_dir'] = PUN_ROOT.$pun_config['o_avatars_dir'];

That should do the trick for you.. I think.  Also, deleting the file size check part is a bad idea, as the script would then simply accept images of any size, which I would not exactly recommend.  The file size check should work just fine if everything's laid out correctly, I didn't really touch that part of the code (though I did move it around a little).

Hope this helps.

10

Re: Resize avatar if resolution is greater than requested amount.

Thanks again thame^.

I did try to edit ALL instances of the o_avatars_dir, but couldn't get it to work. I haven't tried the "redeclare the variable" method yet.

I'm now trying to find out more about the consequences of rearranging PunBB folders.