简体   繁体   English

透明图像的黑色背景

[英]Black background from transparent image

I created an image from other image with the following code, 我使用以下代码从其他图片创建了图片,

 imagepng(imagecreatefromstring(file_get_contents(destination path)),source_path);

The image was created,But when i try to create a png image from a transparent image,The background become black color. 该图像已创建,但是当我尝试从透明图像创建png图像时,背景变为黑色

Why this happens?How to solve this? 为什么会这样?如何解决呢?

If you look at the GD Documentation you'll find the alpha based functions. 如果查看GD文档 ,则会发现基于alpha的函数。 Taken directly from imagesavealpha() is the following code that opens a png and outputs it with transparency maintained; 以下代码直接从imagesavealpha()中获取,该代码打开png并在保持透明性的情况下将其输出;

// Load a png image with alpha channels
$png = imagecreatefrompng('./alphachannel_example.png');

// Do required operations
// (So any resizing/rotating/cropping etc)

// Turn off alpha blending and set alpha flag
imagealphablending($png, false);
imagesavealpha($png, true);

// Output image to browser
header('Content-Type: image/png');

imagepng($png);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM