如何在调整大小/将透明背景的PNG图像转换为JPEG时将黑色背景替换为白色


How to replace black background with white when resizing/converting PNG images with transparent backgrounds to 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;
}

我尝试编辑大小写"png":部分以匹配大小写"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;
switch($image_extension){
  case 'gif':
  case 'GIF':
    $image_orig_resource = imagecreatefromgif($image_orig_path);
    break;
  case 'png':
  case 'PNG':
    $image_orig_resource = imagecreatefrompng($image_orig_path);
    break;
  case 'jpg':
  case 'jpeg':
  case 'JPG':
  case 'JPEG':
    $image_orig_resource = imagecreatefromjpeg($image_orig_path);
    break;
  default:
    throw new Exception('Extension not supported');
}
$image_resource = imagecreatetruecolor($width, $height);
imagefill($image_resource, 0, 0, imagecolorallocate($image_resource, 255, 255, 255));  // white background;
imagecopyresampled($image_resource, $image_orig_resource, 0, 0, 0, 0, $width, $height, $image_orig_width, $image_orig_height);
imagejpeg($image_resource, $image_path, 100); // quality: [0-100]

我在这个网站上发现了另一个类似的问题。我希望这对你有帮助。

使用gd和php为透明图像添加背景色

好了,这个函数来自php。因为我从来没有在php上使用过图像,所以我不知道它。

所以,图像由三种颜色组成:RGB(红、绿、蓝)。对于PNG,它也是由另一个叫做alpha的滤镜组成的,也就是透明度

所以,对于PNG,你应该将imagecolorallocate切换为imagecolorallocatealpha($file,$i,$i,$i,$i);

这应该使您的图像透明。这能解决你的问题吗?还是你真的需要白色的背景?

编辑:

我还注意到在所有情况下都使用imagejpg函数。将它们切换到对应的函数,即imagepng、imagebmp、imagegif

尝试如下(未测试):

case "png":
  $file = imagecreatetruecolor($width, $height);
  $new = imagecreatefrompng($this->file_tempname);
  imagefilledrectangle ($file, 0, 0, $width, $height, imagecolorallocate($file, 0,0,0))

(在$file image上预画一个白色背景)

另外,for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); }部分看起来很奇怪。

After Image true color添加line:

    $file = imagecreatetruecolor($width, $height);
    $background = imagecolorallocate($file, 0, 0, 0);
    imagecolortransparent($file, $background);
    imagealphablending($file, false);
    imagesavealpha($file, true);

这将有助于维护所有格式的alpha。Ping如果你没有得到答案。

对于我来说,这些设置影响了黑色背景,要获得白色,请更改为:

$background = imagecolorallocate($file, 255, 255, 255);

我的例子:

$NewImageWidth      = 275; //New Width of Image
$NewImageHeight     = 275; // New Height of Image
$Quality        = 85; //Image Quality
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
$background = imagecolorallocate($NewCanves, 255, 255, 255);
imagefilledrectangle($NewCanves, 0, 0, $NewWidth, $NewHeight, $background);
$NewImage = imagecreatefrompng($SrcImage);
imagejpeg($NewCanves,$DestImage,$Quality);