【问题标题】:How to replace black background with white when resizing/converting PNG images with transparent backgrounds to JPEG.在将具有透明背景的 PNG 图像大小调整/转换为 JPEG 时如何用白色替换黑色背景。
【发布时间】:2011-08-07 00:47:53
【问题描述】:

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

【问题讨论】:

  • 这个 imagecolorallocate 有什么作用?它为 png 接收 4 个参数,不应该为 alpha 通道接收 5 个参数吗?
  • 代码不是我写的,我是菜鸟。我不知道你的问题的答案。对不起,我不能帮你。
  • 这实际上都在手册中。 imagecolorallocate - 返回一个颜色标识符,表示由给定 RGB 分量组成的颜色。 对于 alpha 透明度,您需要使用imagecolorallocatealpha,它确实添加了第 5 个参数:php.net/manual/en/function.imagecolorallocatealpha.php
  • @Jeff - 当您自己解决问题时,为什么不写一个答案并接受它作为解决方案?这样一来,您的问题就不再“悬而未决”。

标签: php image resize gd


【解决方案1】:
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]

【讨论】:

    【解决方案2】:

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

    adding background color to transparent images using gd and php

    【讨论】:

      【解决方案3】:

      好的,这个函数来自php。由于我从未在 php 上处理过图像,所以我不知道。

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

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

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

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

      【讨论】:

      • 我需要为所有功能保留 imagejpeg,因为所有新图像都必须是 jpg。我进行了您建议的其他更改,但生成的 jpg 在有透明度的地方仍然有黑色。我不需要保持透明度,我只需要将黑色变为白色。谢谢!
      【解决方案4】:

      试试这样(未测试):

      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&lt;256; $i++) { imagecolorallocate($file, $i, $i, $i); } 部分看起来很奇怪。

      【讨论】:

        【解决方案5】:

        图片真彩后添加线条:

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

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

        【讨论】:

          【解决方案6】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-10-21
            • 2015-02-03
            • 2019-12-13
            • 2017-11-07
            • 2011-09-24
            • 1970-01-01
            • 2011-04-23
            相关资源
            最近更新 更多