Как заменить черный фон с белым при изменении размера / преобразовании изображений PNG с прозрачным фоном в JPEG.

Я использую сценарий, который позволяет пользователям загружать изображения. Сценарий изменяет размер и преобразует изображения в формат JPEG.

У меня проблема заключается в том, что когда загружается PNG с прозрачностью, полученное изображение JPEG становится черным там, где была прозрачность.

Как я могу отредактировать приведенный ниже сценарий, чтобы заменить изображение черный с белым? Он уже делает это для файлов GIF, но не для PNG.

 // RESIZE IMAGE AND PUT IN USER DIRECTORY
  switch($this->file_ext)
{
    case "gif":
      $file = imagecreatetruecolor($width, $height);
      $new = imagecreatefromgif($this->file_tempname);
      $kek=imagecolorallocate($file, 255, 255, 255);
      imagefill($file,0,0,$kek);
      imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);
      imagejpeg($file, $photo_dest, 100);
      ImageDestroy($new);
      ImageDestroy($file);
      break;

    case "bmp":
      $file = imagecreatetruecolor($width, $height);
      $new = $this->imagecreatefrombmp($this->file_tempname);
      for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }
      imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
      imagejpeg($file, $photo_dest, 100);
      ImageDestroy($new);
      ImageDestroy($file);
      break;

    case "jpeg":
    case "jpg":
      $file = imagecreatetruecolor($width, $height);
      $new = imagecreatefromjpeg($this->file_tempname);
      for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }
      imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);
      imagejpeg($file, $photo_dest, 100);
      ImageDestroy($new);
      ImageDestroy($file);
      break;

    case "png":
      $file = imagecreatetruecolor($width, $height);
      $new = imagecreatefrompng($this->file_tempname);
      for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }
      imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
      imagejpeg($file, $photo_dest, 100);
      ImageDestroy($new);
      ImageDestroy($file);
      break;
  } 

  chmod($photo_dest, 0777);

  return true;
}

Я попытался отредактировать часть case "png": , чтобы она соответствовала части кода case "gif": , но получившийся JPEG полностью белый.

ОБНОВЛЕНИЕ:

Я исправил это сам.

Спасибо всем за участие!

Я заменил:

case "png":
      $file = imagecreatetruecolor($width, $height);
      $new = imagecreatefrompng($this->file_tempname);
      for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }
      imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height); 
      imagejpeg($file, $photo_dest, 100);
      ImageDestroy($new);
      ImageDestroy($file);
      break;

на:

case "png":
      $file = imagecreatetruecolor($width, $height);
      $new = imagecreatefrompng($this->file_tempname);
      $kek=imagecolorallocate($file, 255, 255, 255);
      imagefill($file,0,0,$kek);
      imagecopyresampled($file, $new, 0, 0, 0, 0, $width, $height, $this->file_width, $this->file_height);
      imagejpeg($file, $photo_dest, 100);
      ImageDestroy($new);
      ImageDestroy($file);
      break;
20
задан michaeltwofish 16 March 2013 в 06:38
поделиться