I know PHP+GD Transparency issues have been beat to death on this and many other sites, but I've followed all the recommendations and I can't seem to fix my issue.

我知道PHP+GD透明度问题在这个网站和许多其他网站上已经被打得一败涂地,但我已经遵循了所有的建议,我似乎无法解决我的问题。

First, the explanation:

首先,解释:

I am trying to overlay one image on top of another one. They both have areas that are transparent. As a demo that I know should look a particular way, I am trying to overlay a checkmark on top of a blue arrow shape that I created.

我想把一个图像叠加在另一个图像上。它们都有透明的领域。作为一个我知道应该看起来特别的演示,我正在尝试在我创建的蓝色箭头形状上覆盖一个复选标记。

Here are the two images:

以下是两幅图片:

Now to my code:

现在我的代码:

I am using a library/API that I built to take a TINY bit of the pain away from editing images with PHP+GD. It's still in it's infancy, but the pertinent files are:

我正在使用一个库/API,我构建这个库/API是为了减少使用PHP+GD编辑图像的痛苦。它仍处于起步阶段,但相关文件是:

The Base Class
The Main Loader
The (poorly named) Combine Class

基类主装入器(名称不正确)组合类

I am running the code using the following script:

我使用以下脚本运行代码:

<?php
    require_once('Image.php');

    header("Content-Type: image/png");

    $img = new Image();
    $over = new Image();

    $img->source = "arrow.png";
    $over->source = "chk-done_24.png";

    $img->Combine->Overlay($over, 20, 20, 0, 0, $over->width, $over->height);
    $img->output();
    $img->clean();
    unset($img);
?>

I expect the output to be something like this:

我希望输出是这样的:

But instead I get this:

但我得到的却是:

I would totally understand the issue if the filled area was white or black, but filling with the blue color just doesn't make any sense to me.

如果填充的区域是白色或黑色的,我完全理解这个问题,但是填充蓝色对我来说没有任何意义。

In the Combine Class I linked above, I have also tried imagecopy, imagecopyresampled, and the vanilla imagecopymerge, both with similar results.

在上面链接的Combine类I中,我还尝试了imagecopy、imagecopysampling和vanilla imagecopymerge,都有类似的结果。

I'm at a total loss.

我完全不知所措。

Edit:

To be clear, my question is this: What part of my code is incorrect? Why is it filling the transparent area with a color (instead of black or white) and how can I fix it while still maintaining the ability to merge images with transparency?

明确地说,我的问题是:我代码的哪一部分是不正确的?为什么它在透明的区域填充颜色(而不是黑色或白色),我如何在保持图像与透明的能力的同时修复它?

Update:

Please note, when a new Image object is created, it calls newImage which contains the following code:

请注意,当创建一个新的图像对象时,它调用newImage,该对象包含以下代码:

$this->handle = imagecreatetruecolor($this->width, $this->height);
imagealphablending($this->handle, false);
imagesavealpha($this->handle, true);

I feel like that might be easy to miss.

我觉得这样很容易错过。

1 个解决方案

#1


2

Note that it doesn't matter that you create the handle in newImage and call imagealphablending and imagesavealpha on it, because loadImage throws that handle away.

注意,在newImage中创建句柄并调用imagealphamerge和imagesavealpha并不重要,因为loadImage将该句柄抛出。

The reason it is "filling" the transparent area with the blue color is that it is not filling the transparent area with anything at all. It it just completely dropping the alpha channel, and the blue color is what happens to be stored in those pixels with an alpha of zero. Note this may be difficult to see in a graphics program, as that program may itself replace completely-transparent pixels with black or white.

之所以用蓝色填充透明区域,是因为它没有填充透明区域。它完全去掉了alpha通道,而蓝色则是存储在这些像素中的值,alpha值为0。注意,这在图形程序中可能很难看到,因为程序本身可能会用黑色或白色来替换完全透明的像素。

As for what is wrong with your code, I can't say for sure as I don't get the same results as you report when I try your existing code. But if I change your loadImage to something like this so the source images are forced to true color, it works for me:

至于您的代码有什么问题,我不能肯定地说,当我尝试您的现有代码时,没有得到与您报告相同的结果。但是如果我把你的loadImage改成这样源图像就会被迫变成真实的颜色,这对我来说是行得通的:

            private function loadImage()
            {
                    $img = null;
                    switch( $this->type )
                    {
                            case 1:
                                            $img = imagecreatefromgif($this->source);
                                            break;
                            case 2:
                                            $img = imagecreatefromjpeg($this->source);
                                            break;
                            case 3:
                                            $img = imagecreatefrompng($this->source);
                                            break;
                            default:
                                            break;
                    }
                    if (!$img) return false;
                    $this->handle = imagecreatetruecolor($this->width, $this->height);
                    imagealphablending($this->handle, false);
                    imagesavealpha($this->handle, true);
                    imagecopyresampled($this->handle, $img, 0, 0, 0, 0, $this->width, $this->height, $this->width, $this->height);
                    return true;
            }

(Personally, I prefer ImageMagick over GD).

(就我个人而言,我更喜欢ImageMagick而不是GD)。

更多相关文章

  1. 从JSON字符串/数组中提取第一个图像
  2. PHP开发微信支付代码及支付通知处理
  3. 如何调整图像覆盖滑块
  4. 几个有用的php字符串过滤,转换函数代码
  5. 使用ASIHTTPRequest从iOS上传图像
  6. 在Web Page中包含PHP代码
  7. php mail函数一段好的代码
  8. 在PHP中将PDF文档转换为预览图像,而无需安装Ghostscript
  9. 用于上传多个文件的PHP代码

随机推荐

  1. python3 自定义比较函数
  2. 在jupyter笔记本中使用ipywidgets
  3. python如何将一个txt文件里的转化为相应
  4. 简单的python爬取网页字符串内容并保存
  5. Python学习笔记(8)
  6. pip常用命令汇总
  7. 在Python中显示来自控制台的图像
  8. python正则表达式的使用
  9. Python发送邮件(最全)
  10. python:inspect函数自动生成函数名