I have this PHP script that receives an uploaded image. The uploaded image is saved in a temp folder, and then this script resamples the image and saves it to the correct folder. A user can upload either JPG, PNG or GIF files. This script only caters for JPG files though.
How would I modify this script to resize both PNG’s and GIF’s without losing transparency?
$targ_w = $targ_h = 150;
$jpeg_quality = 90;
$src = $_POST['n'];
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
$new_src = str_replace('/temp','',$_POST['n']);
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
imagejpeg($dst_r,$new_src,$jpeg_quality);
JPEG images can’t have transparent background.
Instead you can make the image based on
imagesavealpha():It will make a PNG from both PNG and GIF (that have transparent background, and resize to 150×150.
This is just an example, as it does not constrain proportions.