【问题标题】:Black background from transparent image透明图像的黑色背景
【发布时间】:2014-04-21 18:43:52
【问题描述】:

我使用以下代码从其他图像创建了一个图像,

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

图像已创建,但是当我尝试从 透明 图像创建 png 图像时,背景变为 黑色 颜色。

为什么会这样?如何解决?

【问题讨论】:

    标签: php image png


    【解决方案1】:

    如果您查看GD Documentation,您会发现基于 alpha 的函数。以下代码直接取自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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2022-01-17
      • 1970-01-01
      • 2016-02-22
      • 2012-06-26
      • 2015-07-04
      相关资源
      最近更新 更多