MyBB Community Forums

Full Version: Getting Avatar to Resize on Portal and Usercp
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Sorry if I didn't post this in the correct area.

I'm currently trying to learn more php so I can accomplish more web developing without requiring so much support. I have experience in other languages so it means interpreting code a lot easier. Today I made a nice little accomplishment I feel.

I wanted to get my avatars on the usercp and portal to resize itself like it does on the postbit if the avatar is too big.

I wanted a system that allows a member to upload any size image(dimension wise) and it will resize it to 125x125 so users don't have to worry about resizing their avatars.

First I just tried setting the resize in the settings to 125x125 and removing the dimension limit. This raised a problem still. I tried uploading a 400x400 avatar and it was the original size in my usercp and portal announcements. Dodgy

I figured it was a pretty public issue so I googled around a bit and couldn't find a clear answer on how to solve the problem. I did get a hint from a topic I found, I took that hint and opened up my portal.php file, looked for the variable $avatar_width_height. I could have simply change the dimensions from 125x125 in that variable itself but I felt that was the sloppy way of doing it. Cool

I digged around in some other files to see if I could get an idea of how to resize an image in php based off the settings in the configuration tab on the admin panel. I found a piece of code in the memberlist.php file and turned it over to work in portal.php

Find this:
if($avatar_dimensions[0] && $avatar_dimensions[1])
{
	$avatar_width_height = "width=\"{$avatar_dimensions[0]}\" height=\"{$avatar_dimensions[1]}\"";
}

Replace with this:
if($avatar_dimensions[0] && $avatar_dimensions[1])
{
	list($max_width, $max_height) = explode("x", my_strtolower($mybb->settings['memberlistmaxavatarsize']));
	if($avatar_dimensions[0] > $max_width || $avatar_dimensions[1] > $max_height)
	{
		require_once MYBB_ROOT."inc/functions_image.php";
		$scaled_dimensions = scale_image($avatar_dimensions[0], $avatar_dimensions[1], $max_width, $max_height);
		$avatar_width_height = "width=\"{$scaled_dimensions['width']}\" height=\"{$scaled_dimensions['height']}\"";
	}
	else
	{
		$avatar_width_height = "width=\"{$avatar_dimensions[0]}\" height=\"{$avatar_dimensions[1]}\"";
	}
}

I used the same concept with my usercp.php

Even though it was just copypasting with a few adjustments , I feel quite proud of myself that I was able to determine what to do by simply looking at it. That is pretty much how I learned the other languages I know, copypasting until I grabbed an understanding of what exactly I was copying and pasting was accomplishing, then I started being able to actual write out code, so it's a milestone for me I guess. Blush

I hope this will be of some help for anyone else that was facing the same issue I was.
Congratz! Phazd , but the best way to learn is by playing with the code. Copy and pasting is good too, but try and build your own lego castle from time to time it helps you get burried deeper in the world of programming.
(2014-02-19, 06:18 PM)KLOX94 Wrote: [ -> ]Congratz! Phazd , but the best way to learn is by playing with the code. Copy and pasting is good too, but try and build your own lego castle from time to time it helps you get burried deeper in the world of programming.
That is true, I'm going to try to play around a bit more with php. As I said I have a good understanding of coding because I know other languages. Python, basic perl, lua, c++, and pawn. I also know a basic amount of reserving.

It sucks I didn't learn php earlier on, now I am going to need to know it lol.
(2014-02-19, 06:25 PM)Phazd Wrote: [ -> ]It sucks I didn't learn php earlier on, now I am going to need to know it lol.

Google is your friend Wink