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

时间:2011-04-16 19:22:30

标签: php image resize gd

我正在使用一个允许用户上传图片的脚本。该脚本调整大小并将图像转换为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;

6 个答案:

答案 0 :(得分:2)

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]

答案 1 :(得分:1)

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

adding background color to transparent images using gd and php

答案 2 :(得分:0)

好的,这个功能来自php。由于我从未使用过php上的图像,所以我不知道。

因此,图像由三种颜色组成:RGB(红色,绿色,蓝色)。在PNG的情况下,它也由另一个名为alpha的过滤器组成,它是透明度

因此,仅针对PNG,您应该将imagecolorallocate切换为imagecolorallocatealpha($file,$i,$i,$i,$i);

这应该可以使您的图像透明。这会解决您的问题,还是您真的需要它们在白色背景下?

修改 我还注意到你在所有情况下都使用imagejpg功能。将它们切换到相应的功能,即imagepng,imagebmp,imagegif

答案 3 :(得分:0)

尝试这样(未经测试):

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); }部分看起来很奇怪。

答案 4 :(得分:0)

在图像真彩色之后添加以下行:

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

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

答案 5 :(得分:0)

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

$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);