Jump to content

Small issue with avatar uploading...


stelthius

Recommended Posts

Ok guys after developing my avatar system ive hit a tight spot.. Once a user uploads a avatar, if the avatar they upload is smaller than what the size limit is it will give the image a black background and im seriously unsure how to stop this from happening, below is the code i have that handles the upload...

 

/* Avatar checking */
$field = "avatar"; //Use field name for avatar
$uploaded_dir = "/home/phpcorec/public_html/include/avatar/$subuser/";
$avatar_path = $uploaded_dir . $avatar_name;
$width = 100;
$height = 100;
$thumbsize = 100;

if(empty($avatar_name))
{
$avatar_name = 'noavatar.png';
$avatar_path = "include/avatar/noavatar.png";
mkdir("/home/phpcorec/public_html/include/avatar/$subuser", 0777);
$destination = "include/avatar/$subuser/noavatar.png";
if(!(copy($avatar_path, $destination)))
{
echo "The specified file could not be copied. Please try again.", "\n";
}

} else {
if(!$avatar_tmpname)
{
	$form->setError($field, "* Avatar does not exist");
} else {
	$num_char=strlen($avatar_name);
	if($num_char > 20)
	{
		$form->setError($field, "* Your Avatar name has $num_char characters, the limit is 20");
	} else {
		if($avatar_size > 102400)
		{
			$form->setError($field, "* Your avatar is $avatar_size, the maximum upload size is 100kb");
		} elseif($avatar_type == "image/jpeg") 
		{
			mkdir("/home/phpcorec/public_html/include/avatar/$subuser", 0777);

			/* Resize Image */
			list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
			$ratio_orig = $width_orig/$height_orig;
			if ($width/$height > $ratio_orig)
			{
				$width = $height*$ratio_orig;
			} else {
				$height = $width/$ratio_orig;
			}
			/* Done Resize Image */

			/* Resize Image */
			$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
			$image = imagecreatefromjpeg($avatar_tmpname);
			imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

			imagejpeg($image_p, $avatar_tmpname, 100);
			/* Done Resize Image */

			move_uploaded_file($avatar_tmpname, "$avatar_path");
		} else if($avatar_type == "image/pjpeg")
		{
			mkdir("/home/phpcorec/public_html/include/avatar/$subuser", 0777);

			/* Resize Image */
			list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
			$ratio_orig = $width_orig/$height_orig;
			if ($width/$height > $ratio_orig) {
				$width = $height*$ratio_orig;
			} else {
				$height = $width/$ratio_orig;
			}
			/* Done Resize Image */

			/* Resize Image */
			$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
			$image = imagecreatefromjpeg($avatar_tmpname);
			imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

			imagejpeg($image_p, $avatar_tmpname, 100);
			/* Done Resize Image */

			move_uploaded_file($avatar_tmpname, "$avatar_path");
		} else if($avatar_type == "image/png"){
			mkdir("/home/phpcorec/public_html/include/avatar/$subuser", 0777);

			/* Resize Image */
			list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
			$ratio_orig = $width_orig/$height_orig;
			if ($width/$height > $ratio_orig) {
				$width = $height*$ratio_orig;
			} else {
				$height = $width/$ratio_orig;
			}
			/* Done Resize Image */

			/* Resize Image */
			$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
			$image = imagecreatefrompng($avatar_tmpname);
			imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

			imagepng($image_p, $avatar_tmpname, 100);
			/* Done Resize Image */

			move_uploaded_file($avatar_tmpname, "$avatar_path");
		} else if($avatar_type == "image/gif"){
			mkdir("/home/phpcorec/public_html/include/avatar/$subuser", 0777);

			/* Resize Image */
			list($width_orig, $height_orig) = getimagesize($avatar_tmpname);
			$ratio_orig = $width_orig/$height_orig;
			if ($width/$height > $ratio_orig) {
				$width = $height*$ratio_orig;
			} else {
				$height = $width/$ratio_orig;
			}
			/* Done Resize Image */

			/* Resize Image */
			$image_p = imagecreatetruecolor($thumbsize, $thumbsize);
			$image = imagecreatefromgif($avatar_tmpname);
			imagecopyresampled($image_p, $image, -($width/2) + ($thumbsize/2), -($height/2) + ($thumbsize/2), 0, 0, $width, $height, $width_orig, $height_orig);

			imagegif($image_p, $avatar_tmpname, 100);
			/* Done Resize Image */

			move_uploaded_file($avatar_tmpname, "$avatar_path");
		} else {
			$form->setError($field, "* $avatar_type is wrong extension");
		}
	}
}
}
}

 

And here is a snapshot of what happens when the image uploaded is smaller than the maximum size allowed

 

gbseoubg.jpg notice the black background on the image, thats what my script is putting there.. anyone got any ideas as to why its doing this ive spent hours trying to find out.

 

Any help is really appretiated.

Link to comment
Share on other sites

replace.

$image_p = imagecreatetruecolor($thumbsize, $thumbsize);

 

to

 

if($height>$thumbsize){$height=$thumbsize;}

$image_p = imagecreatetruecolor($width, $height);

 

because resize scripts use ratio resizing, so width will be 100

 

 

that means, if height of avatar is 50px then it will remain 50px and your image that your are making with php is going to be 50px not 100px... I don't see a way around thing with jpg.  if you mandated png or gif, you could make it transparent.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.