i have a image hosting site which users upload files and a thumbnail will be created. how do i remove the black border and create a best quality thumbnail from the original photo?

Note: it's working properly just that i don't want the border and i want the quality of the thumbnail to be 'best'

PHP Code:
function createThumb($pic)
{
global 
$baseWeb,$IM_path,$db,$USER;
        
$filename="upload/".$pic[name];

        
$maxx=$USER[t_width];
        
$maxy=$USER[t_height];
        
$type=$pic[type];
        
        
$USE_IM=0;  //'D:\Program Files\ImageMagick-6.2.3-Q16/';

        
switch($type)
        {
            case 
1:
                
$type "gif";
                break;
            case 
2:
                
$type "jpg";
                break;
            case 
3:
                
$type "png";
                break;
            default:
                
//convert other type to jpg file to create thumb
                
$type2=strtolower(strrchr($pic[name],'.'));
                
$orgfilename=$pic[name];
                
$pic[name]=substr($pic[name],0,-strlen($type2)).'.jpg';
                
$type='jpg';
                
$USE_IM=1;
        }

        switch(
$type)
        {
            case 
"gif":
                if( 
function_exists("imagecreatefromgif") )
                {
                    
$s_img = @imagecreatefromgif($filename);
                    break;
                }
                else
                {
                    
$type='jpg';
                }
            case 
"png":
                
$s_img = @imagecreatefrompng($filename);
                break;
            case 
"jpg":
            case 
"jpeg":
                
//convert to jpg for create thumb
                
if($USE_IM==1)
                {
                   
$cmd $IM_path."convert $filename temp/$pic[name]";
                   
system($cmd);
                   
$s_img = @imagecreatefromjpeg('temp/'.$pic[name]);
                }
                else
                
$s_img = @imagecreatefromjpeg($filename);
                break;
            default:

        }

        
$old_x=$pic[w];
        
$old_y=$pic[h];
        
//too small,not to genarate thumb:(
        
if($maxx>$old_x&&$maxy>$old_y) return 0;
        if(
$maxx==-1)$maxx=$old_x;
        if(
$maxy==-1)$maxy=$old_y;
        if((
$old_y/$old_x)>($maxy/$maxx))
        {
            
$new_y=$maxy;
            
$new_x=$new_y*($old_x/$old_y);
        }
        else
        {
            
$new_x=$maxx;
            
$new_y=$new_x*($old_y/$old_x);
        }
        if(
$picSet[fixed]==1)
        {
            
$new_x=$maxx;
            
$new_y=$maxy;
        }
        
$font   3;
        
$info_width=15;
        
$size=convertsize($pic[size],1);
        
$string "$pic[w] x $pic[h] ".$size;
        
$str_width  ImageFontWidth($font) * strlen($string);
        
$str_height ImageFontHeight($font);
        if(
$new_x>$new_y)
        {
        
$info_x=$new_x;
        
$info_y=$new_y+$info_width;
        
$str_x=($info_x-$str_width)/2;
        
$str_y=$new_y+($info_width-$str_height)/2;
        
$imagestring='imagestring';
        }
        else
        {
        
$info_x=$new_x+$info_width;
        
$info_y=$new_y;
        
$str_x=$new_x+($info_width-$str_height)/2;
        
$str_y=$info_y-($info_y-$str_width)/2;
        
$imagestring='imagestringup';
        }
        
$info_image = @imagecreatetruecolor($info_x$info_y);
        
$black = @ImageColorAllocate($info_image0,);
        
$white = @ImageColorAllocate($info_image255255 ,255 );
        
$blue = @ImageColorAllocate($info_image0x000x00 ,0xFF );
        @
$imagestring ($info_image$font$str_x$str_y,  $string$white);

        
$return[id]=$id;
        
$return[type]=$type;
        
$return[url]=$url;
        
$return[width]=$new_x;
        
$return[height]=$new_y;
        
$d_image = @imagecreatetruecolor($new_x$new_y);
        @
imagecopyresized($d_image$s_img0000$new_x$new_y$old_x$old_y);
        
$d_file="thumb_".$filename;

        @
imagecopyresized($info_image$d_image0000$new_x$new_y$new_x$new_y);

        
$d_file="thumb/thumb_".$pic[name];

        if(
$type=='jpg'||$type=='jpeg')
        {
            if(
$USE_IM==0)
            @
imagejpeg($info_image,$d_file);
        }
        if(
$type=='gif')
        {
             if( 
function_exists("imagegif"))
             {
                  @
imagegif($info_image,$d_file);
             }
             else
             {
                  @
imagejpeg($info_image,$d_file);
             }
        }
        if(
$type=='png')
        {
             @
imagepng($info_image,$d_file);
        }

        if(
$USE_IM==1)
        {
           
//make thumb on the jpg file
           
@imagejpeg($info_image,"temp/thumb_".$pic[name]);
           
//then convert to the orgfilename filetype thumb
           
$cmd $IM_path."convert temp/thumb_{$pic[name]} thumb/thumb_$orgfilename";
           
system($cmd);

           @
unlink("temp/thumb_{$pic[name]}");
           @
unlink("temp/{$pic[name]}");
        }
        @
ImageDestroy($s_img);
        @
ImageDestroy($d_image);
        @
ImageDestroy($info_image);
        return 
$return;