在将具有透明背景的PNG图像调整大小/转换为JPEG时,如何用白色替换黑色背景。 [英] How to replace black background with white when resizing/converting PNG images with transparent backgrounds to JPEG.

查看:126
本文介绍了在将具有透明背景的PNG图像调整大小/转换为JPEG时,如何用白色替换黑色背景。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的脚本可以让用户上传图片。该脚本调整大小并将图像转换为JPEG。



我遇到的问题是上传了具有透明度的PNG时,生成的JPEG图像是黑色的,其中有透明度。 / p>

如何编辑以下脚本以用白色​​替换黑色?它已经为GIF做了这个,但不是用于PNG。

  //调整图像并输入用户目录
开关( $ this-> file_ext)
{
casegif:
$ 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);
休息;

casebmp:
$ 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);
休息;

casejpeg:
casejpg:
$ 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);
休息;

casepng:
$ 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);
休息;
}

chmod($ photo_dest,0777);

返回true;
}

我尝试编辑案例png:部分匹配案例gif:代码,但生成的JPEG格式为白色。



更新:



我自己修好了。



谢谢大家,为了贡献!



我更换了:

  casepng:
$ file = imagecreatetruecolor($宽度,高度);
$ 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);
休息;

with:

  casepng:
$ 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);
休息;


解决方案

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



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


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;

解决方案

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

这篇关于在将具有透明背景的PNG图像调整大小/转换为JPEG时,如何用白色替换黑色背景。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆