0

I have script:

$path = "uploads/";

$img = $_FILES['photoimg']['tmp_name'];
$dst = $path . $_FILES['photoimg']['name'];

if (($img_info = getimagesize($img)) === FALSE)
  die("Image not found or not an image");

$width = $img_info[0];
$height = $img_info[1];

switch ($img_info[2]) {
  case IMAGETYPE_GIF  : $src = imagecreatefromgif($img);  break;
  case IMAGETYPE_JPEG : $src = imagecreatefromjpeg($img); break;
  case IMAGETYPE_PNG  : $src = imagecreatefrompng($img);  break;
  default : die("Unknown filetype");
}

$tmp = imagecreatetruecolor($width, $height);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $width, $height, $width, $height);
imagejpeg($tmp, $dst.".jpg");

How to convert uploaded images always into JPG format?

This working perfect, but if file is png and have transparency then my new image have black background. Can i change this color? I would like have white background.

Community
  • 1
  • 1
Steven Sacradoor
  • 253
  • 2
  • 3
  • 5

1 Answers1

3

Open png with transparency

$src = imagecreatefrompng($img);
imagealphablending($src, true);

While creating your empty file, fill it with white background (as default empty image is black)

$tmp = imagecreatetruecolor($width, $height);
imagefill($tmp, 0, 0, imagecolorallocate($tmp, 255, 255, 255));
Peter
  • 16,453
  • 8
  • 51
  • 77