Page 1 of 1

Avatar Size Restraints

Posted: Tue Feb 14, 2012 8:38 am
by soshannad
Hello!

I just followed your avatar system tutorial and everything is working great, however, in my website I need the image to be constrained to 75px high. I'm wondering if perhaps, there's a way to constrain it to the height rather than the width..

I have more room to play on width, but the height is non-negotiable.

Ideally it'd be 75px high x max-width of 150px or so.

Is this possible?
//updates the current users profile info
function set_profile_info($email, $about, $location, $avatar){
	$email 		= mysql_real_escape_string(htmlentities($email));
	$about 		= mysql_real_escape_string(nl2br(htmlentities($about)));
	$location 	= mysql_real_escape_string($location);

	if (file_exists($avatar)){
		$src_size = getimagesize($avatar);
		
		if ($src_size['mime'] === 'image/jpeg'){
			$src_img = imagecreatefromjpeg($avatar);
		}else if ($src_size['mime'] === 'image/png'){
			$src_img = imagecreatefrompng($avatar);
		}else if ($src_size['mime'] === 'image/gif'){
			$src_img = imagecreatefromgif($avatar);
		}else{
			$src_img = false;
		}
		
		if ($src_img !== false){
			$thumb_width = 75;
			
			if ($src_size[0] <= $thumb_width){
				$thumb = $src_img;
			}else{
			     $new_size[0] = $thumb_width;
				 $new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width;
				
				$thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
				imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
			}
			
			imagejpeg($thumb, "{$GLOBALS['path']}/user_avatars/{$_SESSION['uid']}.jpg");
			
		}
	}
	
	$sql = "UPDATE `users` SET
				`user_email` = '{$email}',
				`user_about` = '{$about}',	
				`user_location` = '{$location}'
			WHERE `user_id` = {$_SESSION['uid']}";
			
			mysql_query($sql);
		
		    
}

Re: Avatar Size Restraints

Posted: Tue Feb 14, 2012 1:15 pm
by jacek
You basically need to do the opposite
                        $thumb_height = 75;
                       
                        if ($src_size[1] <= $thumb_height){
                                $thumb = $src_img;
                        }else{
                             $new_size[1] = $thumb_height;
                                 $new_size[0] = ($src_size[0] / $src_size[1]) * $thumb_height;
                               
                                $thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
                                imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
                        }
Try something like that, the logc is that you need to fix the height to 75 and then work out how wide it should be based on the aspect ratio.

Re: Avatar Size Restraints

Posted: Tue Feb 14, 2012 8:54 pm
by soshannad
That worked like a charm, thank you!

Is there a way to implement the max-width?

Re: Avatar Size Restraints

Posted: Fri Feb 17, 2012 5:04 pm
by jacek
soshannad wrote:Is there a way to implement the max-width?
Sure, once you have calculated the new width, check to see if it is greater than some limit.