stelthius Posted May 23, 2009 Share Posted May 23, 2009 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 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. Quote Link to comment Share on other sites More sharing options...
BK87 Posted May 23, 2009 Share Posted May 23, 2009 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. Quote Link to comment Share on other sites More sharing options...
stelthius Posted May 24, 2009 Author Share Posted May 24, 2009 Hi, that doesnt work as it resizes the black background to the size of the image.. i tried that already. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.