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

发布于 2024-11-01 23:24:24 字数 3140 浏览 1 评论 0原文

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

I am using a script that lets users upload images. The script resizes and converts the images to JPEG.

The problem I have is when a PNG with transparency is uploaded, the resulting JPEG image is black where there was transparency.

How can I edit the below script to replace the black with white? It already does this for GIF's but not for PNG's.

 // 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;
}

I tried editing the case "png": portion to match that of the case "gif": code but the resulting JPEG is completely white.

UPDATE:

I fixed it myself.

Thanks, Everyone, for contributing!

I replaced:

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;

with:

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浸婚纱 2024-11-08 23:24:24
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]
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]
眼眸里的快感 2024-11-08 23:24:24

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

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

I found this other similar question here on this site. I hope it is helpful.

adding background color to transparent images using gd and php

饮湿 2024-11-08 23:24:24

好吧,这个函数来自php。因为我从来没有在 php 上处理过图像,所以我不知道。

因此,图像由三种颜色组成:RGB(红、绿、蓝)。对于 PNG,它还由另一个称为 alpha 的滤镜组成,即透明度,

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

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

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

Ok, this function is from php. As I've never worked with images on php, so I didn't know it.

So, images a composed of three colors: RGB (red, green, blue). In case of PNG, it's also composed of another filter, called alpha, which is the transparency

SO, just for PNG, you should switch imagecolorallocate to imagecolorallocatealpha($file,$i,$i,$i,$i);

This should make your images transparent. Does this solve your problem or do you really need them in white background?

Edit:
I also noted that you are using imagejpg function in all cases. Switch them to the corresponding function, i.e., imagepng, imagebmp, imagegif

山色无中 2024-11-08 23:24:24

尝试这样(未测试):(

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

在 $file 图像上预绘制白色背景)

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

Try like this (not tested):

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

(predraw a white background on $file image)

Also, the for($i=0; $i<256; $i++) { imagecolorallocate($file, $i, $i, $i); } part looks strange.

狼亦尘 2024-11-08 23:24:24

在图像真彩色之后添加线条:

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

这将有助于所有格式的 mailtaining alpha。如果您没有得到答复,请 Ping。

After Image true colour add the lines:

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

This will help in mailtaining alpha for all formats. Ping if u dont get answer.

本王不退位尔等都是臣 2024-11-08 23:24:24

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

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

for me these settings effected a black background, to get white change it to:

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

My example:

$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文