在将具有透明背景的PNG图像调整大小/转换为JPEG时,如何用白色替换黑色背景。 [英] How to replace black background with white when resizing/converting PNG images with transparent backgrounds to 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);
休息;
我在这个网站上发现了这个类似的问题。我希望它有所帮助。
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屋!