¿How to change the background Black to Transparent of an image with PHP? : How to resize png and gif image and store transparent background using php?

This is my code

Your will resize the image to 200x200 px and climb to PIE dir. It's a good job. But when used with png or gif image (transparent bg). It will change bg to black. How can I make srote transparent bg after resizing the image?


**Original:**

http://imgur.com/a/8FicX

**Copy:**

 http://imgur.com/a/7yQo8

    include 'compressImage.php';
        <?php
        $widthArray = array(200);
        foreach($widthArray as $newwidth)
        {						
           compressImage($ext,$tmp,$path,$userID.$actual_image_name,$newwidth);
        }
        ?>

**compressImage.php:**

    <?php
    //Compress Image 
    function compressImage($ext,$uploadedfile,$path,$actual_image_name,$newwidth)
    {
    	if($ext=="jpg" || $ext=="jpeg" )
    	{
    	$src = imagecreatefromjpeg($uploadedfile);
    	}
    	else if($ext=="png")
    	{
    	$src = imagecreatefrompng($uploadedfile);
    	}
    	else if($ext=="gif")
    	{
    	$src = imagecreatefromgif($uploadedfile);
    	}
    	else
    	{
    	$src = imagecreatefrombmp($uploadedfile);
    	}
    																	
    	list($width,$height)=getimagesize($uploadedfile);
    	$newheight=($height/$width)*$newwidth;
    	$tmp=imagecreatetruecolor($newwidth,$newheight);
    	imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    	$filename = $path.$newwidth.'_'.$actual_image_name;
    	imagejpeg($tmp,$filename,100);
    	imagedestroy($tmp);
    	return $filename;
    }
    ?>


  [1]: https://i.stack.imgur.com/5YwZv.png