I use the below code to resize images (jpg, png, gif). The code is working perfectly. But the problem is after resizing the images, all transparent images (both png and gif) have a black background.
How can I maintain the transparency so that resized images will have not have black background?
$target = 'uploads/'.$newname;
move_uploaded_file( $_FILES['file']['tmp_name'], $target);;
$filename=$newname;
if($ext=='jpg'||$ext=='jpeg') {
$im = imagecreatefromjpeg('uploads/'.$filename);
} else if ($ext=='gif') {
$im = imagecreatefromgif('uploads/'.$filename);
} else if ($ext=='png') {
$im = imagecreatefrompng('uploads/'.$filename);
}
$ox = imagesx($im);
$oy = imagesy($im);
$nm = imagecreatetruecolor(400, 300);
imagecopyresized($nm, $im, 0,0,0,0,400,300,$ox,$oy);
imagejpeg($nm, 'thumbnails/' . $filename);